• Courses
  • Tutorials
  • Jobs
  • Practice
  • Contests
September 24, 2022 |970 Views
C program to find Normal and Trace of a Matrix
  Share  2 Likes
Description
Discussion

In this video, we will write a C program to find the normal and trace of a matrix.

Normal: A Normal is the square root of the sum of squares of each element in a matrix.
Trace: A Trace is the sum of the primary diagonal i.e starting from top-left to bottom-right.

Example:

Input: 

mat[][] = {{9, 2, 7},
{4, 5, 6},
{3, 8, 1}};

Output:

Normal = 16
Trace = 15

Explanation:
Normal = sqrt(9*9+ 2*2 + 7*7 + 4*4 + 5*5 + 6*6 + 3*3 + 8*8 + 1*1) = 16
Trace = 1+5+9 = 15

Here we see 2 different methods for getting a normal and trace of a matrix:

1. Using the Iterative method:
The first is Iterative, we use nested for loop and single for loop to calculate normal and trace. Here we will sum up the squares of each element and then find the square root of it. In this method, we have another loop to calculate the trace by adding diagonal elements.

2. Using the Non-Iterative method:
In the non-iterative method, we calculate both normal and trace in input for loops only, which doesn't require another for loops. So approach-2 is beneficial with respect to time complexity.

Time Complexity: O(n*n)
Space Complexity: O(1)
 
C program for normal & trace matrix: https://www.geeksforgeeks.org/c-program-to-find-normal-and-trace-of-matrix/

Read More