C 言語による行列の加算と減算

CBeginner
オンラインで実践に進む

はじめに

この実験では、C プログラミング言語で行列の加算と減算を行う方法を学びます。このプログラムでは、ユーザーに同じサイズの 2 つの行列を入力してもらい、その後加算と減算の演算を行い、結果の行列を表示します。

注意:コーディングの練習と、gcc を使用してコンパイルおよび実行する方法を学ぶために、~/project/main.c ファイルを自分で作成する必要があります。

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

プログラムをセットアップする

まず、必要なライブラリをインクルードし、メイン関数を定義してプログラムをセットアップします。

#include<stdio.h>

int main()
{
    // Add code here
}

変数を宣言する

メイン関数内で必要な変数を宣言します。入力用の行列を 2 つ、加算と減算の結果を格納する行列を 2 つ必要とします。

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

nm は行列の行数と列数です。

firstsecond は入力行列です。

sumdiff はそれぞれ加算と減算の結果を格納する行列です。

ユーザー入力を受け取る

scanf() 関数を使用して、ユーザーに行列の行数と列数を尋ね、その後行列の要素を入力してもらいます。

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]);

入力した行列を表示する

printf() 関数を使用して入力行列を表示します。

/*
    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");
}

行列の加算

2 つの行列の対応する要素を加算し、その結果を和行列に格納します。

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

加算結果を表示する

printf() 関数を使用して和行列を表示します。

// 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");
}

行列の減算

2 つの行列の対応する要素を減算し、その結果を差行列(diff matrix)に格納します。

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

減算結果を表示する

printf() 関数を使用して差行列(diff matrix)を表示します。

// 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");
}

最終コード

これがプログラムの最終コードです。

#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;
}

まとめ

この実験では、C プログラミング言語における行列の加算と減算のプロセスを学びました。ユーザーに同じサイズの 2 つの行列を入力させ、加算と減算の演算を行い、その結果を画面に表示するプログラムを作成しました。