I have been trying to return a two dimensional array in a function but it is getting me no where, please tell me how to do it. the code is as follow :

int *population();
int *testCase::population()
		{
				

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

matrix[i][j]=rand()%2;			 

					
							}
	}
		
			
	return matrix[0][0];
  
			
}

when I run the program, it returns a memory address. I want it to return the value for the array.

Regards

You must read this for pointers-http://www.eternallyconfuzzled.com

I think that you might be unable to access the value stored in the returned memory address , so you could point to that memory address to get the value for getting the things right :)

At the site you must check out the Languages section of the index.

... when I run the program, it returns a memory address. I want it to return the value for the array.

array names 'decay' into pointers (addresses); this is fundamental to their use in C and C++. to return an array by value, you could either wrap a structure around the array and return the structure ( ok for pod types, you know the size at compile time) or return std library containers (which support value semantics).

#include <cstddef>
#include <vector>

template< typename T, size_t M, size_t N > struct array2d
{
  T a[M][N] ; // T must have a default constructor
};

array2d<int,10,5> foo()
{
  array2d<int,10,5> array ;
  for( size_t i=0 ; i<10 ; ++i )
    for( size_t j=0 ; j<5 ; ++j )
      array.a[i][j] = i*j ;
  return array ;
}

std::vector< std::vector<int> > bar( size_t M, size_t N )
{
  std::vector< std::vector<int> > array( M, std::vector<int>(N) ) ;
  for( size_t i=0 ; i<M ; ++i )
    for( size_t j=0 ; j<N ; ++j )
      array[i][j] = i*j ;
  return array ;
};

caveat: returning large objects by value can be inefficient

I have been trying to return a two dimensional array in a function but it is getting me no where, please tell me how to do it. the code is as follow :

int *population();
int *testCase::population()
		{
				

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

matrix[i][j]=rand()%2;			 

					
							}
	}
		
			
	return matrix[0][0];
  
			
}

when I run the program, it returns a memory address. I want it to return the value for the array.

Then ditch the asterisk which causes your function to return a pointer value. If you want the value stored in the array, then just return a plain int.

The problem is, It only returns the value for matrix[0][0]. I want the whole array to be returned. I just don't know how to do it.

Could you please tell me what does this statement does
matrix[j]=rand()%2;

check your loop working

Besides you are yourself returning matrix[0][0]; so please try with a[j] in the j's loop

Regards,
hinduengg

I want it to return the value for the array.

Why? The only thing I can think of is that you want a copy of the array instead of something that changes the array owned by the testCase class. If that's the case, you should still return the address of the array because that's how arrays work, but you still have to make a new array.

int **testCase::population() {
  // Make the first dimension
  int **copy = new int*[3];

  for ( int i = 0; i < 3; ++i ) {
    // Make the second dimension
    copy[i] = new int[2];

    for ( int j = 0; j < 2; ++j ) {
      copy[i][j] = matrix[i][j] = rand() % 2;
    }
  }

  return copy;
}

That returns the dynamic array by pointer, but it's still a copy of the original array. You have to remember to free the memory so that's not a good way to do it. A better way is a vector object.

vector<vector<int> > testCase::population() {
  vector<vector<int> > copy = vector( 3, vector<int>( 2, int ) );

  for ( int i = 0; i < 3; ++i ) {
    for ( int j = 0; j < 2; ++j ) {
      copy[i][j] = matrix[i][j] = rand() % 2;
    }
  }

  return copy;
}

If you don't want a copy, why do you want to return the array by value?

commented: Right advice +3

Could you please tell me what does this statement does
matrix[j]=rand()%2;

check your loop working

Besides you are yourself returning matrix[0][0]; so please try with a[j] in the j's loop


matrix[j]=rand()%2; this will randomly allocate values to the array . I did try returning matrix[j] , but it did not work, I'm confused

The best solution, is the one which hamrick describes, avoiding arrays altogether and using an STL vector

The problem with 'returning an array' is that arrays are returned by pointer. Which means one of the following must happen

- The array is declared static (Similar to a global object)
- The array is dynamically created inside the function with new
- The array is passed into the function as a parameter

If the array is passed into the function as a parameter, there's probably no need to return it

If the array is dynamically created with new, then you have all sorts of problems ahead, with regards to keeping track of the memory and ensuring it gets properly delete[] 'd

If the array is declared static, you can return it by reference

int ( &my_func() )[4][5]
{
    static int arr[4][5];
    return arr;
}

Note, that this is essentially the same as having a global variable wrapped in a function

The alternative, that someone else suggested, is to wrap it in a struct, and pass the struct around. (which causes copies to be made)


However, it would be better if you described what exactly you are trying to achieve, and where it fits into your program. returning an array isn't a "normal" activity, so you could probably do something differently elsewhere, without needing a function returning an array.

I need to pass a matrix :

1 2 3 4
____________
1- 0 1 1 0
2- 1 0 0 0
3- 0 1 1 1

something like above. Could anyone please tell me how to do it. I am fairly new to c++ and have a bloody project deadline in three weeks time (:

Many thanks

If it's an array, you pass it just like you declare it.

void function( int matrix[3][4] ) {
  // ...
}

i can pass it, but I cannot return it, I need to return the whole array in a function say :

return matrix[j]

I can get only one value , return matrix[0][0]
I don't know how to return the whole array

Pass means to go into the function, return means to come out. And I'm pretty sure I answered your returning question earlier... How was it not good enough?

I did it ur way, but I get only the first value in the array. not the full array

This is what I do in the main

testCase *Newcase[10]

for(int i=0;i<10;i++)
{
cout<<NewCase->population()<<endl;
}

when I use your method, I get one memory address.

If you're passing it, why do you need to return it?

Arrays are already passed by pointer (by reference), so any modification which occurs in the function will directly affect the array passed into the function - no copies are made.

I need to swap values of one matrix with another matrix. I am working on a Genetic algorithm. It is killing me man

So pass two arrays to the function

void function( const int matrix[3][4], int result[3][4] )

So rather than trying to do something like result = function(mymat); you do function(mymat,result);

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.