# have some problem in C++ matrix

Discussion in 'C++' started by Edge89, May 6, 2008.

1. ### Edge89New Member

Joined:
May 6, 2008
Messages:
3
0
Trophy Points:
0
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;
}
subtractMatrices (A,B);
[COLOR=Green]//multiplyMatrices (A,B);[/COLOR]
transposeMatrix (A);

cin.get();
}
Matrix3X3 transposeMatrix (Matrix3X3 A){
[COLOR=Green]//transpose the matrix[/COLOR]
for(int i = 0;i<3;i++) {
for(int j=0;j<3;j++) {
}
}
cout<<"\nThe Transpose Results:"<<endl;
for(int i = 0;i<3;i++){
for(int j=0;j<3;j++){
} cout <<endl;
}
}
Matrix3X3 multiplyMatrices (Matrix3X3 A, Matrix3X3 B){
[COLOR=Green]		//before multipllication process, try to check against the 2 matrices are defined
//or not (use isDefined method

[/COLOR]
}

Matrix3X3 subtractMatrices (Matrix3X3 A, Matrix3X3 B){
[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++){
}
}
cout<<"\nThe Subtraction Results:"<<endl;
for(int i = 0;i<3;i++){
for(int j=0;j<3;j++){
} cout <<endl;
}

Matrix3X3 addMatrices (Matrix3X3 A, Matrix3X3 B){

for(int i = 0;i<3;i++){
for(int j=0;j<3;j++){
}

}

for(int i = 0;i<3;i++){
for(int j=0;j<3;j++){
} cout <<endl;
}

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

Last edited by a moderator: May 7, 2008
2. ### mr.anandcNew Member

Joined:
May 6, 2008
Messages:
18
0
Trophy Points:
0
Multiplication function could be.....

Matrix3X3 multiplyMatrices (Matrix3X3 A, Matrix3X3 B){
Code:
```for(int i = 0;i<3;i++){
for(int j=0;j<3;j++){
for (int k = 0; k<3; k++){
}
}
}

cout<<"\nThe Multiplication Results:"<<endl;
for(int i = 0;i<3;i++){
for(int j=0;j<3;j++){
} cout <<endl;
} ```

Last edited by a moderator: May 7, 2008
3. ### Edge89New Member

Joined:
May 6, 2008
Messages:
3
0
Trophy Points:
0
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

4. ### mr.anandcNew Member

Joined:
May 6, 2008
Messages:
18
0
Trophy Points:
0
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.

Joined:
May 6, 2008
Messages:
3