C++ Program to Find the Normal and Trace


2-Dimensional arrays or matrices are very much useful in several applications. Matrices have rows and columns and store numbers inside them. In C++ also we can define 2D matrices using multi-dimensional arrays. In this article, we will see how to calculate the Normal and the Trace of a given matrix using C++.

The Normal is the square root of the sum of all elements present in the matrix. And the trace is the sum of elements present in the main diagonal. Let us see the algorithm and C++ code representation.

Matrix Normal

$\begin{bmatrix} 5 & 1& 8\newline 4 & 3& 9\newline 2& 7& 3\ \end{bmatrix},$

Sum of all elements: (5 + 1 + 8 + 4 + 3 + 9 + 2 + 7 + 3) = 42
Normal: (Square root of the sum of all elements) = √42 = 6.48

In the above example, we have taken one 3 x 3 matrix, here we get the sum of all elements, then perform the square root of it. Let us see the algorithm, for a better understanding.

Algorithm

  • read matrix M as input
  • consider M has n rows and n columns
  • sum := 0
  • for i ranging from 1 to n, do
    • for j ranging from 1 to n, do
      • sum := sum + M[ i ][ j ]
    • end for
  • end for
  • res := square root of the sum
  • return res

Example

#include <iostream>
#include <cmath>
#define N 5
using namespace std;
float solve( int M[ N ][ N ] ){
   int sum = 0;
   for ( int i = 0; i < N; i++ ) {
      for ( int j = 0; j < N; j++ ) {
         sum = sum + M[ i ][ j ];
      }
   }
   return sqrt( sum );
}
int main(){
   int mat1[ N ][ N ] = {
      {5, 8, 74, 21, 69},
      {48, 2, 98, 6, 63},
      {85, 12, 10, 6, 9},
      {6, 12, 18, 32, 5},
      {8, 45, 74, 69, 1},
   };
   cout << "Normal of the first matrix is: " << solve( mat1 ) << endl;
   int mat2[ N ][ N ] = {
      {6, 8, 35, 21, 87},
      {99, 2, 36, 326, 25},
      {15, 215, 3, 157, 8},
      {96, 115, 17, 5, 3},
      {56, 4, 78, 5, 10},
   };
   cout << "Normal of the second matrix is: " << solve( mat2 ) << endl;
}

Output

Normal of the first matrix is: 28.0357
Normal of the second matrix is: 37.8418

Matrix Trace

$\begin{bmatrix} 5 & 1& 8\newline 4 & 3& 9\newline 2& 7& 3\ \end{bmatrix},$

Sum of all elements in main diagonal: (5 + 3 + 3) = 11 which is
the trace of given matrix

In the above example, we have taken one 3 x 3 matrix, here we get the sum of all elements in the major diagonal. The sum is the trace of the matrix. Let us see the algorithm, for a better understanding.

Algorithm

  • read matrix M as input
  • consider M has n rows and n columns
  • sum := 0
  • for i ranging from 1 to n, do
    • sum := sum + M[ i ][ i ]
  • end for
  • return sum

Example

#include <iostream>
#include <cmath>
#define N 5
using namespace std;
float solve( int M[ N ][ N ] ){
   int sum = 0;
   for ( int i = 0; i < N; i++ ) {
      sum = sum + M[ i ][ i ];
   }
   return sum;
}
int main(){
   int mat1[ N ][ N ] = {
      {5, 8, 74, 21, 69},
      {48, 2, 98, 6, 63},
      {85, 12, 10, 6, 9},
      {6, 12, 18, 32, 5},
      {8, 45, 74, 69, 1},
   };
   cout << "Trace of the first matrix is: " << solve( mat1 ) << endl;
   int mat2[ N ][ N ] = {
      {6, 8, 35, 21, 87},
      {99, 2, 36, 326, 25},
      {15, 215, 3, 157, 8},
      {96, 115, 17, 5, 3},
      {56, 4, 78, 5, 10},
   };
   cout << "Trace of the second matrix is: " << solve( mat2 ) << endl;
}

Output

Trace of the first matrix is: 50
Trace of the second matrix is: 26

Conclusion

The normal and trace are matrix operations. To perform these two operations, we need a square matrix (for trace square is needed). The normal is just the square root of the sum of all elements present in the matrix, and the trace is the sum of elements present in the major diagonal of the matrix. The matrix can be represented using 2D arrays in C++. Here we have taken two examples of the matrices with 5 rows and 5 columns (a total of 25 elements). Accessing the matrix needs looping statements with index manipulation. For the normal calculation, we need to go through each element, so two nested loops are needed. And the complexity of this program is O(n2). For trace, as we need to see only the major diagonal, the row and column indices will be the same. So only one for loop is sufficient. It can be calculated in O(n) time.

Updated on: 14-Dec-2022

282 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements