Program to Find Normal and Trace of a Matrix in C++ using For loop
ADVERTISEMENTS
Program to find normal and trace of a matrix in c++ using for loop. In this article, you will learn how to make program to find normal and trace of a matrix in c++ using for loop.
What is the Normal of a square matrix?
The square root of the sum of the squares of each element of the matrix.
What is the Trace of a square matrix?
The sum of the diagonal elements of the square matrix.
Source Code
// Program to Find Normal and Trace of a Matrix in C++ using For loop
#include <iostream>
#include <cmath>
using namespace std;
int main() {
int i, j, n, sum = 0, sum1 = 0, a = 0, normal;
cout << "\nEnter the number of rows[columns] of the matrix::\n\n";
cin >> n;
int x[n][n];
cout << "\n----Enter the " << n * n << " elements of the first matrix----\n\n";
for(i = 0; i < n; i++) {
for(j = 0; j < n; j++) {
cin >> x[i][j];
a = x[i][j]*x[i][j];
sum1 += a;
}
}
normal = sqrt((double) sum1);
cout << "\n\nThe normal of the given matrix is: " << normal;
for(i = 0; i < n; i++) {
sum = sum + x[i][i];
}
cout << "\n\nThe Trace of the given matrix is: " << sum << "\n";
return 0;
}
Output
Enter the number of rows[columns] of the matrix::
3
----Enter the 9 elements of the first matrix----
12 34 6 23 65 76 23 76 3
The normal of the given matrix is: 134
The Trace of the given matrix is: 80