C 言語で 2 つの行列を減算する方法

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

はじめに

この実験では、C プログラムで 2 つの行列の減算を行う方法を学びます。この実験では、以下の手順をカバーします。

まず、2 つの行列の次元と要素をユーザー入力から読み取る方法を学びます。C プログラムを作成し、ユーザーに行数と列数、そして 2 つの行列の要素を入力するように促します。

次に、行列の減算操作を実装します。ここで、2 つの行列の対応する要素を減算し、結果を新しい行列に格納します。最後に、結果の行列を出力する方法を学びます。

行列の次元と要素の読み込み

このステップでは、行列の減算を行う C プログラムで、行列の次元と要素をユーザー入力から読み込む方法を学びます。ユーザーが 2 つの行列のサイズと値を入力できるようにするプログラムを作成します。

まず、行列の減算プログラム用の新しい C ファイルを作成しましょう。

cd ~/project
nano matrix_subtraction.c

次に、ファイルに以下のコードを追加します。

#include <stdio.h>

#define MAX_SIZE 100

int main() {
    int rows, cols;
    int matrix1[MAX_SIZE][MAX_SIZE];
    int matrix2[MAX_SIZE][MAX_SIZE];

    // 行列の次元を読み込む
    printf("Enter the number of rows: ");
    scanf("%d", &rows);
    printf("Enter the number of columns: ");
    scanf("%d", &cols);

    // 最初の行列の要素を入力
    printf("Enter elements of the first matrix:\n");
    for (int i = 0; i < rows; i++) {
        for (int j = 0; j < cols; j++) {
            printf("Enter element [%d][%d]: ", i, j);
            scanf("%d", &matrix1[i][j]);
        }
    }

    // 2 番目の行列の要素を入力
    printf("Enter elements of the second matrix:\n");
    for (int i = 0; i < rows; i++) {
        for (int j = 0; j < cols; j++) {
            printf("Enter element [%d][%d]: ", i, j);
            scanf("%d", &matrix2[i][j]);
        }
    }

    return 0;
}

出力例:

Enter the number of rows: 2
Enter the number of columns: 2
Enter elements of the first matrix:
Enter element [0][0]: 5
Enter element [0][1]: 6
Enter element [1][0]: 7
Enter element [1][1]: 8
Enter elements of the second matrix:
Enter element [0][0]: 1
Enter element [0][1]: 2
Enter element [1][0]: 3
Enter element [1][1]: 4

プログラムをコンパイルして実行しましょう。

gcc matrix_subtraction.c -o matrix_subtraction
./matrix_subtraction

コードの説明:

  • MAX_SIZE を使用して、最大行列サイズを 100x100 に定義しています。
  • rowscols に行列の次元を格納します。
  • 2 次元配列 matrix1matrix2 を作成して、行列要素を格納します。
  • scanf() を使用して、ユーザー入力から行列の次元と要素を読み込みます。
  • ネストされたループを使用して、両方の行列の要素を入力します。

対応する要素の減算

このステップでは、2 つの行列の対応する要素を減算し、その結果を新しい行列に格納する方法を学びます。前のプログラムを拡張して、行列の減算を実行します。

既存のファイルを開いてコードを修正します。

cd ~/project
nano matrix_subtraction.c

行列減算ロジックでコードを更新します。

#include <stdio.h>

#define MAX_SIZE 100

int main() {
    int rows, cols;
    int matrix1[MAX_SIZE][MAX_SIZE];
    int matrix2[MAX_SIZE][MAX_SIZE];
    int result[MAX_SIZE][MAX_SIZE];

    // 行列の次元を読み込む
    printf("Enter the number of rows: ");
    scanf("%d", &rows);
    printf("Enter the number of columns: ");
    scanf("%d", &cols);

    // 最初の行列の要素を入力
    printf("Enter elements of the first matrix:\n");
    for (int i = 0; i < rows; i++) {
        for (int j = 0; j < cols; j++) {
            printf("Enter element [%d][%d]: ", i, j);
            scanf("%d", &matrix1[i][j]);
        }
    }

    // 2 番目の行列の要素を入力
    printf("Enter elements of the second matrix:\n");
    for (int i = 0; i < rows; i++) {
        for (int j = 0; j < cols; j++) {
            printf("Enter element [%d][%d]: ", i, j);
            scanf("%d", &matrix2[i][j]);
        }
    }

    // 対応する要素を減算する
    for (int i = 0; i < rows; i++) {
        for (int j = 0; j < cols; j++) {
            result[i][j] = matrix1[i][j] - matrix2[i][j];
        }
    }

    return 0;
}

