Originally Posted by

**QuaX**
Actually it looks very complicated to me as im on very early phase of programming,

Isnt there another way to do it with For loop ?

Look closer; if you know how to multiply a matrix you should be able to follow this easily (the reason I added those notes originally was because I'd never done matrix multiplication before, so I learned the procedure (rows by columns) from a website).

You are pretty much doing the same thing in your Original Post. Here's the prototype again:

Code:

int **multiply (int *A[], int *B[], int Ar, int Ac, int Br, int Bc)

It returns a **matrix AB. The first two parameters are the the matrices to multiply; AB is the product. Since these are not necessarily square and could be of any size, rather than try and measure by dividing with sizeof, I just include the dimensions (Ar = rows in A, Ac = columns in A, etc). So "if (Ac!=Br)" is just a check to make sure that B has as many rows as A has columns -- otherwise the matrices cannot be multiplied

If you are using square matrices that are of the same set size, and don't want to use malloc, you could simplify it a lot:

Code:

void multiply (int A[3][3], int B[3][3], int AB[3][3]) {
int i, ii, iii;
for (i=0; i<3; i++) {
for (ii=0; ii<3; ii++)
for (iii=0; iii<3; iii++) AB[i][ii]+=A[i][iii]*B[iii][ii];
}
}

Make sure AB is all zeros first!

Here's a demo that multiplies by an "identity matrix" (B), so the product (AB) should be identical to A:

Code:

#include <stdio.h>
void show (int matrix[3][3]) {
int i, ii;
for (i=0; i<3; i++) {
for (ii=0; ii<3; ii++) printf("%5d",matrix[i][ii]);
printf("\n");
}
}
void multiply (int A[3][3], int B[3][3], int AB[3][3]) {
int i, ii, iii;
for (i=0; i<3; i++) {
for (ii=0; ii<3; ii++)
for (iii=0; iii<3; iii++) AB[i][ii]+=A[i][iii]*B[iii][ii];
}
}
int main() {
int mx1[3][3]={ {3,-1,-2}, {2,-2,-1}, {6, 6, 6} },
mx2[3][3]={ {1,0,0}, {0,1,0}, {0,0,1} },
product[3][3]={{0,0,0}};
puts("__A__");
show(mx1);
puts("__B__");
show(mx2);
multiply(mx1,mx2,product);
puts("__AB__");
show(product);
return 0;
}