Hi everyone

This should be one of our last assignments. Well, not sure, but most probably.
As the title implies, we're supposed to write a program using multi-dimensional arrays for a 6x6 matrix multiplication.

Here's what I've done. It's almost OK; except the most important part which is the final result. It's wrong.

What's the algorithm for matrix multiplication?

``````#include <iostream>
#include <fstream>

using namespace std;

int main()

{

ifstream infile("MATRIX.dat");
ofstream outfile ("RESULT.dat");

int m1[6][6], m2[6][6], M[6][6];
int i=0, j=0;

for (i=0; i<6; i++)
{
for (j=0; j<6; j++)
{infile>>m1[i][j]
>>m2[i][j];
M[i][j] = m1[0][j]*m2[i][0];
cout<<M[i][j]<<" ";
} cout<<endl;
}

infile.close ();
outfile.close ();

return 0;
}``````

BTW, Ancient Dragon, Our tutor said, I asked you to use End Of File function. Why you invent sth different when you can't solve it? Insist on it, and solve it that way. :@
WTF!!! :'(

## All 12 Replies

What you have done is multiply corresponding elements of matrices. However its not so. First of all the two matrices should not be necessarily of same dimensions. It should be rather of form NxP and PxM whose product is of form nxm. I guess your teacher wants you to multiply two square matrices of dimension 6.

Take a look at this article at wii describing how to multiply matrices, and then try to implement it on code, in case of problem post. :)

The multiplication of matrices defines this rule.

If M = A X B , suppose that A is mxn and B is nxp
then , M is a m x p agree !

M(i,j) = ( k=0 to n ) Σ { A (i,k) x B (k,j) } ----------- (1)

are you agree with the (1) , if not you have to re read this and draw matrices and think again and again until you clear this.it's important.

BTW, Ancient Dragon, Our tutor said, I asked you to use End Of File function. Why you invent sth different when you can't solve it? Insist on it, and solve it that way. :@
WTF!!! :'(

Hmm , be patient bro ! don't bug the lectures like that , they can implement it they never give something that you can't or he can't
slove. But the problem is they think in their level. This is mostly true
for teaching the mathematics like subjects.

I also have this experience , I understood nothing my lectures said. I pass that subject using these resources. If you feeling the
same confusion , then may be these will be useful.

I know how you feeling now. But the only solution is just be patient and try to understand ! you will surely get it.

This post is not offense with the lectures ! but This is the true in most uni's and only the smartest 1% of the students will only get what the lecture told. May be this post also will be helpful them to
think in a different way. Look at the first site , it contains math tutor and it optimized for the student's understand capacity.

Hmm , be patient bro ! don't bug the lectures like that , they can implement it they never give something that you can't or he can't
slove. But the problem is they think in their level. This is mostly true
for teaching the mathematics like subjects.

I also have this experience , I understood nothing my lectures said. I pass that subject using these resources. If you feeling the
same confusion , then may be these will be useful.

I know how you feeling now. But the only solution is just be patient and try to understand ! you will surely get it.

This post is not offense with the lectures ! but This is the true in most uni's and only the smartest 1% of the students will only get what the lecture told. May be this post also will be helpful them to
think in a different way. Look at the first site , it contains math tutor and it optimized for the student's understand capacity.

well, thanks. I've also taken Engineering Calculus this semester, but I was talking about our Programming tutor. ;)

So, Can't you give me a code for that multiplication?

BTW, I have some problems with Chain rule (For differentiation), Both partial differentiation and integration and Jacobian.
Can you help me with that too?:D

What you have done is multiply corresponding elements of matrices. However its not so. First of all the two matrices should not be necessarily of same dimensions. It should be rather of form NxP and PxM whose product is of form nxm. I guess your teacher wants you to multiply two square matrices of dimension 6.

Take a look at this article at wii describing how to multiply matrices, and then try to implement it on code, in case of problem post. :)

You know what?! I actually couldn't remember the matrix multiplication from high school math; so, right there at the tutorial class, I took my iPhone out of my pocket and went to Wikipedia to find it out. The tutor was looking at me, somehow like saying, "What the hell are you doing there?". :D

I just can't figure out the code for that.

Can you please give a code, algorithm or sth for that?

M(i,j) = ( k=0 to n ) Σ { A (i,k) x B (k,j) }

so if you can underestand this then you can code you'r own.
This is the so called algorithm , the real implementation is like this.

1. first fetch your two input matrices
2. take the size's of the matrices into int's am , an , bm , bn
3. chek whether an = bm if not say error message
4. your answer matrix should be in the from amx bn therefore
allocate a memory space for that . and using the for loops run that
M(i,j) = ( k=0 to n ) Σ { A (i,k) x B (k,j) } to until j=1 to bn for each
i=1 to an.
note :you can implement the k=0 to n using also a for loop.

Just three for loops ! Just give up a try !

In math:
M(i,j) = ( k = 1 to n ) Σ { A (i,k) x B (k,j) }

In C++:
There are 8 variables to deal with:
M, A, and B are matrixes (multidimensional arrays in C++ speak)

rows is the number of rows in M and the number of rows in A

columns is the number of columns in M and the number of columns in B.

n is the number of columns in A and the number of rows in B, which must be equal to do a dot product multiplication for matrixes.

i is a given row

j is a given column

D(i, j) is a given element of a matrix with indices i representing the row and j representing the column.

Σ in C++ is essentially +=

``````i ranges from 0 to rows - 1
j ranges from 0 to columns - 1
k ranges from 0 to n - 1
M(i, j) +=  A(i, k) x B(k, j)``````

No, you're making it more complicated than it is. It's a multiplication of two 6x6 matrices.

OK. Here I've tried to define another variable which is k, but came up with a load of garbage.

``````int m1[6][6], m2[6][6], M[6][6];
int i=0, j=0, k=0;

for (i=0; i<6; i++)
{
for (j=0; j<6; j++)
{
for (k=0; k<6; k++)
{infile>>m1[i][j]
>>m2[i][j];
M[i][j] = m1[i][k]*m2[k][j];
cout<<M[i][j]<<" ";
}} cout<<endl;
}``````

First of all replace `M[i][j] = m1[i][k]*m2[k][j];` with `M[i][j] += m1[i][k]*m2[k][j];` . See the algorithm in above post, for explanation. Also dont forget to set M[j] value to 0 before running third loop.

``````infile >> m1[i][j] >> m2[i][j];
M[i][j] += m1[i][k]*m2[k][j];``````

Here in second line you are multiplying two array members which haven't got their value yet. You're taking values for different cells. Better take inputs earlier than start multiplying matrices or take it like `infile >> m1[i][k] >> m2[k][j]` but wouldnt it be a bit awkward :)

Well, thanks but I think k shouldn't have a value. It’s basically 0 and later it's used for increment only.
I've also added that += and your infile method, but no difference. :(

Oops...I just missed a point in previous post. Anyway the loop gotta look like this.

``````//take matrices values before starting
for (int i=0; i<6; i++)
{
for (int j=0; j<6; j++)
{
M[i][j] = 0;
for (int k=0; k<6; k++)
{
M[i][j] += m1[i][k] * m2[k][j];
}
cout << M[i][j] << " ";  // this is what i missed to tell
}
cout<<endl;
}``````