943,808 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 10302
  • C++ RSS
You are currently viewing page 1 of this multi-page discussion thread
Apr 3rd, 2009
0

Matrix Multiplication using Multi-Dimensional Arrays

Expand Post »
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?

C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. #include <fstream>
  3.  
  4. using namespace std;
  5.  
  6.  
  7. int main()
  8.  
  9. {
  10.  
  11. ifstream infile("MATRIX.dat");
  12. ofstream outfile ("RESULT.dat");
  13.  
  14. int m1[6][6], m2[6][6], M[6][6];
  15. int i=0, j=0;
  16.  
  17. for (i=0; i<6; i++)
  18. {
  19. for (j=0; j<6; j++)
  20. {infile>>m1[i][j]
  21. >>m2[i][j];
  22. M[i][j] = m1[0][j]*m2[i][0];
  23. cout<<M[i][j]<<" ";
  24. } cout<<endl;
  25. }
  26.  
  27.  
  28. infile.close ();
  29. outfile.close ();
  30.  
  31. return 0;
  32. }


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!!!
Similar Threads
Reputation Points: 7
Solved Threads: 0
Light Poster
ARYT is offline Offline
38 posts
since Feb 2009
Apr 3rd, 2009
0

Re: Matrix Multiplication using Multi-Dimensional Arrays

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.

http://en.wikipedia.org/wiki/Matrix_multiplication
Reputation Points: 85
Solved Threads: 42
Nearly a Posting Virtuoso
vishesh is offline Offline
1,362 posts
since Oct 2006
Apr 3rd, 2009
0

Re: Matrix Multiplication using Multi-Dimensional Arrays

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.
Reputation Points: 86
Solved Threads: 43
Posting Pro
NicAx64 is offline Offline
532 posts
since Mar 2009
Apr 3rd, 2009
0

Re: Matrix Multiplication using Multi-Dimensional Arrays

Click to Expand / Collapse  Quote originally posted by ARYT ...
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.

http://justmathtutoring.com/
http://tutorial.math.lamar.edu/
http://www.sosmath.com/
http://www.purplemath.com/modules/index.htm

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.
Reputation Points: 86
Solved Threads: 43
Posting Pro
NicAx64 is offline Offline
532 posts
since Mar 2009
Apr 3rd, 2009
0

Re: Matrix Multiplication using Multi-Dimensional Arrays

Click to Expand / Collapse  Quote originally posted by NicAx64 ...
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.

http://justmathtutoring.com/
http://tutorial.math.lamar.edu/
http://www.sosmath.com/
http://www.purplemath.com/modules/index.htm

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?
Reputation Points: 7
Solved Threads: 0
Light Poster
ARYT is offline Offline
38 posts
since Feb 2009
Apr 3rd, 2009
0

Re: Matrix Multiplication using Multi-Dimensional Arrays

Click to Expand / Collapse  Quote originally posted by vishesh ...
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.

http://en.wikipedia.org/wiki/Matrix_multiplication
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?".

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

Can you please give a code, algorithm or sth for that?
Reputation Points: 7
Solved Threads: 0
Light Poster
ARYT is offline Offline
38 posts
since Feb 2009
Apr 3rd, 2009
0

Re: Matrix Multiplication using Multi-Dimensional Arrays

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 !
Reputation Points: 86
Solved Threads: 43
Posting Pro
NicAx64 is offline Offline
532 posts
since Mar 2009
Apr 3rd, 2009
0

Re: Matrix Multiplication using Multi-Dimensional Arrays

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 +=
C++ Syntax (Toggle Plain Text)
  1. i ranges from 0 to rows - 1
  2. j ranges from 0 to columns - 1
  3. k ranges from 0 to n - 1
  4. M(i, j) += A(i, k) x B(k, j)
Reputation Points: 718
Solved Threads: 373
Nearly a Posting Maven
Lerner is offline Offline
2,253 posts
since Jul 2005
Apr 5th, 2009
0

Re: Matrix Multiplication using Multi-Dimensional Arrays

No, you're making it more complicated than it is. It's a multiplication of two 6x6 matrices.
Reputation Points: 7
Solved Threads: 0
Light Poster
ARYT is offline Offline
38 posts
since Feb 2009
Apr 5th, 2009
0

Re: Matrix Multiplication using Multi-Dimensional Arrays

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

C++ Syntax (Toggle Plain Text)
  1. int m1[6][6], m2[6][6], M[6][6];
  2. int i=0, j=0, k=0;
  3.  
  4. for (i=0; i<6; i++)
  5. {
  6. for (j=0; j<6; j++)
  7. {
  8. for (k=0; k<6; k++)
  9. {infile>>m1[i][j]
  10. >>m2[i][j];
  11. M[i][j] = m1[i][k]*m2[k][j];
  12. cout<<M[i][j]<<" ";
  13. }} cout<<endl;
  14. }
Last edited by ARYT; Apr 5th, 2009 at 2:32 am.
Reputation Points: 7
Solved Threads: 0
Light Poster
ARYT is offline Offline
38 posts
since Feb 2009

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: solution for 3 programs
Next Thread in C++ Forum Timeline: Implementing Linked List iterator, gcc is acting wierd





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC