i would like to make a code for matrix. the value of both matrix is random and the addition, subtraction,multiply and transpose function. i have some problem in the multiply fucntion, can someone help ?? thank! :p Code: #include <iostream> #include <ctime> using namespace std; typedef struct{ int x[3][3]; }Matrix3X3; [COLOR=Green]//prototype[/COLOR] bool isEqual (Matrix3X3 A, Matrix3X3 B); bool isSameDimension (Matrix3X3 A, Matrix3X3 B); bool isDefined (Matrix3X3 A, Matrix3X3 B); Matrix3X3 addMatrices (Matrix3X3 A, Matrix3X3 B); Matrix3X3 subtractMatrices (Matrix3X3 A, Matrix3X3 B); Matrix3X3 multiplyMatrices (Matrix3X3 A, Matrix3X3 B); Matrix3X3 transposeMatrix (Matrix3X3 A); void main(){ Matrix3X3 A; Matrix3X3 B; [COLOR=Green]// initialise the matrix[/COLOR] for (int i=0;i<3;i++){ for (int j=0;j<3;j++){ A.x[i][j] = (rand()*time(0))% 100; B.x[i][j] = (rand()*time(0))% 100; } } [COLOR=Green] // print out the matrix A[/COLOR] cout<<"Matrix A"<<endl; for (int i=0;i<3;i++){ for (int j=0;j<3;j++){ cout<<A.x[i][j]<< " "; } cout<<endl; } [COLOR=Green] // print out the matrix B [/COLOR] cout<<"\nMatrix B"<<endl; for (int i=0;i<3;i++){ for (int j=0;j<3;j++){ cout<<B.x[i][j]<< " "; } cout<<endl; } addMatrices (A,B); subtractMatrices (A,B); [COLOR=Green]//multiplyMatrices (A,B);[/COLOR] transposeMatrix (A); cin.get(); } Matrix3X3 transposeMatrix (Matrix3X3 A){ Matrix3X3 answer; [COLOR=Green]//transpose the matrix[/COLOR] for(int i = 0;i<3;i++) { for(int j=0;j<3;j++) { answer.x[i][j] = A.x[j][i]; } } cout<<"\nThe Transpose Results:"<<endl; for(int i = 0;i<3;i++){ for(int j=0;j<3;j++){ cout <<answer.x[i][j] << "\t" ; } cout <<endl; } return answer; } Matrix3X3 multiplyMatrices (Matrix3X3 A, Matrix3X3 B){ Matrix3X3 answer; [COLOR=Green] //before multipllication process, try to check against the 2 matrices are defined //or not (use isDefined method [/COLOR] return answer; } Matrix3X3 subtractMatrices (Matrix3X3 A, Matrix3X3 B){ Matrix3X3 answer; [COLOR=Green] //before subtraction process, try to check against the dimension[/COLOR] for(int i = 0;i<3;i++){ for(int j=0;j<3;j++){ answer.x[i][j] = (A.x[i][j] - B.x[i][j]); } } cout<<"\nThe Subtraction Results:"<<endl; for(int i = 0;i<3;i++){ for(int j=0;j<3;j++){ cout <<answer.x[i][j] << "\t" ; } cout <<endl; } return answer; } Matrix3X3 addMatrices (Matrix3X3 A, Matrix3X3 B){ Matrix3X3 answer; for(int i = 0;i<3;i++){ for(int j=0;j<3;j++){ answer.x[i][j] = (A.x[i][j] + B.x[i][j]); } } cout<<"\nThe Addition Results:"<<endl; for(int i = 0;i<3;i++){ for(int j=0;j<3;j++){ cout <<answer.x[i][j] << "\t" ; } cout <<endl; } return answer; } bool isDefined (Matrix3X3 A, Matrix3X3 B){ [COLOR=Green] //make sure the first matrix row is column to seceond matrix row, then 2 matrices only // considered multipilicable[/COLOR] return true; } bool isSameDimension (Matrix3X3 A, Matrix3X3 B) { [COLOR=Green] //first check the dimension //if deimension is not equal then return false else proceed[/COLOR] return true; } [COLOR=Green]//function body[/COLOR] bool isEqual (Matrix3X3 A, Matrix3X3 B){ if (isSameDimension(A,B)){ [COLOR=Green] //loop thru 2 matrices and check each coontent //if one of the entry is not equal then return false //if finish loop then return true.[/COLOR] } else{ cout<<"The matrix dimension is not equal"<<endl; return false; } return true; }

Multiplication function could be..... Matrix3X3 multiplyMatrices (Matrix3X3 A, Matrix3X3 B){ Matrix3X3 answer; Code: for(int i = 0;i<3;i++){ for(int j=0;j<3;j++){ answer.x[i][j] = 0; for (int k = 0; k<3; k++){ answer.x[i][j] += (A.x[i][k] * B.x[k][j]); } } } cout<<"\nThe Multiplication Results:"<<endl; for(int i = 0;i<3;i++){ for(int j=0;j<3;j++){ cout <<answer.x[i][j] << "\t" ; } cout <<endl; } return answer; }

Yeah...i know it. my problem is at below tat boolean function, it make my multiply function cant work.... i need some help at there. thx

Ok. Before multiplying matrices A and B, you want to check whether columns in matrix A matches with rows in matrix B in IsDefined function. But I think this is not required in your case because matrix size is hard-coded to [3][3] in which rows and colums are equal to 3.

Ya....my matrix size is setted [3][3] but i still need to write it out. i not so good in boolean and i am so confuse in those 3 boolean function..can you give me an example or tips? thx!