CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Apr 2008
    Posts
    25

    Need help with Matrix codes.

    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.

    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 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
    Last edited by shadachi; April 27th, 2008 at 03:01 PM.

  2. #2
    Join Date
    Dec 2006
    Posts
    166

    Re: Need help with Matrix codes.

    Quote Originally Posted by shadachi
    the multiplication function is not working well.
    did you expect elements of arrays to start at 0?

  3. #3
    Join Date
    Apr 2008
    Posts
    25

    Re: Need help with Matrix codes.

    of course . [0][0] .. wrong?? is there any suggestions can be made to make the code shorter and better?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured