대각선 요소가 처음에 비어 있는 하나의 3x3 행렬이 있다고 가정합니다. 행, 열, 대각선의 합이 같도록 대각선을 채워야 합니다. 행렬이 −
와 같다고 가정합니다.

채우고 나면 -

대각선 요소가 x, y, z라고 가정합니다. 값은 -
- x =(M[2, 3] + M[3, 2])/ 2
- z =(M[1, 2] + M[2, 1])/ 2
- y =(x + z)/2
예
#include<iostream>
using namespace std;
void displayMatrix(int matrix[3][3]) {
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++)
cout << matrix[i][j] << " ";
cout << endl;
}
}
void fillDiagonal(int matrix[3][3]) {
matrix[0][0] = (matrix[1][2] + matrix[2][1]) / 2;
matrix[2][2] = (matrix[0][1] + matrix[1][0]) / 2;
matrix[1][1] = (matrix[0][0] + matrix[2][2]) / 2;
cout << "Final Matrix" << endl;
displayMatrix(matrix);
}
int main() {
int matrix[3][3] = {
{ 0, 3, 6 },
{ 5, 0, 5 },
{ 4, 7, 0 }};
cout << "Given Matrix" << endl;
displayMatrix(matrix);
fillDiagonal(matrix);
} 출력
Given Matrix 0 3 6 5 0 5 4 7 0 Final Matrix 6 3 6 5 5 5 4 7 4