C Program to Find Norm and Trace of Matrix
Write C Program to Find Norm and Trace of Matrix
// C Program to Find Norm and Trace of Matrix
// Normal is defined as the square root of the sum of the square of matrix elements
// Trace is defined as the sum of the diagonal element
#include <stdio.h>
#include <math.h>
#define max 100;
int norm(int A[3][3],int n)
{
int sum = 0, i, j;
for(i = 0; i < n ; i++)
{
for(j = 0; j < n; j++)
{
sum += A[i][j] * A[i][j];
}
}
return (sqrt(sum));
}
int trace(int A[3][3],int n)
{
int sum = 0, i;
for(i = 0; i < n ; i++)
{
sum = sum + A[i][i];
}
return sum;
}
int main()
{
int i, j, A[3][3] = {{2, 6, 7},{6, 2, 8},{0, 1, 3}};
printf("Matrix A :--> \n");
for(i = 0;i < 3; i++)
{
for( j = 0; j < 3; j++)
{
printf("%d ", A[i][j]);
}
printf("\n");
}
printf("\n\nNorm of Matrix :--> %d",norm(A,3));
printf("\nTrace of Matrix :--> %d",trace(A,3));
return 0;
}
Output:
Matrix A :-->
2 6 7
6 2 8
0 1 3
Norm of Matrix :--> 14
Trace of Matrix :--> 7