# Matrix Multiplication

Discussion in 'C' started by abhi, Sep 11, 2008.

1. ### abhiNew Member

Joined:
Aug 30, 2008
Messages:
3
0
Trophy Points:
0
pls let me know the logic for multiplying a matrix...i have been racking my brains on that

2. ### aortizbNew Member

Joined:
Sep 12, 2008
Messages:
32
0
Trophy Points:
0

3. ### xpi0t0sMentor

Joined:
Aug 6, 2004
Messages:
3,009
203
Trophy Points:
63
Occupation:
Senior Support Engineer
Location:
England
Multiplying by a constant or multiplying two matrices?
If the former then it's just a case of multiplying each element by that number. a{p,q,r} = {ap,aq,ar}.
Multiplying two matrices is more involved, first you must check that the number of columns in the first equals the number of rows in the second, then:
[a0 a1] [b0 b1 b2] = [a0b0+a1b3 a0b1+a1b4 a0b2+a1b5 ]
[a2 a3] [b3 b4 b5] [a2b0+a3b3 a2b1+a2b4 a2b2+a3b5 ]

Is it the maths you're stuck on or the implementation?

4. ### abhiNew Member

Joined:
Aug 30, 2008
Messages:
3
0
Trophy Points:
0
i want the source code not the math behind it.... how to implement it using the concepts of loops and arrays only?

5. ### xpi0t0sMentor

Joined:
Aug 6, 2004
Messages:
3,009
203
Trophy Points:
63
Occupation:
Senior Support Engineer
Location:
England
It's not difficult, where are you stuck? If I give you two matrices, e.g.
[ 1 2 ] [ 5 6 7 ]
[ 3 4 ] [ 8 9 0 ]

how far do you get with "write a program to multiply them"? Do you at least get a main function?
Obviously you'll need some way to represent the matrices, you're correct to use arrays, that's exactly what they're designed for, and you're not going to be able to do this generically without some kind of loop.
Have you figured out how to store the matrices in the arrays yet?

6. ### abhiNew Member

Joined:
Aug 30, 2008
Messages:
3
0
Trophy Points:
0
yes i do,
i have taken the two input matrix matrix declared as a b using loop
n use 3 for loops,one inside other
i,j,k,initiated thm at zero, & i<=2,j<=2, k<=0
n used
C[j]=c[j]+a[j]*b[j][k]
is it correct?

Joined:
Aug 6, 2004
Messages:
3,009