7.07.2017

2차원 배열 - 두 행렬의 합

//C++로 시작하는 객체지향 프로그래밍 p.356 예제 8.5 - 대수학 : 두 행렬의 합
#include <iostream>
using namespace std;
const int N = 3;

void addMatrix(const double a[][N], const double b[][N], double c[][N]){
for (int i = 0; i < N; i++)
for (int j = 0; j < N; j++)
c[i][j] = a[i][j] + b[i][j];
}

void printResult(double a[][N], double b[][N], double c[][N]) {
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
cout << " " << a[i][j] << ' ';
}
           if (i == N / 2)
      cout << "  =  ";
   else cout << "     ";
}
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++)
cout << " " << b[i][j] << ' ';
            if (i == N / 2)
       cout << "  =  ";
    else cout << "     ";
}
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
cout << " " << c[i][j] << ' ';
                 if (i == N / 2)
            cout << "  =  ";
        else cout << "     ";
                cout << endl;
                }
}
}


int main() {
int i, j;
double a[N][N], b[N][N], c[N][N];
cout << "Enter Matrix 1: ";
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++)
cin >> a[i][j];
}

cout << "Enter Matrix 2: ";
for (int i = 0; i < N; i++) {
for (int k = 0; k < N; k++)
cin >> b[i][k];
}
addMatrix(a, b, c);
printResult(a, b, c);
system("pause");
return 0;
}

댓글 없음:

댓글 쓰기