Friday, May 21, 2010

We have two matrix 3x3 and 4x4 how can i do this matrix sum,minus,multiply,barter,reve... in C language?

Result is such as c=IxxxxI


IxxxxI

We have two matrix 3x3 and 4x4 how can i do this matrix sum,minus,multiply,barter,reve... in C language?
I'm sure that you know that a 3X3 and a 4X4 matrix cannot be added, but anyway, here are general programs for sum,minus,multiply, etc:





Multiplication:





#include %26lt;tstdio.h%26gt;





void mult_matrices(int a[][3], int b[][3], int result[][3]);


void print_matrix(int a[][3]);





void main(void)


{


int p[3][3] = { {1, 3, -4}, {1, 1, -2}, {-1, -2, 5} };


int q[3][3] = { {8, 3, 0}, {3, 10, 2}, {0, 2, 6} };


int r[3][3];





mult_matrices(p, q, r);


print_matrix(r);


}





void mult_matrices(int a[][3], int b[][3], int result[][3])


{


int i, j, k;


for(i=0; i%26amp;lt3; i++)


{


for(j=0; j%26amp;lt3; j++)


{


for(k=0; k%26amp;lt3; k++)


{


result[i][j] = a[i][k] + b[k][j];


}


}


}


}





void print_matrix(int a[][3])


{


int i, j;


for (i=0; i%26amp;lt3; i++)


{


for (j=0; j%26amp;lt3; j++)


{


printf("%d\t", a[i][j]);


}


printf("\n");


}


}





#include %26amp;ltstdio.h%26gt;





void add_matrices(int a[][3], int b[][3], int result[][3]);


void print_matrix(int a[][3]);





void main(void)


{


int p[3][3] = { {1, 3, -4}, {1, 1, -2}, {-1, -2, 5} };


int q[3][3] = { {8, 3, 0}, {3, 10, 2}, {0, 2, 6} };


int r[3][3];





add_matrices(p, q, r);





printf("\nMatrix 1:\n");


print_matrix(p);





printf("\nMatrix 2:\n");


print_matrix(q);





printf("\nResult:\n");


print_matrix(r);


}








Matrix Add:





void add_matrices(int a[][3], int b[][3], int result[][3])


{


int i, j;


for(i=0; i%26amp;lt3; i++)


{


for(j=0; j%26amp;lt3; j++)


{


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


}


}


}





void print_matrix(int a[][3])


{


int i, j;


for (i=0; i%26amp;lt3; i++)


{


for (j=0; j%26amp;lt3; j++)


{


printf("%d\t", a[i][j]);


}


printf("\n");


}


}





Transpose:





#include%26lt;conio.h%26gt;


#include%26lt;stdio.h%26gt;


main()


{


int a[3][3],i,j,temp;


clrscr();


textcolor(GREEN);


cprintf("TRANSPOSE OF 3X3 MATRIX(by usman riaz)");


printf ("\nEnter digits into the matrix with one space:\n");


printf("Press enter when 3 digits are entered;i.e\n");


printf(" 1 2 3 %26lt;ENTER%26gt;\n");


printf(" 4 5 6 %26lt;ENTER%26gt;\n");


printf(" 7 8 9 %26lt;ENTER%26gt;\n");


printf(" %26lt;-----------%26gt;\n");


printf(" NOW ENTER NO'S\n");


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


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


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


}


i=0;


j=0 ;


for(int b=0;b%26lt;2;b++){





temp= a[i][j+1];


a[i][j+1]=a[i+1][j];


a[i+1][j]=temp;





i=i+1;


j=j+1;


if(b==1){


i=0;


j=0;


temp=a[i][j+2];


a[i][j+2]=a[i+2][j];


a[i+2][j]=temp;


}


}


printf("\nthe transpose of the matrix is:\n");


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


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


printf("%d\t",a[i][j]);


printf("\n");


}


getch();


}
Reply:hey improve ur maths concept none of these operation can be preformed on this size of array. sum and min u need two n x n arrays. but for multiply u need m x n and n x m array or simply 2 n x n array.
Reply:The C programming language allows you to store values in a two-dimensional array but you must do the matrix operations yourself.





So to do matrix multiplication you're going to have to write a program to take one value in the first matrix and multiply it by one value in the second matrix until you've done all of the elements.





Use a FOR loop.


No comments:

Post a Comment