C Program to Find Normal and Trace of a Matrix

This is a program to find the trace & normal of a given matrix.

Problem Description

This C Program find the trace & normal of a given matrix. Here trace of the matrix is the sum of the elements of the main diagonal i.e the diagonal from the upper left to the lower right of a matrix. Normal of the matrix is the square root of the sum of all the elements.

Problem Solution

1. Create a matrix and define all its elements.
2. To evaluate normal of the matrix, take sum of all the elements of the array and calculate the square root of it.
3. To evaluate trace of the matrix, take sum of the main diagonal elements.

Program/Source Code

Here is source code of the C program to find the trace & normal of a given matrix. The program is successfully compiled and tested using Turbo C compiler in windows environment. The program output is also shown below.

  1.     /*
  2.      * C program to find the trace and normal of a matrix
  3.      *
  4.      * Trace is defined as the sum of main diagonal elements and
  5.      * Normal is defined as square root of the sum of all the elements
  6.      */
  7.  
  8.     #include <stdio.h>
  9.     #include <math.h>
  10.  
  11.     void main ()
  12.     {
  13.  
  14.         static int array[10][10];
  15.         int i, j, m, n, sum = 0, sum1 = 0, a = 0, normal;
  16.  
  17.         printf("Enter the order of the matrix\n");
  18.         scanf("%d %d", &m, &n);
  19.  
  20.         printf("Enter the n coefficients of the matrix \n");
  21.         for (i = 0; i < m; ++i) 
  22.         {
  23.             for (j = 0; j < n; ++j)
  24.             {
  25.                 scanf("%d", &array[i][j]);
  26.                 a = array[i][j] * array[i][j];
  27.                 sum1 = sum1 + a;
  28.             }
  29.         }
  30.  
  31.         normal = sqrt(sum1);
  32.         printf("The normal of the given matrix is = %d\n", normal);
  33.         for (i = 0; i < m; ++i) 
  34.         {
  35.             sum = sum + array[i][i];
  36.         }
  37.  
  38.         printf("Trace of the matrix is = %d\n", sum);
  39.  
  40.     }
Program Explanation

1. Declare a matrix and define all its elements.
2. Declare variables for storing the normal and trace of the matrix.
3. Find the sum of all the elements of the matrix using nested for loop.
4. Evaluate normal by passing the above calculated sum to sqrt() function.
5. Take sum of all the main diagonal elements of the array to calculate trace.
6. Print trace and normal.

advertisement
Runtime Test Cases
Enter the order of the matrix
3 3
Enter the coefficients of the matrix
3  7 9
2 6 10
8 5 9
The normal of the given matrix is = 21
Trace of the matrix is = 18

Sanfoundry Global Education & Learning Series – 1000 C Programs.

Here’s the list of Best Books in C Programming, Data Structures and Algorithms.

Free 30-Day C Certification Bootcamp is Live. Join Now!

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
I’m Manish - Founder and CTO at Sanfoundry. I’ve been working in tech for over 25 years, with deep focus on Linux kernel, SAN technologies, Advanced C, Full Stack and Scalable website designs.

You can connect with me on LinkedIn, watch my Youtube Masterclasses, or join my Telegram tech discussions.

If you’re in your 40s–60s and exploring new directions in your career, I also offer mentoring. Learn more here.