查找由相同矩阵的行优先和列优先顺序相加形成的矩阵的迹
本教程中,我们将讨论一个程序,找到由相同矩阵的行优先和列优先顺序相加形成的矩阵的迹。
为此,我们将提供两个数组,一个为行优先,另一个为列优先。我们的任务是找到由给定的两个矩阵相加形成的矩阵的迹。
示例
#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
广告