Monday, July 27, 2009

Write a program to add and subtract two 3*3 matrices in c language.?

a program should be made in c language

Write a program to add and subtract two 3*3 matrices in c language.?
#include%26lt;stdio.h%26gt;


void main()


{


int a[3][3],b[3][3],c[3][3],i,j;





printf("Enter the First 3x3 Matrix : ");


for(i=0;i%26lt;3;i++)


{


for(j=0;j%26lt;3;j++)


scanf("%d",%26amp;a[i][j]);


}





printf("Enter the Second 3x3 Matrix : ");


for(i=0;i%26lt;3;i++)


{


for(j=0;j%26lt;3;j++)


scanf("%d",%26amp;b[i][j]);


}


printf("\nThe Sum of Matrices is : ");


for(i=0;i%26lt;3;i++)


{


for(j=0;j%26lt;3;j++)


{


c[i][j]=a[i][j]+b[i][j];


printf("%d ",%26amp;c[i][j];


}


printf("\n");


}








printf("\n\nThe Difference of Matrices is : ");


for(i=0;i%26lt;3;i++)


{


for(j=0;j%26lt;3;j++)


{


c[i][j]=a[i][j]-b[i][j];


printf("%d ",%26amp;c[i][j];


}


printf("\n");


}





}





if u want to add or sub greater dimension matrices





declare a,b,c with [10][10]


declare two variable r for rows and c for columns


accept r and c





and in the i for loop replace 3 by r


and in the j for loop replace 3 by c
Reply:int m=3; int n=3;


int i,j;


int a[m][n], b[m][n],c[m][n],d[m][n];


for (i=0;i%26lt;3;i++){


for (j=0;j%26lt;3;j++){


cout %26lt;%26lt; "enter row elements one at a time :"


cin %26gt;%26gt; a[i][j] ;


}}


cout %26lt;%26lt; "Enter the elements of the second matrix by row " %26lt;%26lt; endl;


for (i=0;i%26lt;3;i++){


for (j=0;j%26lt;3;j++){


cout %26lt;%26lt; "enter row elements one at a time :"


cin %26gt;%26gt; b[i][j] ;


}}


for (i=0;i%26lt;3;i++){


for (j=0;j%26lt;3;j++){


c[i][j]=a[i][j]+b[i][j];


d[i][j]=a[i][j]-b[i][j];


}


}


cout %26lt;%26lt; "output the elements of the matrix for sum " %26lt;%26lt;endl;


for (i=0;i%26lt;3;i++){


for (j=0;j%26lt;3;j++){





cout %26lt;%26lt; c[i][j] ;


}


endl;


}





cout %26lt;%26lt; "output the elements of the matrix for sum " %26lt;%26lt;endl;


for (i=0;i%26lt;3;i++){


for (j=0;j%26lt;3;j++){





cout %26lt;%26lt; d[i][j] ;


}


endl;


}





Sorry I have no time to complete
Reply:Sounds like Coursework lol
Reply:I could probably do it in C++, which is the same as C. All you have to do is enter 18 variables: A1, A2, ... , A9, B1, B2, ... , B9.





Then calculate A1+B1, A2+B2, etc, and print out the results.





It would be fairly simple to print out both add and subtract. I don't think that I am good enough to give the option of adding OR subtracting!

mothers day flowers

No comments:

Post a Comment