Tuesday 7 October 2014

Write a C Program to Calculate the Trace and Normal of a Matrix

Trace of Matrix                                                                                              Normal of Matrix


Trace: Trace of a Matrix can be defined as the sum of the main diagonal elements.
Normal: Normal can be defined as the square root of the sum of squares of all the elements of a Matrix.


#include<stdio.h>
#include<conio.h>
void main()
{
int i , j , n ;                                       /* 'i' used for Rows and 'j' used for Columns */
int Row , Column;
int Matrix[9][9];
float trace , sum , normal;

clrscr();
trace = 0;
sum = 0;

printf(" Enter the order of Matrix\n");
scanf("%d * %d" , &Row , &Column);

printf(" Enter the elements of Matrix\n");
for(i=0 ; i<Row ; i++)
{
for(j=0 ; j<Column ; j++)
{
scanf("%d" , &Matrix[i][j] );
}
}

                                                  /* Trace and Normal */

for(i=0 ; i<Row ; i++)
{
for(j=0 ; j<Column ; j++)
{
if(i==j)
{
trace + = Matrix[i][j];                   /* It can also be Written as  trace=trace+Matrix[i][j]  */
}
sum + = pow( Matrix[i][j] , 2 );       
}
}
normal = sqrt(sum);

printf(" Trace = %f\n" , trace);
printf(" Normal = %f\n" , normal);
getch();
}                                                               /* End of main() */

No comments:

Post a Comment