| | |
passing arrays
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Dec 2007
Posts: 113
Reputation:
Solved Threads: 4
I am having a compiling issue because of some syntax errors while trying to return an array. You can probably ignore most of the code below; I am only concerned with the call and return of the CreateBoard[][] array. As I understand a pointer might be necessary to do this. Does anyone have some tips that I can use?
C++ Syntax (Toggle Plain Text)
int main() { int Board[9][9]; Board = CreateBoard(); int difficulty = Difficulty(); WriteFile(Board); Display(Board); system ("pause"); return 0; } int CreateBoard() { int row = 0; int column = 0; bool BoardStatus [9][9]; int Board [9][9]; // Creates the Sudoku board int BoardTemp; Reset(Board, BoardStatus); for (row = 0; row <= 8; row++) { for (column = 0; column <= 8; column++) { while (BoardStatus [row][column] == false) { if (Board [row][column] == -1) { Board [row][column] = Random_Number(); // a random number is placed inside every cell before the validation process begins in order to randomize each puzzle. BoardTemp = Board [row][column]; } BoardStatus [row][column] = Verify(Board, row, column); if (BoardStatus [row][column] == false) { Board [row][column] = Board[row][column]+ 1; if (Board [row][column] == 10) // A value inside a cell cannot be greater than 9. { Board[row][column] = 1; } if (BoardTemp == Board[row][column] ) { Reset(Board, BoardStatus); row = 0; column = 0; } } } } } return Board; }
Last edited by zoner7; Jun 12th, 2008 at 3:47 am.
why not just create the array in main() and pass it as a parameter
C++ Syntax (Toggle Plain Text)
int CreateBoard(int Board[9][9]) { // all your code here return 0; } int main() { int Board[9][9]; CreateBoard(Board); return 0; }
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
> Does anyone have some tips that I can use?
C++ doesn't allow you to return an array. You can return a pointer to an array or a pointer to the first element of an array. Both of those are tricky to get right if the array is local to the function returning it. The best all around trick using arrays is to pass the array as a parameter like Ancient Dragon showed. The best solution is probably to use the STL vector instead of an array. With a vector you can return a copy of the vector and all of the problems with returning arrays go away.
C++ doesn't allow you to return an array. You can return a pointer to an array or a pointer to the first element of an array. Both of those are tricky to get right if the array is local to the function returning it. The best all around trick using arrays is to pass the array as a parameter like Ancient Dragon showed. The best solution is probably to use the STL vector instead of an array. With a vector you can return a copy of the vector and all of the problems with returning arrays go away.
If at first you don't succeed, keep on sucking until you do succeed.
vectors are great for single dimension arrays. As you add more dimensions vectors become much more complicated than simple int arrays like Board. Here's an example of a 2d array of vectors
The nice thing about the above is that the rows do not have to all contain the same number of columns, that is the number of columns can be different for each row.
C++ Syntax (Toggle Plain Text)
typedef vector<int> COLUMNS; vector<COLUMNS> Board; //or you can do this too vector< vector<int> > Board;
The nice thing about the above is that the rows do not have to all contain the same number of columns, that is the number of columns can be different for each row.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
![]() |
Similar Threads
- Problem when passing arrays from c# to matlab (C#)
- Passing arrays between subs (VB.NET)
- Help: Passing arrays between functions (C)
- What relation does **indirection operator have with Multidimensional Arrays (C++)
- Passing arrays of objects to functions (C++)
- passing arrays to classes? (Java)
- passing arrays in visual basic (Visual Basic 4 / 5 / 6)
Other Threads in the C++ Forum
- Previous Thread: Add a little Graphics to your Console
- Next Thread: returning a vector that is a local variable
| Thread Tools | Search this Thread |
api array arrays based beginner binary bitmap c++ c/c++ calculator char class classes code compile compiler console conversion count data delete deploy desktop developer directshow dll download dynamic encryption error file forms fstream function functions game getline givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux list loop looping loops map math matrix memory news node number output parameter pointer problem program programming project proxy python read recursion recursive return string strings struct temperature template templates test text text-file tree unix url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






