이 튜토리얼에서는 동일한 행렬의 Row-major 및 Column-major order를 추가하여 형성된 행렬의 자취를 찾는 프로그램에 대해 설명합니다.
이를 위해 행 우선 배열과 열 우선 배열의 두 가지 배열이 제공됩니다. 우리의 임무는 주어진 두 행렬을 더하여 형성된 행렬의 자취를 찾는 것입니다.
예시
#include <bits/stdc++.h>
using namespace std;
//calculating the calculateMatrixTrace of the new matrix
int calculateMatrixTrace(int row, int column) {
int A[row][column], B[row][column], C[row][column];
int count = 1;
for (int i = 0; i < row; i++)
for (int j = 0; j < column; j++) {
A[i][j] = count;
count++;
}
count = 1;
for (int i = 0; i < row; i++)
for (int j = 0; j < column; j++) {
B[j][i] = count;
count++;
}
for (int i = 0; i < row; i++)
for (int j = 0; j < column; j++)
C[i][j] = A[i][j] + B[i][j];
int sum = 0;
for (int i = 0; i < row; i++)
for (int j = 0; j < column; j++)
if (i == j)
sum += C[i][j];
return sum;
}
int main() {
int ROW = 6, COLUMN = 9;
cout << calculateMatrixTrace(ROW, COLUMN) << endl;
return 0;
} 출력
384