Click to See Complete Forum and Search --> : Need help with Matrix codes.


shadachi
April 27th, 2008, 02:58 PM
I've written a Matrix code.And the thing is i can't some functions right and how to make it shorter. the multiplication function is not working well.


#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 transposeMatrices(Matrix3X3 A,Matrix3X3 B);


void main()
{
Matrix3X3 A;
Matrix3X3 B;

//initialize 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 <<" This is Matrix a"<< endl;
for (int i =0; i<3;i++)
{
for(int j=0; j<3;j++)
{
cout <<A.x[i][j] << "\t" ;

} cout << endl;
}

//print out the matrix b
cout <<" \nThis is Matrix b"<< endl;
for (int i =0; i<3;i++)
{
for(int j=0; j<3;j++)
{

cout << B.x[i][j] << "\t " ;

} cout << endl;
}

cout << "\nThis is Matrix A + B" << endl;
addMatrices(A,B);

cout <<"\n This is Matrix A - B" << endl;
subtractMatrices(A,B);

cout <<"\n This is Matrix A *B "<< endl;
multiplyMatrices(A,B);

cout <<"\n This is Transpose Matrix "<< endl;
transposeMatrices( A,B);

cout << "\n Testing equal" << endl;
isEqual(A,B);

cin.get();
}// end of main


bool isSameDimension(Matrix3X3 A, Matrix3X3 B)

{
//first check the dimension
//if dimension is not equal then return false else proceed
//to missing code 2

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

for(int j=0;j<3;j++)

{
if(( A.x[i][j]) != (B.x[i][j]))

errorFlag = 1;
}
}

//test for an error in equality.
if(errorFlag == 1)
{
return false;
}

else
{
return true;
}
}

bool isEqual(Matrix3X3 A, Matrix3X3 B)
{
int errorFlag = 0;

if (isSameDimension(A,B)== true){

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

for(int j=0;j<3;j++)

{
if(( A.x[i][j]) != (B.x[i][j]))

errorFlag = 1;
}
}
}
//test for an error in equality.
if(errorFlag == 1)
{
cout << " the Matrix dimension is not equal" << endl;
return false;
}
else
{
cout << " the Matrix dimension is equal" << endl;
return true;
}



}


bool isDefined(Matrix3X3 A, Matrix3X3 B)
{

//make sure the first matrix column is equal
//to the second matrix row then 2 matrices only
//considered multipliable
return true;
}


Matrix3X3 addMatrices(Matrix3X3 A,Matrix3X3 B)
{
Matrix3X3 answer;
//before addition process , try to check aginst 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]);
}
}



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 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]);
}
}


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 ; //createFixed3X3Matrix(0);
for(int i = 0;i<3;i++)
{
for(int j=0;j<3;j++)
{
for(int k=0;k<3;k++)
{
answer.x[i][j] += (A.x[i][k] * B.x[k][j]);
}
}
}

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 transposeMatrices(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[j][i];
}
}

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

{
cout <<answer.x[i][j] << "\t" ;
} cout <<endl;
}


return answer;
}



I've thought of making another function to retrieve all the other answers from the other function by using one function called answerz but i don't know how to apply it

spoon!
April 27th, 2008, 08:46 PM
the multiplication function is not working well.
did you expect elements of arrays to start at 0?

shadachi
April 28th, 2008, 12:10 AM
of course . [0][0] .. wrong?? is there any suggestions can be made to make the code shorter and better?