Dark Mode On/Off

LAST UPDATED ON: SEPTEMBER 17, 2024

Do you know waht Normal and Trace of a square matrix are? Well before we jump to the program, we should first try to understand how we calculate the Normal and Trace for a Matric.

The square root of the sum of the squares of each element of the matrix is said to be **Normal** of a matrix.

The sum of the diagonal elements of a matrix is called **Trace**. Let's consider the following matrix A.

Here, the elements which are highlighted are diagonal elements of the matrix. If we sum them, we will find trace of the matrix as follows :-

**Trace of A = 1 + 2 + 2 = 5**

To find the normal, we will square each element and find sum of those squared elements. After that, we will calculate square root of the sum calculated. The resulting number will be Normal of our matrix. Let's calculate Normal for A.

Normal = (√(1^{2} + 3^{2} + 5^{2} + 4^{2} + 2^{2} + 6^{2} + 5^{2} + 9^{2} + 2^{2}))

Normal = (√(195))

**Normal = 13.96**

Few important points to remember:

**Normal**and**Trace**are only defined for a square matrix.**Square Matrix:**Matrix in which, the**number of rows = number of columns**.**Diagonal Element:**An element having same indices for row and column.

To find Trace of a matrix, the algorithm is as below:

- Let the matrix be
**A[m][n]** - trace = 0
- for i = 0 to m
- for j = 0 to n
- if i == j
- trace += A[i][j]

The algorithm for finding Normal of a matrix is as follows:

- Let the matrix be
**A[m][n]** - sum = 0, normal = 0
- for i = 0 to m
- for j = 0 to n
- A[i][j] *= A[i][j], sum += A[i][j]
- normal = sqrt(sum)

So let's see the complete program now. We are using the **math.h** header library in this program.

In some latest versions of C compilers (gcc), you have to **explicitly specify** that you want to use **math.h** header in the program, when you compile it.

You can do so by adding the `-lm`

flag at the end of the gcc command. For example, `gcc program.c -o program -lm`

So when you run this code in your local setup, remember this.

```
#include<stdio.h>
/*
to use the sqrt method to find
the square root of a number we include
math.h header file
*/
#include<math.h>
int main()
{
printf("\n\n\t\tStudytonight - Best place to learn\n\n\n");
int i, j, n, aj[10][10], sum = 0, sum1 = 0, a = 0, normal;
printf("\nEnter the number of rows (columns) of the matrix: \n\n");
scanf("%d", &n);
printf("\nEnter the %d elements of the first matrix: \n\n", n*n);
for(i = 0; i < n; i++) // to iterate the rows
{
for(j = 0; j < n; j++) // to iterate the columns
{
scanf("%d", &aj[i][j]);
a = aj[i][j]*aj[i][j]; // finding square of each element
sum1 += a; // same as sum1 = sum1 + a
}
}
normal = sqrt((double)sum1); // typecasting to double value
printf("\n\nThe normal of the given matrix is: %d", normal);
for(i = 0; i < n; i++)
{
sum = sum + aj[i][i]; // sum of the diagonal elements
}
printf("\n\nThe Trace of the given matrix is: %d", sum);
printf("\n\n\t\t\tCoding is Fun !\n\n\n");
return 0;
}
```

The output of the program will be,