| | |
Please helpwith how to return two dimensional array
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Jul 2007
Posts: 18
Reputation:
Solved Threads: 0
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 :
when I run the program, it returns a memory address. I want it to return the value for the array.
Regards
C++ Syntax (Toggle Plain Text)
int *population();
C++ Syntax (Toggle Plain Text)
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
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 :)
•
•
Join Date: Dec 2006
Posts: 1,089
Reputation:
Solved Threads: 164
•
•
•
•
... when I run the program, it returns a memory address. I want it to return the value for the array.
C++ Syntax (Toggle Plain Text)
#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 ; };
Last edited by vijayan121; Aug 1st, 2007 at 3:19 am.
•
•
•
•
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 :
C++ Syntax (Toggle Plain Text)
int *population();
C++ Syntax (Toggle Plain Text)
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.
¿umop apisdn upside down? 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
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 :)
•
•
•
•
I want it to return the value for the array.
C++ Syntax (Toggle Plain Text)
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; }
C++ Syntax (Toggle Plain Text)
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; }
The truth does not change according to our ability to stomach it.
•
•
Join Date: Jul 2007
Posts: 18
Reputation:
Solved Threads: 0
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
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
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 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.
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
CPP Syntax (Toggle Plain Text)
int ( &my_func() )[4][5] { static int arr[4][5]; return arr; }
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? ![]() |
Similar Threads
- Need Help With two-dimensional array (VB.NET)
- (reformatted) How to return Multi-Dimensional Arrays (C++)
- Validation with js of two dimensional array (JavaScript / DHTML / AJAX)
- 2 dimensional array class (C)
- Need help passing a multi-dimensional array (C++)
- Trying to create a method to convert string letters into a two dimensional array (Java)
Other Threads in the C++ Forum
- Previous Thread: dat,txt file and csv file????
- Next Thread: need some help
| Thread Tools | Search this Thread |
api application array arrays based beginner binary c++ c/c++ calculator char char* class classes code compile compiler console conversion count delete deploy desktop directshow dll download dynamic dynamiccharacterarray encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp iamthwee ifstream input int integer java lib linkedlist linker linux list loop looping loops map math matrix memory news number numbertoword output pointer problem program programming project python random read recursion recursive reference return rpg simple sorting string strings struct temperature template templates test text text-file tree unix url variable vector video visual visualstudio win32 windows winsock wordfrequency wxwidgets






