इस ट्यूटोरियल में, हम एक ही मैट्रिक्स के रो-मेजर और कॉलम-मेजर ऑर्डर को जोड़कर बनने वाले मैट्रिक्स के ट्रेस को खोजने के लिए एक प्रोग्राम पर चर्चा करेंगे।
इसके लिए हमें दो सरणियाँ प्रदान की जाएंगी, एक पंक्ति-मेजर में और दूसरी कॉलममेजर में। हमारा काम दो दिए गए मैट्रिक्स को जोड़ने से बनने वाले मैट्रिक्स का पता लगाना है।
उदाहरण
#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