Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C++ (http://www.go4expert.com/forums/cpp/)
-   -   have some problem in C++ matrix (http://www.go4expert.com/forums/cpp-matrix-t10421/)

Edge89 6May2008 23:10

have some problem in C++ matrix
 
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;

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


mr.anandc 7May2008 17:01

Re: have some problem in C++ matrix
 
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;
}

:)

Edge89 7May2008 17:20

Re: have some problem in C++ matrix
 
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

mr.anandc 7May2008 18:02

Re: have some problem in C++ matrix
 
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.

Edge89 7May2008 18:11

Re: have some problem in C++ matrix
 
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!


All times are GMT +5.5. The time now is 15:19.