| | |
Matrix Multiplication using Multi-Dimensional Arrays
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
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?
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!!!
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)
#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!!!
Religion is a way of austerity and a mental self-tranquilizer which is associated with numerous cultures. One cannot deny that Moses predominate Jewish over other believers, while Mohammad calls Muslims to be the ones who excel in humanity and still they are the messengers sent from one God, postulating the impacts of religion and ideology on genetics?
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
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
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.
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.
Nothing like a kernel pannic !
•
•
•
•
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.
Nothing like a kernel pannic !
•
•
•
•
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.
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?
Religion is a way of austerity and a mental self-tranquilizer which is associated with numerous cultures. One cannot deny that Moses predominate Jewish over other believers, while Mohammad calls Muslims to be the ones who excel in humanity and still they are the messengers sent from one God, postulating the impacts of religion and ideology on genetics?
•
•
•
•
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
I just can't figure out the code for that.
Can you please give a code, algorithm or sth for that?
Religion is a way of austerity and a mental self-tranquilizer which is associated with numerous cultures. One cannot deny that Moses predominate Jewish over other believers, while Mohammad calls Muslims to be the ones who excel in humanity and still they are the messengers sent from one God, postulating the impacts of religion and ideology on genetics?
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 !
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 !
Nothing like a kernel pannic !
•
•
Join Date: Jul 2005
Posts: 1,676
Reputation:
Solved Threads: 262
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 +=
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)
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)
Klatu Barada Nikto
No, you're making it more complicated than it is. It's a multiplication of two 6x6 matrices.
Religion is a way of austerity and a mental self-tranquilizer which is associated with numerous cultures. One cannot deny that Moses predominate Jewish over other believers, while Mohammad calls Muslims to be the ones who excel in humanity and still they are the messengers sent from one God, postulating the impacts of religion and ideology on genetics?
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)
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; }
Last edited by ARYT; Apr 5th, 2009 at 2:32 am.
Religion is a way of austerity and a mental self-tranquilizer which is associated with numerous cultures. One cannot deny that Moses predominate Jewish over other believers, while Mohammad calls Muslims to be the ones who excel in humanity and still they are the messengers sent from one God, postulating the impacts of religion and ideology on genetics?
![]() |
Similar Threads
Other Threads in the C++ Forum
- Previous Thread: solution for 3 programs
- Next Thread: Implementing Linked List iterator, gcc is acting wierd
| Thread Tools | Search this Thread |
api array based beginner binary bitmap c++ c/c++ calculator char class classes code compile compiler console conversion count database delete deploy desktop developer directshow dll download dynamic dynamiccharacterarray encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux list loop looping loops map math matrix memory multiple news node number output parameter pointer problem program programming project proxy python read recursion recursive reference return rpg string strings struct temperature template templates test text text-file tree unix url variable vector visualstudio win32 windows winsock word wordfrequency wxwidgets






