C++ 를 이용한 행렬 연산

C++Beginner
지금 연습하기

소개

이 랩에서는 C++ 프로그래밍을 사용하여 주어진 2 차원 배열에 대한 덧셈, 뺄셈, 곱셈과 같은 다양한 행렬 연산을 수행하는 방법을 배우게 됩니다. 또한 배열을 효과적으로 조작하고 사용하는 방법도 배우게 됩니다.

C++ 프로그램 파일 생성

~/project 디렉토리에 matrix_operations.cpp라는 새 파일을 생성합니다. 터미널에서 다음 명령을 사용하여 이 작업을 수행할 수 있습니다.

touch ~/project/matrix_operations.cpp

필요한 라이브러리 포함 및 Main 함수 설정

C++ 프로그램의 첫 번째 단계는 입/출력 연산 및 콘솔 지우기에 필요한 라이브러리를 포함하는 것입니다. 그 후, C++ 프로그램의 main 함수를 설정합니다. 다음 코드를 템플릿으로 사용할 수 있습니다.

#include<iostream.h>
#include<conio.h>

int main()
{
    clrscr();
    // Your code here
    getch();
    return 0;
}

2 개의 행렬 덧셈

이 단계에서는 두 행렬의 덧셈을 수행하는 케이스를 생성합니다. C++ 프로그램은 사용자에게 두 행렬의 차원 또는 크기 ( m*no*p 여야 함) 를 입력하도록 요청합니다.

행렬의 차원이 같지 않으면 프로그램은 오류 메시지를 반환합니다. 그런 다음 행렬의 요소를 입력하고 더합니다. 최종 코드는 다음과 같아야 합니다.

//ADDITION
case 1:
    cout<<"\nEnter the order of matrix a (must be m*n): "<<endl;
    cin>>m;
    cout<<"* \n";
    cin>>n;
    cout<<"Enter the order of matrix b (must be o*p): "<<endl;
    cin>>o;
    cout<<"* \n";
    cin>>p;
    if (m==o&&n==p)
    {
        cout<<"Addition possible "<<endl;
    }
    else
    {
        cout<<"Addition not possible ";
        l=0;
    }

    if(l)
    {
        cout<<"\n\nEnter the elements of matrix 1: "<<endl;
        for (i=1;i<=m;i++)
        {
            for (j=1;j<=n;j++)
                cin>>a[i][j];
        }
        cout<<"Elements of matrix 1 are: ";
        for (i=1;i<=m;i++)
        {
            cout<<endl;
            for (j=1;j<=n;j++)
                cout<<a[i][j]<<" ";
        }
        cout<<"\nEnter the elements of matrix 2: "<<endl;
        for (i=1;i<=o;i++)
        {
            for (j=1;j<=p;j++)
                cin>>b[i][j];
        }
        cout<<"Elements of matrix 2 are: ";
        for (i=1;i<=o;i++)
        {
            cout<<endl;
            for (j=1;j<=p;j++)
                cout<<b[i][j]<<" ";
        }
        cout<<"\n\n\nAddition:\nc=a+b=";
        for (i=1;i<=m;i++)
        {
            for (j=1;j<=n;j++)
            {
                c[i][j]=a[i][j]+b[i][j];
            }
        }
        for (i=1;i<=m;i++)
        {
            cout<<endl;
            for (j=1;j<=n;j++)
                cout<<c[i][j]<<" ";
        }
    }
    break;

2 개의 행렬 뺄셈

이 단계에서는 두 행렬의 뺄셈을 수행하는 케이스를 생성합니다. C++ 프로그램은 사용자에게 두 행렬의 차원 또는 크기 ( m*no*p 여야 함) 를 입력하도록 요청합니다.

행렬의 차원이 같지 않으면 프로그램은 오류 메시지를 반환합니다. 그런 다음 행렬의 요소를 입력하고 뺍니다. 최종 코드는 다음과 같아야 합니다.

//SUBTRACTION
case 2:
    cout<<"\nEnter the order of matrix a (must be m*n): "<<endl;
    cin>>m;
    cout<<"* \n";
    cin>>n;
    cout<<"Enter the order of matrix b (must be o*p): "<<endl;
    cin>>o;
    cout<<"* \n";
    cin>>p;
    if (m==o&&n==p)
    {
        cout<<"Subtracion possible "<<endl;
    }
    else
    {
        cout<<"Subtraction not possible ";
        l=0;
    }

    if(l)
    {
        cout<<"\n\nEnter the elements of matrix 1: "<<endl;
        for (i=1;i<=m;i++)
        {
            for (j=1;j<=n;j++)
                cin>>a[i][j];
        }
        cout<<"Elements of matrix 1 are: ";
        for (i=1;i<=m;i++)
        {
            cout<<endl;
            for (j=1;j<=n;j++)
                cout<<a[i][j]<<" ";
        }
        cout<<"\nEnter the elements of matrix 2: "<<endl;
        for (i=1;i<=o;i++)
        {
            for (j=1;j<=p;j++)
                cin>>b[i][j];
        }
        cout<<"Elements of matrix 2 are: ";
        for (i=1;i<=o;i++)
        {
            cout<<endl;
            for (j=1;j<=p;j++)
                cout<<b[i][j]<<" ";
        }
        cout<<"\n\n\nSubtraction:\nc=a-b=";
        for (i=1;i<=m;i++)
        {
            for (j=1;j<=n;j++)
            {
                c[i][j]=a[i][j]-b[i][j];
            }
        }
        for (i=1;i<=m;i++)
        {
            cout<<endl;
            for (j=1;j<=n;j++)
                cout<<c[i][j]<<" ";
        }
    }
    break;