出力例:

Enter the number of rows: 2
Enter the number of columns: 2
Enter elements of the first matrix:
Enter element [0][0]: 5
Enter element [0][1]: 6
Enter element [1][0]: 7
Enter element [1][1]: 8
Enter elements of the second matrix:
Enter element [0][0]: 1
Enter element [0][1]: 2
Enter element [1][0]: 3
Enter element [1][1]: 4

コードの説明:

  • 減算結果を格納するための新しい 2 次元配列resultが作成されます。
  • ネストされたループは、各行列要素を反復処理します。
  • result[i][j] は、matrix1matrix2 から対応する要素を減算することによって計算されます。
  • 減算は、要素ごとの行列減算規則に従います。

プログラムをコンパイルします。

gcc matrix_subtraction.c -o matrix_subtraction

In this step, you will learn how to print the resulting matrix after subtraction. We'll modify the previous program to display the subtraction results.

Open the existing file and update the code:

cd ~/project
nano matrix_subtraction.c

Update the code with matrix printing logic:

#include <stdio.h>

#define MAX_SIZE 100

int main() {
    int rows, cols;
    int matrix1[MAX_SIZE][MAX_SIZE];
    int matrix2[MAX_SIZE][MAX_SIZE];
    int result[MAX_SIZE][MAX_SIZE];

    // Read matrix dimensions
    printf("Enter the number of rows: ");
    scanf("%d", &rows);
    printf("Enter the number of columns: ");
    scanf("%d", &cols);

    // Input elements for the first matrix
    printf("Enter elements of the first matrix:\n");
    for (int i = 0; i < rows; i++) {
        for (int j = 0; j < cols; j++) {
            printf("Enter element [%d][%d]: ", i, j);
            scanf("%d", &matrix1[i][j]);
        }
    }

    // Input elements for the second matrix
    printf("Enter elements of the second matrix:\n");
    for (int i = 0; i < rows; i++) {
        for (int j = 0; j < cols; j++) {
            printf("Enter element [%d][%d]: ", i, j);
            scanf("%d", &matrix2[i][j]);
        }
    }

    // Subtract corresponding elements
    for (int i = 0; i < rows; i++) {
        for (int j = 0; j < cols; j++) {
            result[i][j] = matrix1[i][j] - matrix2[i][j];
        }
    }

    // Print the resulting matrix
    printf("\nResulting Matrix after Subtraction:\n");
    for (int i = 0; i < rows; i++) {
        for (int j = 0; j < cols; j++) {
            printf("%d ", result[i][j]);
        }
        printf("\n");
    }

    return 0;
}

Compile and run the program:

gcc matrix_subtraction.c -o matrix_subtraction
./matrix_subtraction

Example output:

Enter the number of rows: 2
Enter the number of columns: 2
Enter elements of the first matrix:
Enter element [0][0]: 5
Enter element [0][1]: 6
Enter element [1][0]: 7
Enter element [1][1]: 8
Enter elements of the second matrix:
Enter element [0][0]: 1
Enter element [0][1]: 2
Enter element [1][0]: 3
Enter element [1][1]: 4

Resulting Matrix after Subtraction:
4 4
4 4

Code Explanation:

  • A new nested loop is added to print the result matrix
  • printf("%d ", result[i][j]) prints each element
  • An additional printf("\n") creates a new line after each row
  • The output shows the matrix with subtracted values

まとめ

この実験では、C プログラムでユーザー入力を用いて行列の次元と要素を読み込み、行列の減算を行う方法を学びました。このプログラムは、ユーザーに 2 つの行列のサイズと値を入力させ、それらを 2 つの 2 次元配列に格納します。このステップは、2 つの行列の対応する要素を減算し、結果の行列を出力する次のステップの基礎となります。

プログラムは、ユーザーに何行何列の行列を入力するかを尋ね、その後、最初の行列と 2 番目の行列の要素を入力する手順を案内します。これにより、プログラムは異なるサイズの行列を適切に処理し、さらに処理するための入力データを格納することができます。