passing arrays

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

Join Date: Dec 2007
Posts: 113
Reputation: zoner7 is an unknown quantity at this point 
Solved Threads: 4
zoner7 zoner7 is offline Offline
Junior Poster

passing arrays

 
0
  #1
Jun 12th, 2008
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?

  1. int main()
  2. {
  3. int Board[9][9];
  4. Board = CreateBoard();
  5. int difficulty = Difficulty();
  6. WriteFile(Board);
  7. Display(Board);
  8. system ("pause");
  9. return 0;
  10. }
  11.  
  12. int CreateBoard()
  13. {
  14. int row = 0;
  15. int column = 0;
  16. bool BoardStatus [9][9];
  17. int Board [9][9]; // Creates the Sudoku board
  18. int BoardTemp;
  19. Reset(Board, BoardStatus);
  20. for (row = 0; row <= 8; row++)
  21. {
  22. for (column = 0; column <= 8; column++)
  23. {
  24. while (BoardStatus [row][column] == false)
  25. {
  26. if (Board [row][column] == -1)
  27. {
  28. Board [row][column] = Random_Number(); // a random number is placed inside every cell before the validation process begins in order to randomize each puzzle.
  29. BoardTemp = Board [row][column];
  30. }
  31. BoardStatus [row][column] = Verify(Board, row, column);
  32. if (BoardStatus [row][column] == false)
  33. {
  34. Board [row][column] = Board[row][column]+ 1;
  35. if (Board [row][column] == 10) // A value inside a cell cannot be greater than 9.
  36. {
  37. Board[row][column] = 1;
  38. }
  39. if (BoardTemp == Board[row][column] )
  40. {
  41. Reset(Board, BoardStatus);
  42. row = 0;
  43. column = 0;
  44. }
  45. }
  46. }
  47. }
  48. }
  49. return Board;
  50. }
Last edited by zoner7; Jun 12th, 2008 at 3:47 am.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,381
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1466
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: passing arrays

 
0
  #2
Jun 12th, 2008
why not just create the array in main() and pass it as a parameter
  1. int CreateBoard(int Board[9][9])
  2. {
  3. // all your code here
  4. return 0;
  5. }
  6.  
  7. int main()
  8. {
  9. int Board[9][9];
  10. CreateBoard(Board);
  11. return 0;
  12. }
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.
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 351
Reputation: Radical Edward has a spectacular aura about Radical Edward has a spectacular aura about Radical Edward has a spectacular aura about 
Solved Threads: 62
Radical Edward's Avatar
Radical Edward Radical Edward is offline Offline
Posting Whiz

Re: passing arrays

 
0
  #3
Jun 12th, 2008
> 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.
If at first you don't succeed, keep on sucking until you do succeed.
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 628
Reputation: daviddoria is a jewel in the rough daviddoria is a jewel in the rough daviddoria is a jewel in the rough 
Solved Threads: 46
daviddoria daviddoria is offline Offline
Practically a Master Poster

Re: passing arrays

 
0
  #4
Jun 12th, 2008
another vote for stl vectors!!
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 113
Reputation: zoner7 is an unknown quantity at this point 
Solved Threads: 4
zoner7 zoner7 is offline Offline
Junior Poster

Re: passing arrays

 
0
  #5
Jun 12th, 2008
hmm. I just got everything up and running using AD's original suggestion with Board[9][9] defined in main(). Perhaps, after I understand vectors a little more clearly, I will replace the arrays with them. Thanks for the help
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,381
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1466
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: passing arrays

 
0
  #6
Jun 12th, 2008
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
  1. typedef vector<int> COLUMNS;
  2. vector<COLUMNS> Board;
  3.  
  4. //or you can do this too
  5. 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.
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