2 개의 행렬 곱셈

이 단계에서는 두 행렬의 곱셈을 수행하는 케이스를 생성합니다. C++ 프로그램은 사용자에게 두 행렬의 차원 또는 크기 ( m*no*p 여야 함) 를 입력하도록 요청합니다.

행렬의 차원이 호환되지 않으면 프로그램은 오류 메시지를 반환합니다. 그런 다음 행렬의 요소를 입력하고 곱합니다. 최종 코드는 다음과 같아야 합니다.

//MULTIPLICATION
case 3:
    cout<<"\nEnter the order of matrix a (must be m*n): "<<endl;
    cin>>m;
    cout<<"* \n";
    cin>>n;
    cout<<"Enter the order of matrix b (must be o*p): "<<endl;
    cin>>o;
    cout<<"* \n";
    cin>>p;
    if (n==o)
    {
        cout<<"Multiplication possible "<<endl;
    }
    else
    {
        cout<<"Multiplication not possible ";
        l=0;
    }

    if(l)
    {
        cout<<"\n\nEnter the elements of matrix 1: "<<endl;
        for (i=1;i<=m;i++)
        {
            for (j=1;j<=n;j++)
                cin>>a[i][j];
        }
        cout<<"Elements of matrix 1 are: ";
        for (i=1;i<=m;i++)
        {
            cout<<endl;
            for (j=1;j<=n;j++)
                cout<<a[i][j]<<" ";
        }
        cout<<"\nEnter the elements of matrix 2: "<<endl;
        for (i=1;i<=o;i++)
        {
            for (j=1;j<=p;j++)
                cin>>b[i][j];
        }
        cout<<"Elements of matrix 2 are: ";
        for (i=1;i<=o;i++)
        {
            cout<<endl;
            for (j=1;j<=p;j++)
                cout<<b[i][j]<<" ";
        }
        cout<<"\n\n\nMultiplication:\nc=aXb=";
        for (i=1;i<=m;i++)
        {
            for (j=1;j<=p;j++)
            {
                c[i][j]=0;
                for (int k=1;k<=n;k++)
                {
                    c[i][j]=c[i][j]+(a[i][k]*b[k][j]);
                }
            }
        }
        for (i=1;i<=m;i++)
        {
            cout<<endl;
            for (j=1;j<=p;j++)
                cout<<c[i][j]<<" ";
        }
    }
    break;

C++ 프로그램 실행 방법

터미널에서 다음 명령을 사용하여 matrix_operations.cpp 파일을 컴파일하여 실행할 수 있습니다.

g++ ~/project/matrix_operations.cpp -o matrix && ./matrix

matrix_operations.cpp를 C++ 프로그램 파일에 사용한 이름으로 바꿔야 할 수도 있습니다.

코드가 예상 출력을 제공한다고 가정하면, 케이스 번호 (덧셈은 1, 뺄셈은 2, 곱셈은 3) 를 묻는 프롬프트를 볼 수 있습니다. 선택 사항을 입력하면 프로그램은 선택한 연산을 기반으로 행렬 입력을 요청합니다.

C++ 프로그램 코드 완성

다음은 matrix_operations.cpp 프로그램 파일의 전체 코드입니다.

#include<iostream.h>
#include<conio.h>

