Thread: Returning a double matrix

  1. #1
    Registered User
    Join Date
    Mar 2012
    Posts
    22

    Returning a double matrix

    Hi there,

    Do you know how to make **myMatrix a double pointer? This is what I have done considering it an int pointer...

    Code:
    int **Matrix(int (*matrixSize)[2])
    {
        (*matrixSize)[0]=3; // number of rows.
        (*matrixSize)[1]=5; // number of cols.
    // These numbers are obtained from another function. 
    // Not shown here, for the sake of simplicity.
    // They are supposed to be 3 and 5, respectively.
        
        
    // Allocating myMatrix (a two-dimensional array)...
        int **myMatrix;
        
        myMatrix=malloc((*matrixSize)[0]*sizeof(int*));
        
        if (myMatrix==NULL)
        {
    fprintf(stderr, "Error: Out of memory\n");
            exit(-1);
        }
        
        for (int i=0; i<(*matrixSize)[0]; i++)
        {
            myMatrix[i]=malloc((*matrixSize)[1]*sizeof(int));
            
            if (myMatrix==NULL)
            {
    fprintf(stderr, "Error: Out of memory\n");
                exit(-1);
            }
        }
        
    // Filling myMatrix with numbers...
        for (int i=0; i<(*matrixSize)[0]; i++)
            for (int j=0; j<(*matrixSize)[1]; j++)
                myMatrix[i][j]=5*i-2*j;
        
        return &(myMatrix[0]);
    }
    
    
    
    
    int main (int argc, const char * argv[])
    {
            int msize[2];
            
            int **m=Matrix(&msize);
            
            for (int i=0; i<msize[0]; i++)
                for (int j=0; j<msize[1]; j++)
                    printf("m[%i][%i] = %i\n",i,j,m[i][j]);
        
        return 0;
    }
    Thank you very much for your kind of help.

    Ricardo.

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    What have you tried? Does your int function work properly?

    Jim

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    int **Matrix(int (*matrixSize)[2])
    {
        (*matrixSize)[0]=3; // number of rows.
        (*matrixSize)[1]=5; // number of cols.
    // These numbers are obtained from another function. 
    // Not shown here, for the sake of simplicity.
    // They are supposed to be 3 and 5, respectively.
    Why don't you save yourself the trouble and just pass two integers, or an array of two integers? Why are you passing a pointer to an array of two integers?
    Code:
    // Allocating myMatrix (a two-dimensional array)...
        int **myMatrix;
        ...
        return &(myMatrix[0]);
    }
    Just do:
    Code:
    return myMatrix;
    This:
    Code:
        for (int i=0; i<(*matrixSize)[0]; i++)
        {
            myMatrix[i]=malloc((*matrixSize)[1]*sizeof(int));
            
            if (myMatrix==NULL)
            {
    fprintf(stderr, "Error: Out of memory\n");
                exit(-1);
            }
        }
    Should actually be:
    Code:
    if( myMagrix[ i ] == NULL )
        ...
    You want to check the row you've allocated, not the base pointer. The base pointer will never be NULL here.


    Quzah.
    Hope is the first step on the road to disappointment.

  4. #4
    Registered User
    Join Date
    Mar 2012
    Posts
    22
    Dear quzah. Do you mean this?

    Code:
    int **Matrix(int (*matrixSize)[2]);
    int **Matrix(int (*matrixSize)[2])
    {
    int numRows=3; // number of rows.
    int numCols=5; // number of cols.
    // These numbers are obtained from another function. 
    // Not shown here, for the sake of simplicity.
    // They are supposed to be 3 and 5, respectively.
        
        (*matrixSize)[0]=numRows;
        (*matrixSize)[1]=numCols;
        
    // Allocating myMatrix (a two-dimensional array)...
        int **myMatrix;
        
        myMatrix=malloc(numRows*sizeof(int*));
        
        if (myMatrix==NULL)
        {
    fprintf(stderr, "Error: Out of memory\n");
            exit(-1);
        }
        
        for (int i=0; i<numRows; i++)
        {
            myMatrix[i]=malloc(numCols*sizeof(int));
            
            if (myMatrix[i]==NULL)
            {
    fprintf(stderr, "Error: Out of memory\n");
                exit(-1);
            }
        }
        
    // Filling myMatrix with numbers...
        for (int i=0; i<numRows; i++)
            for (int j=0; j<numCols; j++)
                myMatrix[i][j]=5*i-2*j;
        
        return myMatrix;
    }
    
    
    
    
    int main (int argc, const char * argv[])
    {
        //@autoreleasepool {
            int msize[2];
            
            int **m=Matrix(&msize);
            
            for (int i=0; i<msize[0]; i++)
                for (int j=0; j<msize[1]; j++)
                    printf("m[%i][%i] = %i\n",i,j,m[i][j]);
        //}
        
        return 0;
    }
    What about making **myMatrix a double pointer?

    Thank you!

  5. #5
    Registered User
    Join Date
    Mar 2012
    Posts
    22
    Ok... I did it! Thank you Quzah.

  6. #6
    Registered User
    Join Date
    Mar 2012
    Posts
    22
    Another question... to free memory, I need to do this, isn't it?

    Code:
    for (int i=0; i<msize[0]; i++) 
         free(m[i]);
    
    
    free(m);
    I mean,


    Code:
    #import <Foundation/Foundation.h>
    
    
    double **Matrix(int (*matrixSize)[2]);
    double **Matrix(int (*matrixSize)[2])
    {
        int numRows=3; // number of rows.
        int numCols=5; // number of cols.
        // These numbers are obtained from another function. 
        // Not shown here, for the sake of simplicity.
        // They are supposed to be 3 and 5, respectively.
        
        (*matrixSize)[0]=numRows;
        (*matrixSize)[1]=numCols;
        
        // Allocating myMatrix (a two-dimensional array)...
        double **myMatrix;
        
        myMatrix=malloc(numRows*sizeof(double*));
        
        if (myMatrix==NULL)
        {
            fprintf(stderr, "Error: Out of memory\n");
            exit(-1);
        }
        
        for (int i=0; i<numRows; i++)
        {
            myMatrix[i]=malloc(numCols*sizeof(double));
            
            if (myMatrix[i]==NULL)
            {
                fprintf(stderr, "Error: Out of memory\n");
                exit(-1);
            }
        }
        
        // Filling myMatrix with numbers...
        for (int i=0; i<numRows; i++)
            for (int j=0; j<numCols; j++)
                myMatrix[i][j]=1/9.*i-2.*j;
        
        return myMatrix;
    }
    
    
    
    
    int main (int argc, const char * argv[])
    {
        @autoreleasepool {
            int msize[2];
            
            double **m=Matrix(&msize);
            
            for (int i=0; i<msize[0]; i++)
                for (int j=0; j<msize[1]; j++)
                    printf("m[%i][%i] = %f\n",i,j,m[i][j]);
            
            // Realeasing memory...
            for (int i=0; i<msize[0]; i++) 
                free(m[i]);
            
            free(m);
        }
        
        return 0;
    }

    Thank you!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Returning a Matrix
    By Shamino in forum C++ Programming
    Replies: 14
    Last Post: 02-25-2006, 01:36 AM
  2. matrix mult - double ptr in col major
    By collymitch in forum C Programming
    Replies: 13
    Last Post: 03-30-2005, 02:07 PM
  3. Replies: 8
    Last Post: 03-10-2005, 08:14 AM
  4. returning a double array [][]
    By Morrow in forum C++ Programming
    Replies: 4
    Last Post: 09-29-2004, 10:01 AM
  5. double matrix and data structure?
    By Tombear in forum C Programming
    Replies: 1
    Last Post: 10-29-2001, 08:10 AM