# Find normal and Trace of a matrix in C++

Hello, in this tutorial, we will learn how to find normal and the trace of a matrix with a solved example, algorithm, and C++ program.

**Normal** of a matrix is the square root of the sum of the squares of each element in that matrix.

**Trace **of a square matrix is the sum of the primary diagonal elements.

## How to find normal and the trace of a matrix in C++

let’s take an example and solve it manually to understand it in a better way.

**Example**

consider a 4*4 matrix input matrix: 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 finding normal and trace of the matrix: normal = sqrt(1*1 + 2*2 + 3*3 + 4*4 + 5*5 + 6*6 + 7*7 + 8*8 + 9*9 + 0*0 + 1*1 + 2*2 + 3*3 + 4*4 + 5*5 + 6*6) = sqrt(1 + 4 + 9 + 16 + 25 + 36 + 49 + 64 + 81 + 0 + 1 + 4 + 9 + 16 + 25 + 36) = sqrt(376) = 19.39 Trace = 1 + 6 + 1 + 6 = 14 Therefore, normal and Trace of input matrix are 376 and 14 respectively.

**Approach:**

To find the normal of a matrix, traverse the matrix form starting to end. Add the square of each element of the matrix and display it. To find the Trace, traverse the matrix, add the diagonal element and display the addition.

### Algorithm to find normal and the trace of a matrix

- Declare and initialize a matrix of size
**n*n**. - declare two variable consider
**double**and**trace**and initialize it with zero. - Traverse the matrix from
**matrix[i][j]**to**matrix[n-1][n-1]**, for each**matrix[i][j]**add the into**double.** - for each
**matrix[i][j]**, when**i**is equal to**j**, add each**matrix[i][j]**to the variable**trace**. - display square root of
**double**and the value of**trace**.

## C++ program to find normal and the trace of a matrix

#include<bits/stdc++.h> using namespace std; //Function to find Normal of a matrixrix of size n x n(m) float Normal(int matrix[50][50], int n) { int sum = 0; for (int i=0; i<n; i++) for (int j=0; j<n; j++) sum += matrix[i][j]*matrix[i][j]; return sqrt(sum); } //Function to find trace of a matrixrix of size n x n(m) int Trace(int matrix[50][50], int n) { int sum = 0; for (int i=0; i<n; i++) sum += matrix[i][i]; return sum; } // Driven source int main() { int matrixrix[50][50], n, m; //taking input in m and n for length of row and column cout<<"Enter the length of row of the matrix: "; cin >> m; cout<<"Enter the length of column of the matrix: "; cin >> n; if(m != n){ cout<<"\nlength of row and column must be same."; exit(0); } //taking input in the matrixrix cout << "\nInput the element of matrix: \n"; for(int i = 0; i < m; i++) for(int j = 0; j < n; j++) cin>>matrixrix[i][j]; cout << "\nTrace of Matrix = " << Trace(matrixrix, n) << endl; cout << "Normal of Matrix = " << Normal(matrixrix, n) << endl; return 0; }

**Output **

Enter the length of row of the matrix: 4 Enter the length of column of the matrix: 4 Input the element of matrix: 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 Trace of Matrix = 14 Normal of Matrix = 19.3907

**Time complexity: **

**O(n^2) **where **n** is the length of row and column of the matrix.

**you may also read: **

## Leave a Reply