Thursday, November 27, 2014

c program to input data into an m*n matrix and calculate the trace and normal of the matrix

/*...................................................................................................................................................
File :rec11.c
program: Write a Menu driven program to input data into an m*n matrix and calculate the
(1):Trace of the matrix
(2):norm of the matrix ie sqrt(sum of squares(a[i][j]))
author: sujithbaby440@gmail.com
.................................................................................................................................................. */
#include<stdio.h>
#include<math.h>
main()
{
int a[20][20],dsum=0,nsum=0,m,n,i,j,c;
float normal;
system("clear");
printf("Program to find the trace and norm of a matrix\n");
printf("Enter the order of the matrix\n");
scanf("%d%d",&m,&n);
printf("Enter the elements of the matrix\n");
for(i=1;i<=m;i++){
for(j=1;j<=n;j++){
scanf("%d",&a[i][j]);
}
}
printf("The matrix\n");
for(i=1;i<=m;i++){
for(j=1;j<=n;j++){
printf("%d ",a[i][j]);
}
printf("\n");
}
printf("Enter your choice\n");
printf("(1):Trace of the matrix\n(2):norm of the matrix\n");
scanf("%d",&c);
for(i=1;i<=m;i++){
for(j=1;j<=n;j++){
if(i==j){
dsum=dsum+a[i][j];
}
nsum=nsum+(a[i][j]*a[i][j]);
}
}
normal= sqrt(nsum);
if(c==1){
printf("The trace of the matrix is %d\n",dsum);
}
if(c==2){
printf("The norm of the matrix\t %f\n",normal);
}
}



OUTPUT 1
Program to find the trace and norm of a matrix
Enter the order of the matrix
3 3
Enter the elements of the matrix
1 5 6
7 8 9
1 2 3
The matrix
1 5 6
7 8 9
1 2 3
Enter your choice
(1):Trace of the matrix
(2):norm of the matrix
1
The trace of the matrix is 12
OUTPUT 2
Program to find the trace and norm of a matrix
Enter the order of the matrix
3 3
Enter the elements of the matrix
1 2 3
4 5 6
7 8 9
The matrix
1 2 3
4 5 6
7 8 9
Enter your choice
(1):Trace of the matrix
(2):norm of the matrix
2
The norm of the matrix 16.881943

No comments:

Post a Comment