C 언어 행렬 덧셈 및 뺄셈

CBeginner
지금 연습하기

소개

이 랩에서는 C 프로그래밍 언어를 사용하여 행렬 덧셈과 뺄셈을 수행하는 방법을 배우게 됩니다. 이 프로그램은 사용자에게 동일한 크기의 두 행렬을 입력하도록 요청한 다음 덧셈과 뺄셈 연산을 수행하고 결과 행렬을 출력합니다.

참고: 코딩을 연습하고 gcc 를 사용하여 컴파일하고 실행하는 방법을 배우려면 직접 ~/project/main.c 파일을 생성해야 합니다.

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

프로그램 설정

먼저, 필요한 라이브러리를 포함하고 main 함수를 정의하여 프로그램을 설정합니다.

#include<stdio.h>

int main()
{
    // Add code here
}

변수 선언

main 함수에서 필요한 변수를 선언합니다. 각 입력에 대해 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");
}

행렬 덧셈

두 행렬의 해당 요소를 더하고 결과를 합계 행렬에 저장합니다.

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

행렬 뺄셈

두 행렬의 해당 요소를 빼고 결과를 차이 행렬 (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 프로그래밍 언어에서 행렬 덧셈과 뺄셈의 과정을 배웠습니다. 사용자에게 동일한 크기의 두 행렬을 입력하도록 요청한 다음 덧셈 및 뺄셈 연산을 수행하고 결과를 화면에 표시하는 프로그램을 만들었습니다.