Please helpwith how to return two dimensional array

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Jul 2007
Posts: 18
Reputation: khalidxa is an unknown quantity at this point 
Solved Threads: 0
khalidxa khalidxa is offline Offline
Newbie Poster

Please helpwith how to return two dimensional array

 
0
  #1
Jul 31st, 2007
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 :

  1. int *population();

  1. int *testCase::population()
  2. {
  3.  
  4.  
  5. for(int i=0;i<3;i++)
  6. {
  7. for(int j=0;j<2;j++){
  8.  
  9. matrix[i][j]=rand()%2;
  10.  
  11.  
  12. }
  13. }
  14.  
  15.  
  16. return matrix[0][0];
  17.  
  18.  
  19. }

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

Regards
Reply With Quote Quick reply to this message  
Join Date: Jun 2007
Posts: 82
Reputation: hinduengg is an unknown quantity at this point 
Solved Threads: 3
hinduengg's Avatar
hinduengg hinduengg is offline Offline
Junior Poster in Training

Re: Please helpwith how to return two dimensional array

 
0
  #2
Aug 1st, 2007
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
Learning C++ is easy with Daniweb :)
Reply With Quote Quick reply to this message  
Join Date: Jun 2007
Posts: 82
Reputation: hinduengg is an unknown quantity at this point 
Solved Threads: 3
hinduengg's Avatar
hinduengg hinduengg is offline Offline
Junior Poster in Training

Re: Please helpwith how to return two dimensional array

 
0
  #3
Aug 1st, 2007
At the site you must check out the Languages section of the index.
Learning C++ is easy with Daniweb :)
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 1,089
Reputation: vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all 
Solved Threads: 164
vijayan121 vijayan121 is offline Offline
Veteran Poster

Re: Please helpwith how to return two dimensional array

 
0
  #4
Aug 1st, 2007
Originally Posted by khalidxa View Post
... 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).
  1. #include <cstddef>
  2. #include <vector>
  3.  
  4. template< typename T, size_t M, size_t N > struct array2d
  5. {
  6. T a[M][N] ; // T must have a default constructor
  7. };
  8.  
  9. array2d<int,10,5> foo()
  10. {
  11. array2d<int,10,5> array ;
  12. for( size_t i=0 ; i<10 ; ++i )
  13. for( size_t j=0 ; j<5 ; ++j )
  14. array.a[i][j] = i*j ;
  15. return array ;
  16. }
  17.  
  18. std::vector< std::vector<int> > bar( size_t M, size_t N )
  19. {
  20. std::vector< std::vector<int> > array( M, std::vector<int>(N) ) ;
  21. for( size_t i=0 ; i<M ; ++i )
  22. for( size_t j=0 ; j<N ; ++j )
  23. array[i][j] = i*j ;
  24. return array ;
  25. };
caveat: returning large objects by value can be inefficient
Last edited by vijayan121; Aug 1st, 2007 at 3:19 am.
Reply With Quote Quick reply to this message  
Join Date: Feb 2006
Posts: 490
Reputation: Bench has a spectacular aura about Bench has a spectacular aura about Bench has a spectacular aura about 
Solved Threads: 49
Bench's Avatar
Bench Bench is offline Offline
Posting Pro in Training

Re: Please helpwith how to return two dimensional array

 
0
  #5
Aug 1st, 2007
Originally Posted by khalidxa View Post
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 :

  1. int *population();

  1. int *testCase::population()
  2. {
  3.  
  4.  
  5. for(int i=0;i<3;i++)
  6. {
  7. for(int j=0;j<2;j++){
  8.  
  9. matrix[i][j]=rand()%2;
  10.  
  11.  
  12. }
  13. }
  14.  
  15.  
  16. return matrix[0][0];
  17.  
  18.  
  19. }

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.
¿umop apisdn upside down?
Reply With Quote Quick reply to this message  
Join Date: Jul 2007
Posts: 18
Reputation: khalidxa is an unknown quantity at this point 
Solved Threads: 0
khalidxa khalidxa is offline Offline
Newbie Poster

Re: Please helpwith how to return two dimensional array

 
0
  #6
Aug 1st, 2007
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.
Reply With Quote Quick reply to this message  
Join Date: Jun 2007
Posts: 82
Reputation: hinduengg is an unknown quantity at this point 
Solved Threads: 3
hinduengg's Avatar
hinduengg hinduengg is offline Offline
Junior Poster in Training

Re: Please helpwith how to return two dimensional array

 
0
  #7
Aug 1st, 2007
Could you please tell me what does this statement does
matrix[i][j]=rand()%2;

check your loop working

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

Regards,
hinduengg
Last edited by hinduengg; Aug 1st, 2007 at 9:39 am.
Learning C++ is easy with Daniweb :)
Reply With Quote Quick reply to this message  
Join Date: Jun 2007
Posts: 322
Reputation: Hamrick will become famous soon enough Hamrick will become famous soon enough 
Solved Threads: 33
Hamrick's Avatar
Hamrick Hamrick is offline Offline
Posting Whiz

Re: Please helpwith how to return two dimensional array

 
1
  #8
Aug 1st, 2007
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.
  1. int **testCase::population() {
  2. // Make the first dimension
  3. int **copy = new int*[3];
  4.  
  5. for ( int i = 0; i < 3; ++i ) {
  6. // Make the second dimension
  7. copy[i] = new int[2];
  8.  
  9. for ( int j = 0; j < 2; ++j ) {
  10. copy[i][j] = matrix[i][j] = rand() % 2;
  11. }
  12. }
  13.  
  14. return copy;
  15. }
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.
  1. vector<vector<int> > testCase::population() {
  2. vector<vector<int> > copy = vector( 3, vector<int>( 2, int ) );
  3.  
  4. for ( int i = 0; i < 3; ++i ) {
  5. for ( int j = 0; j < 2; ++j ) {
  6. copy[i][j] = matrix[i][j] = rand() % 2;
  7. }
  8. }
  9.  
  10. return copy;
  11. }
If you don't want a copy, why do you want to return the array by value?
The truth does not change according to our ability to stomach it.
Reply With Quote Quick reply to this message  
Join Date: Jul 2007
Posts: 18
Reputation: khalidxa is an unknown quantity at this point 
Solved Threads: 0
khalidxa khalidxa is offline Offline
Newbie Poster

Re: Please helpwith how to return two dimensional array

 
0
  #9
Aug 1st, 2007
Could you please tell me what does this statement does
matrix[i][j]=rand()%2;

check your loop working

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


matrix[i][j]=rand()%2; this will randomly allocate values to the array . I did try returning matrix[i][j] , but it did not work, I'm confused
Reply With Quote Quick reply to this message  
Join Date: Feb 2006
Posts: 490
Reputation: Bench has a spectacular aura about Bench has a spectacular aura about Bench has a spectacular aura about 
Solved Threads: 49
Bench's Avatar
Bench Bench is offline Offline
Posting Pro in Training

Re: Please helpwith how to return two dimensional array

 
0
  #10
Aug 1st, 2007
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
  1. int ( &my_func() )[4][5]
  2. {
  3. static int arr[4][5];
  4. return arr;
  5. }
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.
Last edited by Bench; Aug 1st, 2007 at 11:46 am.
¿umop apisdn upside down?
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC