Signup/Sign In

Find Normal and Trace of a Square Matrix in C Language

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.

What are Normal and Trace of a Matrix?

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.

matrix diagonal

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 = (√(12 + 32 + 52 + 42 + 22 + 62 + 52 + 92 + 22))

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.

Algorithm for Normal and Trace of a Square Matrix Program in C

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

  1. Let the matrix be A[m][n]
  2. trace = 0
  3. for i = 0 to m
  4. for j = 0 to n
  5. if i == j
  6. trace += A[i][j]

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

  1. Let the matrix be A[m][n]
  2. sum = 0, normal = 0
  3. for i = 0 to m
  4. for j = 0 to n
  5. A[i][j] *= A[i][j], sum += A[i][j]
  6. normal = sqrt(sum)

C Program to find Normal and Trace of Square Matrix

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;
}

Program Output:

The output of the program will be,

Program to find Normal and Trace of a Square Matrix