have some problem in C++ matrix

Edge89's Avatar, Join Date: May 2008
Newbie Member
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!
Code:
#include <iostream>
#include <ctime>

using namespace std;

typedef struct{
	int x[3][3];
	
}Matrix3X3;

//prototype
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;

		// initialise the matrix
		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;
			}
		}

        // print out the matrix A
		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;
	}
         // print out the matrix B   
		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);
//multiplyMatrices (A,B);
transposeMatrix (A);

	cin.get();
	}
	Matrix3X3 transposeMatrix (Matrix3X3 A){
		Matrix3X3 answer;
		//transpose the matrix
		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;
		//before multipllication process, try to check against the 2 matrices are defined
		//or not (use isDefined method        
	
	
	return answer;
	}
	
	Matrix3X3 subtractMatrices (Matrix3X3 A, Matrix3X3 B){
		Matrix3X3 answer;
        //before subtraction process, try to check against the dimension
		
		   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){
		
		//make sure the first matrix row is column to seceond matrix row, then 2 matrices only
        // considered multipilicable
return true;
}
bool isSameDimension (Matrix3X3 A, Matrix3X3 B)
{
	    
    //first check the dimension
	//if deimension is not equal then return false else proceed
return true;
	} 
	//function body
bool isEqual (Matrix3X3 A, Matrix3X3 B){
		if (isSameDimension(A,B)){
	
	//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.
		}
		else{
			cout<<"The matrix dimension is not equal"<<endl;
			return false;
		}
		return true;
	}

Last edited by shabbir; 7May2008 at 18:01.. Reason: Code block
0
mr.anandc's Avatar, Join Date: May 2008
Go4Expert Member
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;
}

Last edited by shabbir; 7May2008 at 18:02.. Reason: Code block
0
Edge89's Avatar, Join Date: May 2008
Newbie Member
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
0
mr.anandc's Avatar, Join Date: May 2008
Go4Expert Member
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.
0
Edge89's Avatar, Join Date: May 2008
Newbie Member
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!