이 튜토리얼에서는 주어진 행렬에 대한 각 행과 열의 합을 찾는 프로그램에 대해 논의할 것입니다.
이를 위해 A*B 행렬이 주어집니다. 우리의 임무는 행렬의 모든 요소를 탐색하고 행렬의 각 행과 각 열의 합을 찾는 것입니다.
예시
#include <iostream>
using namespace std;
#define m 7
#define n 6
//calculating sum of each row
void calc_rsum(int arr[m][n]){
int i,j,sum = 0;
for (i = 0; i < 4; ++i) {
for (j = 0; j < 4; ++j) {
sum = sum + arr[i][j];
}
cout << "Sum of the row "<< i << ": " << sum << endl;
sum = 0;
}
}
//calculating sum of each column
void calc_csum(int arr[m][n]) {
int i,j,sum = 0;
for (i = 0; i < 4; ++i) {
for (j = 0; j < 4; ++j) {
sum = sum + arr[j][i];
}
cout << "Sum of the column "<< i << ": " << sum <<endl;
sum = 0;
}
}
int main() {
int i,j;
int arr[m][n];
int x = 1;
for (i = 0; i < m; i++)
for (j = 0; j < n; j++)
arr[i][j] = x++;
calc_rsum(arr);
calc_csum(arr);
return 0;
} 출력
Sum of the row 0: 10 Sum of the row 1: 34 Sum of the row 2: 58 Sum of the row 3: 82 Sum of the column 0: 40 Sum of the column 1: 44 Sum of the column 2: 48 Sum of the column 3: 52