Matrixaddition und -subtraktion in C

CCBeginner
Jetzt üben

💡 Dieser Artikel wurde von AI-Assistenten übersetzt. Um die englische Version anzuzeigen, können Sie hier klicken

Einführung

In diesem Lab werden Sie lernen, Matrixaddition und -subtraktion in der Programmiersprache C durchzuführen. Dieses Programm wird den Benutzer bitten, zwei Matrizen gleicher Größe einzugeben und dann die Addition und Subtraktion durchzuführen und die resultierenden Matrizen auszugeben.

Hinweis: Sie müssen die Datei ~/project/main.c selbst erstellen, um das Programmieren zu üben und zu lernen, wie Sie sie mit gcc kompilieren und ausführen.

cd ~/project
## create main.c
touch main.c
## compile main.c
gcc main.c -o main
## run main
./main

Einrichten des Programms

Zuerst richten Sie das Programm ein, indem Sie die erforderlichen Bibliotheken einbinden und die Hauptfunktion definieren.

#include<stdio.h>

int main()
{
    // Add code here
}

Variablen deklarieren

Deklarieren Sie die erforderlichen Variablen in der Hauptfunktion. Wir benötigen 2 Matrizen, eine für jede Eingabe, und 2 Matrizen, um das Ergebnis der Addition und Subtraktion zu speichern.

int n, m, c, d, first[10][10], second[10][10], sum[10][10], diff[10][10];

n und m sind die Anzahl der Zeilen und Spalten der Matrizen.

first und second sind die Eingabematrizen.

sum und diff sind die Matrizen, die das Ergebnis der Addition und Subtraktion jeweils speichern werden.

Benutzer-Eingabe erhalten

Verwenden Sie die scanf()-Funktion, um den Benutzer nach der Anzahl der Zeilen und Spalten der Matrizen zu fragen und dann die Elemente der Matrizen einzugeben.

printf("\nEnter the number of rows and columns of the first matrix \n\n");
scanf("%d%d", &m, &n);

printf("\nEnter the %d elements of the first matrix \n\n", m*n);
for(c = 0; c < m; c++)   // to iterate the rows
    for(d = 0; d < n; d++)   // to iterate the columns
        scanf("%d", &first[c][d]);

printf("\nEnter the %d elements of the second matrix \n\n", m*n);
for(c = 0; c < m; c++)   // to iterate the rows
    for(d = 0; d < n; d++)   // to iterate the columns
        scanf("%d", &second[c][d]);

Die Eingabematrizen anzeigen

Verwenden Sie die printf()-Funktion, um die Eingabematrizen anzuzeigen.

/*
    printing the first matrix
*/
printf("\n\nThe first matrix is: \n\n");
for(c = 0; c < m; c++)   // to iterate the rows
{
    for(d = 0; d < n; d++)   // to iterate the columns
    {
        printf("%d\t", first[c][d]);
    }
printf("\n");
}

/*
    printing the second matrix
*/
printf("\n\nThe second matrix is: \n\n");
for(c = 0; c < m; c++)   // to iterate the rows
{
    for(d = 0; d < n; d++)   // to iterate the columns
    {
        printf("%d\t", second[c][d]);
    }
printf("\n");
}

Matrixaddition

Addieren Sie die entsprechenden Elemente der beiden Matrizen und speichern Sie das Ergebnis in der Summenmatrix.

for(c = 0; c < m; c++)
    for(d = 0; d < n; d++)
        sum[c][d] = first[c][d] + second[c][d];

Das Ergebnis der Addition anzeigen

Verwenden Sie die printf()-Funktion, um die Summenmatrix anzuzeigen.

// printing the elements of the sum matrix
printf("\n\nThe sum of the two entered matrices is: \n\n");
for(c = 0; c < m; c++)
{
    for(d = 0; d < n; d++)
    {
        printf("%d\t", sum[c][d]);
    }
    printf("\n");
}

Matrixsubtraktion

Subtrahieren Sie die entsprechenden Elemente der beiden Matrizen und speichern Sie das Ergebnis in der Differenzmatrix (diff matrix).

for(c = 0; c < m; c++)
    for(d = 0; d < n; d++)
        diff[c][d] = first[c][d] - second[c][d];

Das Ergebnis der Subtraktion anzeigen

Verwenden Sie die printf()-Funktion, um die Differenzmatrix (diff matrix) anzuzeigen.

// printing the elements of the diff matrix
printf("\n\nThe difference(subtraction) of the two entered matrices is: \n\n");
for(c = 0; c < m; c++)
{
    for(d = 0; d < n; d++)
    {
        printf("%d\t", diff[c][d]);
    }
    printf("\n");
}

Endgültiger Code

Hier ist der endgültige Code für das Programm:

#include<stdio.h>

int main()
{
    int n, m, c, d, first[10][10], second[10][10], sum[10][10], diff[10][10];
    printf("\nEnter the number of rows and columns of the first matrix \n\n");
    scanf("%d%d", &m, &n);

    printf("\nEnter the %d elements of the first matrix \n\n", m*n);
    for(c = 0; c < m; c++)   // to iterate the rows
        for(d = 0; d < n; d++)   // to iterate the columns
            scanf("%d", &first[c][d]);

    printf("\nEnter the %d elements of the second matrix \n\n", m*n);
    for(c = 0; c < m; c++)   // to iterate the rows
        for(d = 0; d < n; d++)   // to iterate the columns
            scanf("%d", &second[c][d]);

    /*
        printing the first matrix
    */
    printf("\n\nThe first matrix is: \n\n");
    for(c = 0; c < m; c++)   // to iterate the rows
    {
        for(d = 0; d < n; d++)   // to iterate the columns
        {
            printf("%d\t", first[c][d]);
        }
    printf("\n");
    }

    /*
        printing the second matrix
    */
    printf("\n\nThe second matrix is: \n\n");
    for(c = 0; c < m; c++)   // to iterate the rows
    {
        for(d = 0; d < n; d++)   // to iterate the columns
        {
            printf("%d\t", second[c][d]);
        }
    printf("\n");
    }

    /*
        finding the SUM of the two matrices
        and storing in another matrix sum of the same size
    */
    for(c = 0; c < m; c++)
        for(d = 0; d < n; d++)
            sum[c][d] = first[c][d] + second[c][d];

    // printing the elements of the sum matrix
    printf("\n\nThe sum of the two entered matrices is: \n\n");
    for(c = 0; c < m; c++)
    {
        for(d = 0; d < n; d++)
        {
            printf("%d\t", sum[c][d]);
        }
        printf("\n");
    }

    /*
        finding the DIFFERENCE of the two matrices
        and storing in another matrix difference of the same size
    */
    for(c = 0; c < m; c++)
        for(d = 0; d < n; d++)
            diff[c][d] = first[c][d] - second[c][d];

    // printing the elements of the diff matrix
    printf("\n\nThe difference(subtraction) of the two entered matrices is: \n\n");
    for(c = 0; c < m; c++)
    {
        for(d = 0; d < n; d++)
        {
            printf("%d\t", diff[c][d]);
        }
        printf("\n");
    }

    return 0;
}

Zusammenfassung

In diesem Lab haben Sie den Prozess der Matrixaddition und -subtraktion in der Programmiersprache C gelernt. Sie haben ein Programm erstellt, das den Benutzer auffordert, zwei Matrizen gleicher Größe einzugeben und dann Addition und Subtraktion durchführt sowie die Ergebnisse auf dem Bildschirm anzeigt.