This is a program to find the trace & normal of a given matrix.
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.
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.
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.
/*
* C program to find the trace and normal of a matrix
*
* Trace is defined as the sum of main diagonal elements and
* Normal is defined as square root of the sum of all the elements
*/
#include <stdio.h>
#include <math.h>
void main ()
{
static int array[10][10];
int i, j, m, n, sum = 0, sum1 = 0, a = 0, normal;
printf("Enter the order of the matrix\n");
scanf("%d %d", &m, &n);
printf("Enter the n coefficients of the matrix \n");
for (i = 0; i < m; ++i)
{
for (j = 0; j < n; ++j)
{
scanf("%d", &array[i][j]);
a = array[i][j] * array[i][j];
sum1 = sum1 + a;
}
}
normal = sqrt(sum1);
printf("The normal of the given matrix is = %d\n", normal);
for (i = 0; i < m; ++i)
{
sum = sum + array[i][i];
}
printf("Trace of the matrix is = %d\n", sum);
}
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.
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.
- Apply for Computer Science Internship
- Watch Advanced C Programming Videos
- Check C Books
- Check Computer Science Books
- Apply for C Internship