Thread: Trouble outputting matrix

  1. #1
    Shadow12345
    Guest

    Trouble outputting matrix

    I have constructed a program that does the following:
    -has one function that adds together two 3x3 matricies and returns a matrix3x3_t pointer.
    -main creates two matricies and initializes all their values to 10.
    -main stores the result of adding the two matricies together in a matrix called resultMatrix;
    -main then attempts to output each value in the resultMatrix

    The line that I have put all the '*'s is what is causing my problem
    Code:
    #include <iostream>
    #include <conio.h>
    
    using namespace std;
    
    typedef struct {
    	int mat[3][3];
    }	matrix3x3_t;
    
    
    //This prototype SHOULD be set up to return a matrix3x3_t pointer
    //	but I am not sure if I set it up correctly
    matrix3x3_t * MatrixAdd(matrix3x3_t *matrixA, matrix3x3_t *matrixB, matrix3x3_t *resultMatrix) {	
    	for(int row = 0; row < 3; row++) {
    		for(int col = 0; col < 3; col++){
    			resultMatrix->mat[row][col] = matrixA->mat[row][col] + matrixB->mat[row][col];
    		}
    	}
    	return resultMatrix;
    }
    
    int main(void) {
    
    matrix3x3_t Matrix1[3][3] = { 10, 10, 10,10, 10, 10,10, 10, 10 };	
    
    matrix3x3_t Matrix2[3][3] = { 10, 10, 10,10, 10, 10,10, 10, 10 };	
    
    	matrix3x3_t resultMatrix[3][3];
    	
    	resultMatrix[3][3]  = 	*MatrixAdd(&Matrix1[3][3], &Matrix2[3][3], &resultMatrix[3][3]);
    
    int OutPut;	//asign values to this
    
    	for(int row = 0; row < 3; row++) {		//while not finished going through the rows
    		for(int col = 0; col < 3; col++) {		//while not finished going through the columns
    			OutPut = 0;	//put to zero before doing anything with it, just to be safe I guess
    ************OutPut = resultMatrix[row][col];***********
    			if(col == 3)
    				cout << OutPut << endl;
    			else
    				cout << OutPut;
    		}//col loop
    	}//row loop
    
    
    cout << "Please don't hit a key, I'm serious, ACK!" << endl;
    getch();
    return 0;
    }
    I get the following compile time error:
    [error]
    Compiling...
    matricies.cpp
    C:\matricies.cpp(41) : error C2679: binary '=' : no operator defined which takes a right-hand operand of type 'matrix3x3_t' (or there is no acceptable conversion)
    Error executing cl.exe.

    matricies.exe - 1 error(s), 0 warning(s)
    [/error]


    I have probably done something simple wrong, but I don't know what! pLEASe hElP ME!¿¿â*

    Also, I tried aligning the numbers to look like a matrix when I created matrix1 and matrix2, but they just spread a few lines apart and I can't get them to line up the way I want them to.
    Last edited by Shadow12345; 08-02-2002 at 01:15 PM.

  2. #2
    Registered User
    Join Date
    Jul 2002
    Posts
    52
    I think this may be your problem
    Code:
    resultMatrix[3][3]  = *MatrixAdd(&Matrix1[3][3], &Matrix2[3][3], &resultMatrix[3][3]);
    I suppose you are trying to add matrix1 and matrix2, but the compiler thinks you are trying to set the element in row 3, column 3, to the pointer returned by MatrixAdd. Maybe try this?
    Code:
    resultMatrix= *MatrixAdd(&Matrix1, &Matrix2, &resultMatrix);
    Hope that helps
    Turn no more aside and brood
    Upon love's bitter mysteries
    For Fergus rules the brazen cars...

  3. #3
    Unregistered
    Guest
    by using a typedef to declare your struct the compiler is not able to supply a default assignment operator. therefore the compiler generates an error when you try to assign the return value of the function to another matrix3x3_t. Use a C++ style struct for the declaration and try again.

    I assume you realize this could be done much easier without structs or pointers to structs in the first place, and that you are doing this as a self instruction project on the use of pointers to structs? If not, just use the plain 2d arrays. Pointers are powerful, but can be a big waste of (programmer) energy, too.


    if you really want 9 instances of type matrix3x3_t to be placed in resultMatrix as a 3 x 3 matrix (that is, resultMatrix is a matrix of matrixes), then the following is appropriate. I'm not sure that's what you want however.

    matrix3x3_t resultMatrix[3][3];

    in the body of the for loops col will never equal 3.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    How about
    Code:
    #include <iostream>
    #include <conio.h>
    
    using namespace std;
    
    typedef struct {
        int mat[3][3];
    }   matrix3x3_t;
    
    matrix3x3_t MatrixAdd ( matrix3x3_t *matrixA, matrix3x3_t *matrixB ) {
        matrix3x3_t resultMatrix;
        for(int row = 0; row < 3; row++) {
            for(int col = 0; col < 3; col++){
                resultMatrix.mat[row][col] = matrixA->mat[row][col] + matrixB->mat[row][col];
            }
        }
        return resultMatrix;
    }
    
    int main(void) {
        matrix3x3_t Matrix1 = { { {10, 10, 10},{10, 10, 10},{10, 10, 10} } };   
        matrix3x3_t Matrix2 = { { {10, 10, 10},{10, 10, 10},{10, 10, 10} } };   
        matrix3x3_t resultMatrix;
    
        resultMatrix = MatrixAdd ( &Matrix1, &Matrix2 );
    
        for(int row = 0; row < 3; row++) {
            for(int col = 0; col < 3; col++) {
                cout << resultMatrix.mat[row][col] << " ";
            }
            cout << endl;
        }
    
        cout << "Please don't hit a key, I'm serious, ACK!" << endl;
        getch();
        return 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C - access violation
    By uber in forum C Programming
    Replies: 2
    Last Post: 07-08-2009, 01:30 PM
  2. Matrix Help
    By HelpmeMark in forum C++ Programming
    Replies: 27
    Last Post: 03-06-2008, 05:57 PM
  3. What is a matrix's purpose in OpenGL
    By jimboob in forum Game Programming
    Replies: 5
    Last Post: 11-14-2004, 12:19 AM
  4. Matrix and vector operations on computers
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 11
    Last Post: 05-11-2004, 06:36 AM