(C++) CPP Programs - 18

Friday, December 14, 2007

18. Write a C program to read matrixes A (M*N) and B (M*N) and perform addition OR subtraction of A and B. Find the trace of the resultant matrix. Output the given matrices, their sum OR difference and the trace.

/*Addition, Subtraction of matrix*/

#include
#include

void main()
{
int a[5][5],b[5][5],c[5][5],m,n,trace=0,i,j,opt;
clrscr();

printf("Enter the order of matrix\n\a");
scanf("%d%d",&m,&n);

printf("Enter the elements of a\n");
for(i=0;i < m;i++)
for(j=0;j < n;j++)
scanf("%d",&a[i][j]);

printf("Enter the elements for matrix b\n\a");
for(i=0;i < m;i++)
for(j=0;j < n;j++)
scanf("%d",&b[i][j]);

printf("\n1.Addition\n2.Subtraction\nEnter any one option\n");
scanf("%d",&opt);

printf("The matrix a is\n");
for(i=0;i < m;i++)
{
for(j=0;j < n;j++)
printf("%4d",a[i][j]);
printf("\n");
}

printf("The matrix b is\n");
for(i=0;i < m;i++)
{
for(j=0;j < n;j++)
printf("%4d",b[i][j]);
printf("\n");
}

if(opt==1)
{
printf("The resultant matrix c is\n");
for(i=0;i < m;i++)
{
for(j=0;j < n;j++)
c[i][j]=a[i][j]+b[i][j];
}
}

else
{
for(i=0;i < m;i++)
{
for(j=0;j < n;j++)
c[i][j]=a[i][j]-b[i][j];
}
}
printf("The obtained matrix c is\n\a");
for(i=0;i < m;i++)
{
for(j=0;j < n;j++)
printf("%4d",c[i][j]);
printf("\n");
}
if(m==n)
{
printf("The trace exist\n");
for(i=0;i < m;i++)
trace+=c[i][i];
printf("The trace =%d\n",trace);
}

getch();
}

0 comments:

Post a Comment

Chitika

About This Blog

Followers

Blog Archive

  © Blogger template The Professional Template II by Ourblogtemplates.com 2009

Back to TOP