int main()
{
    clrscr();
    int a[50][50],b[50][50],c[50][50],i,j,m,n,o,p,l=1,r;
    cout<<"Enter case:\n";
    cin>>r;
    switch(r)
    {
        //ADDITION
        case 1:
            cout<<"\nEnter the order of matrix a (must be m*n): "<<endl;
            cin>>m;
            cout<<"*\n";
            cin>>n;
            cout<<"Enter the order of matrix b (must be o*p): "<<endl;
            cin>>o;
            cout<<"*\n";
            cin>>p;
            if (m==o&&n==p)
            {
                cout<<"Addition possible "<<endl;
            }
            else
            {
                cout<<"Addition not possible ";
                l=0;
            }

            if(l)
            {
                cout<<"\n\nEnter the elements of matrix 1: "<<endl;
                for (i=1;i<=m;i++)
                {
                    for (j=1;j<=n;j++)
                        cin>>a[i][j];
                }
                cout<<"Elements of matrix 1 are: ";
                for (i=1;i<=m;i++)
                {
                    cout<<endl;
                    for (j=1;j<=n;j++)
                        cout<<a[i][j]<<" ";
                }
                cout<<"\nEnter the elements of matrix 2: "<<endl;
                for (i=1;i<=o;i++)
                {
                    for (j=1;j<=p;j++)
                        cin>>b[i][j];
                }
                cout<<"Elements of matrix 2 are: ";
                for (i=1;i<=o;i++)
                {
                    cout<<endl;
                    for (j=1;j<=p;j++)
                        cout<<b[i][j]<<" ";
                }
                cout<<"\n\n\nAddition:\nc=a+b=";
                for (i=1;i<=m;i++)
                {
                    for (j=1;j<=n;j++)
                    {
                        c[i][j]=a[i][j]+b[i][j];
                    }
                }
                for (i=1;i<=m;i++)
                {
                    cout<<endl;
                    for (j=1;j<=n;j++)
                        cout<<c[i][j]<<" ";
                }
            }
            break;

        //SUBTRACTION
        case 2:
            cout<<"\nEnter the order of matrix a (must be m*n): "<<endl;
            cin>>m;
            cout<<"*\n";
            cin>>n;
            cout<<"Enter the order of matrix b (must be o*p): "<<endl;
            cin>>o;
            cout<<"*\n";
            cin>>p;
            if (m==o&&n==p)
            {
                cout<<"Subtracion possible "<<endl;
            }
            else
            {
                cout<<"Subtraction not possible ";
                l=0;
            }

            if(l)
            {
                cout<<"\n\nEnter the elements of matrix 1: "<<endl;
                for (i=1;i<=m;i++)
                {
                    for (j=1;j<=n;j++)
                        cin>>a[i][j];
                }
                cout<<"Elements of matrix 1 are: ";
                for (i=1;i<=m;i++)
                {
                    cout<<endl;
                    for (j=1;j<=n;j++)
                        cout<<a[i][j]<<" ";
                }
                cout<<"\nEnter the elements of matrix 2: "<<endl;
                for (i=1;i<=o;i++)
                {
                    for (j=1;j<=p;j++)
                        cin>>b[i][j];
                }
                cout<<"Elements of matrix 2 are: ";
                for (i=1;i<=o;i++)
                {
                    cout<<endl;
                    for (j=1;j<=p;j++)
                        cout<<b[i][j]<<" ";
                }
                cout<<"\n\n\nSubtraction:\nc=a-b=";
                for (i=1;i<=m;i++)
                {
                    for (j=1;j<=n;j++)
                    {
                        c[i][j]=a[i][j]-b[i][j];
                    }
                }
                for (i=1;i<=m;i++)
                {
                    cout<<endl;
                    for (j=1;j<=n;j++)
                        cout<<c[i][j]<<" ";
                }
            }
            break;

        //MULTIPLICATION
        case 3:
            cout<<"\nEnter the order of matrix a (must be m*n): "<<endl;
            cin>>m;
            cout<<"*\n";
            cin>>n;
            cout<<"Enter the order of matrix b (must be o*p): "<<endl;
            cin>>o;
            cout<<"*\n";
            cin>>p;
            if (n==o)
            {
                cout<<"Multiplication possible "<<endl;
            }
            else
            {
                cout<<"Multiplication not possible ";
                l=0;
            }

            if(l)
            {
                cout<<"\n\nEnter the elements of matrix 1: "<<endl;
                for (i=1;i<=m;i++)
                {
                    for (j=1;j<=n;j++)
                        cin>>a[i][j];
                }
                cout<<"Elements of matrix 1 are: ";
                for (i=1;i<=m;i++)
                {
                    cout<<endl;
                    for (j=1;j<=n;j++)
                        cout<<a[i][j]<<" ";
                }
                cout<<"\nEnter the elements of matrix 2: "<<endl;
                for (i=1;i<=o;i++)
                {
                    for (j=1;j<=p;j++)
                        cin>>b[i][j];
                }
                cout<<"Elements of matrix 2 are: ";
                for (i=1;i<=o;i++)
                {
                    cout<<endl;
                    for (j=1;j<=p;j++)
                        cout<<b[i][j]<<" ";
                }
                cout<<"\n\n\nMultiplication:\nc=aXb=";
                for (i=1;i<=m;i++)
                {
                    for (j=1;j<=p;j++)
                    {
                        c[i][j]=0;
                        for (int k=1;k<=n;k++)
                        {
                            c[i][j]=c[i][j]+(a[i][k]*b[k][j]);
                        }
                    }
                }
                for (i=1;i<=m;i++)
                {
                    cout<<endl;
                    for (j=1;j<=p;j++)
                        cout<<c[i][j]<<" ";
                }
            }
            break;
        // default case
        default:
            cout<<"Wrong choice";
    }
    getch();
    return 0;
}

요약

이 랩에서는 C++ 프로그래밍을 사용하여 주어진 2 차원 배열에 대한 다양한 행렬 연산을 수행하는 방법을 배웠습니다. 또한 배열을 조작하고 사용하는 방법도 배웠습니다. 이제 C++ 프로그래밍에서 행렬이 어떻게 작동하는지에 대한 훌륭한 이해를 갖게 되셨을 것입니다.