| | |
Matrix multiplication
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Nov 2009
Posts: 2
Reputation:
Solved Threads: 0
Hey, so I have to make a program that has to multiply two 3x3 matrices (among other things). I used classes to do the project, but I just can't seem to get this multiplication to work. Here is my code so far:
Any advice would be great, and please let me know if there's anything I should clarify. Thank You.
C++ Syntax (Toggle Plain Text)
void matrix::mult(const matrix& u) { double c[9]; double a[9]; for (int i=0; i<rows; i++) { for (int j=0; j<columns; j++) { for(int r=0; r<columns; r++) { c[i*columns+j]+=a[i*columns+r]*u.data[r*columns+j]; } } } }
Any advice would be great, and please let me know if there's anything I should clarify. Thank You.
•
•
Join Date: Nov 2008
Posts: 394
Reputation:
Solved Threads: 72
0
#2 25 Days Ago
First thing that comes to mind is that
Next you are using variables columns and row. So check that this->rows == u.columns. Then set the data in this. as c[] is just ignored.
Other than that it looks ok.
a[9] and c[9] are not initialized.Next you are using variables columns and row. So check that this->rows == u.columns. Then set the data in this. as c[] is just ignored.
Other than that it looks ok.
Last edited by StuXYZ; 25 Days Ago at 11:35 pm.
experience is the most expensive way to learn anything
•
•
Join Date: Nov 2009
Posts: 2
Reputation:
Solved Threads: 0
0
#3 25 Days Ago
Hey, thanks for replying. I'm not quite sure what you mean with the above though. And if it matters, rows and columns aren't actually variables, they're constants.
0
#4 24 Days Ago
Why do you have 3 loops for a 1d vector?
If you want to emulate it as a 2d then all you need is 2 for loops.
Its better to make a function that converts (j+4*i) into a 2d index so it becomes more readable.
If you want to emulate it as a 2d then all you need is 2 for loops.
C++ Syntax (Toggle Plain Text)
int A[4] = {1,2,3,4}; int B[4] = {1,2,3,4}; int R[4] = {0}; for(int i = 0; i < 4; i++) { for(int j = 0; j < 4; j++) R[ j + 4*i] = A[j + 4*i] * B[j + 4*i]; }
Its better to make a function that converts (j+4*i) into a 2d index so it becomes more readable.
1) What word becomes shorter if you add a letter to it? [ Solved by : niek_e, Paul Thompson] 2) What does this sequence equal to : (.5u - .5a)(.5u-.5b)(.5u-.5c) ...[*] [*solved by : murtan] 3) What is the 123456789 prime numer?
0
#5 24 Days Ago
Hi.
Try this. Naive, but should give you some idea.
The way you architect the classes entirely depends on you.
Cheers.
Try this. Naive, but should give you some idea.
The way you architect the classes entirely depends on you.
C++ Syntax (Toggle Plain Text)
#include<iostream> using namespace std; const int SIZE = 3; class Matrix{ public: int matrix[SIZE][SIZE]; void read_matrix(); }; class MatOps{ public: Matrix mul_matrix(Matrix &, Matrix &); }; void Matrix :: read_matrix(){ cout<<"Enter matrix(3 x 3)"<<endl; int i = 0; int j = 0; for(i = 0; i < SIZE; i++){ for(j = 0; j < SIZE; j++){ cin>>matrix[i][j]; } }//for ends }//read_matrix() ends Matrix MatOps :: mul_matrix(Matrix & a, Matrix & b){ Matrix result; int i = 0; int j = 0; int k = 0; for(i = 0; i < SIZE; i++){ for(j = 0; j < SIZE; j++){ result.matrix[i][j] = 0; for(k = 0; k < SIZE; k++){ result.matrix[i][j] += a.matrix[i][k] * b.matrix[k][j]; } } }//for ends return result; }//read_matrix() ends int main(){ Matrix a, b; a.read_matrix(); b.read_matrix(); MatOps obj; Matrix result = obj.mul_matrix(a, b); for(int i = 0; i < SIZE; i++){ for(int j = 0; j < SIZE; j++){ cout<<result.matrix[i][j]<<" "; } cout<<endl; } return 0; }//main() ends
Cheers.
PEACE !
![]() |
Similar Threads
- matrix multiplication in c++ (C++)
- Please Help (matrix multiplication with mutlit-threading) (C++)
- (Matrix Multiplication with Multi-threading) (C++)
- Matrix Multiplication using Multi-Dimensional Arrays (C++)
- Matrix Multiplication (sorry) (C)
- Matrix - Matrix multiplication (C++)
- Pascal - Matrix Chain Multiplication (Pascal and Delphi)
- Matrix multiplication (Perl)
Other Threads in the C++ Forum
- Previous Thread: Internet Provider Switch Program Help
- Next Thread: Creating an array of linked lists -- seg fault :'(
| Thread Tools | Search this Thread |
api array arrays based beginner binary bitmap c++ c/c++ calculator char char* class classes coding compile compiler console conversion convert count data database delete desktop developer directshow dll dynamiccharacterarray email encryption error file forms fstream function functions game generator getline google graph homeworkhelper iamthwee ifstream input int integer java lib linkedlist linux list loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference return rpg sorting string strings struct template templates text tree unix url vector video visualstudio win32 windows winsock word wordfrequency wxwidgets






