user297850
user297850

Reputation: 8015

pass and return multi-dimensional arrays from a function in C++

I am trying to write a function that takes two matrices as parameters and return the sum of these two matrices.

The following is the code, which fails to pass the compiler. I think the implementation of function is wrong, how to correct it?

#include <iostream>

int **func(int **array1, int **array2, int n){
    a1 = new int[n][n];
    for (int i = 0; i<n; i++){
     for (int j = 0; j<n; j++)
     a1[i][j]=array1[i][j]+array2[i][j];
   }
   return a1;
  }

 int main( ){

 int m1[2][2];
 int m2[2][2];

  m1[0][0]=1; m1[0][1]=2; m1[1][0]=3; m1[1][1]=4;
  m2[0][0]=1; m2[0][1]=2; m2[1][0]=3; m2[1][1]=4;

 int **m3;
  m3 = func(m1,m2,2);

 return 0;
 }

Upvotes: 1

Views: 4146

Answers (7)

SzymonS
SzymonS

Reputation: 93

#include <iostream>

using namespace std;
int** Create2DMatrix(int);
int** AddMatrix(int**&,int**&,int);

void DeleteMatrix(int**&);


 int main( )
 {
     int myDimension=4; // for example
     int i,j;

     int** myFirstMatrix=Create2DMatrix(myDimension);
     int** mySecondMatrix=Create2DMatrix(myDimension);


     // for example without function *


     for (i=0;i<myDimension;i++)
     {
         for (j=0;j<myDimension;j++)
         {
             // for example
             myFirstMatrix[i][j]=i+j;
             mySecondMatrix[i][j]=2*i+7*j-16;
         }
     }

     // show matrix 1
     for (i=0;i<myDimension;i++)
     {
         for (j=0;j<myDimension;j++)
         {
             cout<< myFirstMatrix[i][j] << " ";
         }
         cout << endl;
     }
     cout << endl;

     // show matrix 2
     for (i=0;i<myDimension;i++)
     {
         for (j=0;j<myDimension;j++)
         {
             cout<< mySecondMatrix[i][j] << " ";
         }
         cout << endl;
     }

     int** finalMatrix=AddMatrix(myFirstMatrix,mySecondMatrix,myDimension);

     // show sum of matrix
     cout << endl;
     for (i=0;i<myDimension;i++)
     {
         for (j=0;j<myDimension;j++)
         {
             cout<< finalMatrix[i][j] << " ";
         }
         cout << endl;
     }

     DeleteMatrix(myFirstMatrix);
     DeleteMatrix(mySecondMatrix);
     DeleteMatrix(finalMatrix);




    return 0;
 }

 int** Create2DMatrix(int dim)
 {
     int** myMatrix=new int*[dim];
     int* linearTable=new int[dim*dim];
     for (int i=0;i<dim;i++)
     {
         myMatrix[i]=linearTable+i*dim;
     }
     return myMatrix;
 }

 void DeleteMatrix(int**& MatrixToDel)
 {
     delete [] MatrixToDel[0];
     delete [] MatrixToDel;
     MatrixToDel=0;
 }

 int** AddMatrix(int**& MatrixOne,int**& MatrixTwo,int dimension)
 {
     int** sumMatrix=Create2DMatrix(dimension);
     for (int i=0;i<dimension;i++)
     {
         for (int j=0;j<dimension;j++)
         {
             // for example
             sumMatrix[i][j]=MatrixOne[i][j]+MatrixTwo[i][j];
         }
     }
     return sumMatrix;
 }

Upvotes: 0

Mahesh
Mahesh

Reputation: 34645

int **func(int **array1, int **array2, int n) ; 

To the above prototype, program cannot pass two dimensional arrays( i.e., [][]) because two dimensional array decays to a pointer to one dimensional array. So, the prototype should be -

int** func( int array1[][2], int array2[][2], int n );

Edit: With the above correction made , program needs to allocate memory for 2D array as @brado86 suggested. But, program also needs to deallocate the resources acquired by new, else memory leak prevails.

int main( ){

   // .......

   int **m3;
   m3 = func(m1,m2,2);      // m3 is pointing to the resources acquired in func(..)
                            // So, the resources acquired should be returned to
                            // free store. 

   for( int i=0; i<2; ++i )
       delete[] m3[i] ;

   delete[] m3 ;

   return 0;

}

Learn to use std::vector, that does this deallocation for process by default. And the two dimensional array can be represented as vector of vector of ints. (i.e., vector<vector<int>> twoDimensionalArray; )

Upvotes: 2

Andriy Tylychko
Andriy Tylychko

Reputation: 16276

as soon as this is C++, use std::vector<std::vector<int> > or boost::array<boost::array<int, 2>, 2 >. their usage is trivial and safer. and you still can learn from @Mahesh's answer how to do this with raw arrays.

Upvotes: 2

Alex Reche Martinez
Alex Reche Martinez

Reputation: 966

You can not create a matrix with dynamic size like this. It's ok for your first two matrices because they are statically compiled, but the one you create in the function must be created dynamically that way:

int ** a1 = new int*[n];
for (int i=0; i<n; ++i) a1[i] = new int[n];

Upvotes: 0

Puppy
Puppy

Reputation: 147028

An int[] will decay to an int*, but an int[][] will NOT decay to an int**. Use a class which contains the array and take a reference to it.

Upvotes: 1

brado86
brado86

Reputation: 174

The line

a1 = new int[n][n];

will not work.

Instead, allocating 2D C++ arrays goes like this:

int **a1;
a1 = new (int *)[n];
for (int i = 0; i < n; i++) {
    a1[i] = new int[n];
}

Upvotes: 0

zyndor
zyndor

Reputation: 1478

The first line of your function: you need to specify the type of a1

int **a1 = new int[n][n];

Upvotes: 0

Related Questions