943,901 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 505
  • C++ RSS
Sep 17th, 2009
0

Problem with a cout test statement

Expand Post »
Hello everyone, I'm having a bit of an issue here. I'm doing a Knight's Tour program for class, and thought I just about had it until this problem popped up.

While I was writing my code, I had some simple cout test statements after some of the function calls just to be able to mark my progress as I went along. Later, my program finally was displaying the proper filled solution arrays, with the test statements in between. When I went to get rid of these statements, the program no longer worked properly.

C++ Syntax (Toggle Plain Text)
  1. //find next possible moves;
  2. cnt = next_valid_cells(cc, nc);
  3.  
  4. cout << "TEST" << endl;
  5.  
  6. //find the best move out of all these valid moves
  7. bestndx = bestmove(nc, cnt);

I narrowed it down to the above "TEST" statement I used between these two function calls. When I run the program with the statement there like above, it works...but with "TEST" between all the solution arrays 60 or so times depending on the particular solution its testing. When I comment it out, the program hangs and doesn't do anything.

I don't understand why I can't just remove that cout statement, which would leave my program running just right. I've never had a problem like this before, so I thought I'd come to the pros. Any help would be appreciated.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
RedWinger is offline Offline
3 posts
since Sep 2009
Sep 17th, 2009
0

Re: Problem with a cout test statement

There's a memory overwrite bug in your code somewhere (most likely anyway).

Without seeing the whole code, we're not going to figure anything out staring at those 3 lines.
Team Colleague
Reputation Points: 5862
Solved Threads: 950
Posting Sage
Salem is offline Offline
7,164 posts
since Dec 2005
Sep 17th, 2009
0

Re: Problem with a cout test statement

Sorry about that, I didn't know if I should post the whole code or not or if there was just some obvious answer that was eluding me.
Here's all the code.

C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. #include <iomanip>
  3. using namespace std;
  4.  
  5. #define SIZE 8
  6.  
  7. int board[SIZE][SIZE]; //board, DX, DY, PRIORITY
  8. const int DX[SIZE] = {-2,-1, 1,2, 2, 1,-1,-2}; //are all global variables
  9. const int DY[SIZE] = { 1, 2, 2,1,-1, -2,-2,-1};
  10. const int PRIORITY[SIZE][SIZE] = {
  11. {1,2,2,2,2,2,2,1},
  12. {2,3,4,4,4,4,3,2},
  13. {2,4,5,6,6,5,4,2},
  14. {2,4,6,7,7,6,4,2},
  15. {2,4,6,7,7,6,4,2},
  16. {2,4,5,6,6,5,4,2},
  17. {2,3,4,4,4,4,3,2},
  18. {1,2,2,2,2,2,2,1},
  19. };
  20. struct CELL { //a cell on the board
  21. int row, col;
  22. };
  23. void display_board() { //display the board, 0=empty; >0 means occupied
  24. int i, j;
  25. for (i = 0; i < SIZE; i++)
  26. {
  27. for (j = 0; j < SIZE; j++)
  28. {
  29. cout << setw(4) << board[i][j];
  30. }
  31. cout << endl;
  32. }
  33. }
  34. void init() { //board[i][j]==0 means cell (i, j) is empty
  35. int i, j;
  36. for (i = 0; i < SIZE; i++)
  37. {
  38. for (j = 0; j < SIZE; j++)
  39. {
  40. board[i][j] = 0;
  41. }
  42. }
  43. }
  44. bool valid_cell(int row, int col) { //(row, col) is "valid" if it is inside the board and is empty
  45. //to be valid row and col should be >=0 and < SIZE
  46. //also: board[row][col]should be ==0
  47. //write this code
  48.  
  49. if ( row < 0 || row >= SIZE ) //checks for row inside the board
  50. {
  51. return false;
  52. }
  53.  
  54. if ( col < 0 || col >= SIZE ) //checks for column inside the board
  55. {
  56. return false;
  57. }
  58.  
  59. if ( board[row][col] != 0 ) //checks if cell is empty
  60. {
  61. return false;
  62. }
  63.  
  64. else
  65. {
  66. return true; //returns true for valid cells
  67. }
  68. }
  69. int next_valid_cells (CELL c, CELL nc[]) { //store the next valid cells in nc array and return the count
  70. //go thru all cells: (c.row+DX[i], c.col+DY[i]) i=0 to SIZE-1
  71. //if it is valid cell put it in nc array
  72. //and keep trackof how many are there: cnt and return this value
  73.  
  74. int count = 0;
  75.  
  76. for ( int i = 0; i < SIZE; i++ ) //loop cycles through the 8 possible moves
  77. //write the code
  78. {
  79. if ( valid_cell( c.row+DX[i], c.col+DY[i] ) ) //if cell is valid
  80. {
  81. nc[count].row = c.row+DX[i]; //assigns valid cell row to nc array
  82. nc[count].col = c.col+DY[i]; //assigns valid cell column to nc array
  83. count++; //counts number of valid moves
  84. }
  85. }
  86.  
  87. return count; // returns valid move count
  88. }
  89.  
  90. int bestmove(CELL nc[], int cnt) { //return the index of the best move
  91. //you know nc[0], nc[1], nc[cnt-1] hold the valid cells
  92. //find the cell which has the least priority number
  93. //and return this index
  94.  
  95. //complete this code
  96.  
  97. int best_index, smallest;
  98.  
  99. if ( cnt == 0 ) //if count is zero, there were no valid moves found
  100. {
  101. best_index = -1;
  102. }
  103.  
  104. else
  105. {
  106. smallest = PRIORITY[nc[0].row][nc[0].col];
  107.  
  108. for ( int i = 1; i < cnt; i++ )
  109. {
  110. if ( PRIORITY[nc[i].row][nc[i].col] < smallest )
  111. {
  112. smallest = PRIORITY[nc[i].row][nc[i].col];
  113. best_index = i;
  114. }
  115. }
  116. }
  117.  
  118. return best_index;
  119. }
  120.  
  121.  
  122.  
  123. int main() {
  124. int i, j, num, cnt, bestndx;
  125. CELL cc, nc[SIZE]; //cc=current cell; nc=next cell array
  126. for ( i = 0; i < SIZE; i++)
  127. {
  128. for (j = 0; j < SIZE; j++)
  129. { //go thru all start positions (i, j)
  130. init(); //make board all 0. (all empty)
  131. cc.row = i;
  132. cc.col = j;
  133. num = 1;
  134. board[cc.row][cc.col] = num; //put 1 on the start cell
  135. num++; //increment and keep ready to put the next number
  136.  
  137.  
  138. while (1)
  139. { //keep going as long as you can go or this starting point (i, j)
  140. //find next possible moves;
  141. cnt = next_valid_cells(cc, nc);
  142.  
  143. cout << "TEST" << endl;
  144.  
  145. //find the best move out of all these valid moves
  146. bestndx = bestmove(nc, cnt);
  147.  
  148. if ( bestndx == -1 )
  149. {
  150. break;
  151. } //no valid moves; break out of while loop
  152. //else put the number on the board and reset current cell to be this new cell and loop back
  153. else
  154. {
  155. board[nc[bestndx].row][nc[bestndx].col] = num;
  156. num++;
  157. cc.row = nc[bestndx].row; cc.col = nc[bestndx].col;
  158. }
  159.  
  160. } //completed for one start position (i, j)
  161. display_board();
  162. if (num < 65) {
  163. cout << i <<" " << j << " failed. max entry= " << num - 1 << endl;
  164. }
  165. else cout << "SUCCESS!! made all 64 moves from " << i << " " << j << endl;
  166. }
  167. }
  168.  
  169.  
  170. system("pause");
  171. return 0;
  172. }
Reputation Points: 10
Solved Threads: 0
Newbie Poster
RedWinger is offline Offline
3 posts
since Sep 2009
Sep 17th, 2009
0

Re: Problem with a cout test statement

C++ Syntax (Toggle Plain Text)
  1. smallest = PRIORITY[nc[0].row][nc[0].col];
  2.  
  3. for ( int i = 1; i < cnt; i++ )
If count is 1, bestindex is garbage.
Team Colleague
Reputation Points: 5862
Solved Threads: 950
Posting Sage
Salem is offline Offline
7,164 posts
since Dec 2005
Sep 17th, 2009
0

Re: Problem with a cout test statement

Click to Expand / Collapse  Quote originally posted by Salem ...
C++ Syntax (Toggle Plain Text)
  1. smallest = PRIORITY[nc[0].row][nc[0].col];
  2.  
  3. for ( int i = 1; i < cnt; i++ )
If count is 1, bestindex is garbage.

Thanks for spotting that! I initialized best_index to zero, and the everything was fixed when I removed the test statement.

So how did the garbage data that I had when count was 1 cause the program to not run correctly with that test statement? I would have thought that it would messed up the actual solutions, which it didn't appear to do. Just trying to understand how the two relate.

Thanks again. :-)
Reputation Points: 10
Solved Threads: 0
Newbie Poster
RedWinger is offline Offline
3 posts
since Sep 2009
Sep 17th, 2009
0

Re: Problem with a cout test statement

Pure dumb luck is all.

Your cout statement basically filled that part of the stack with a value which was harmless enough (maybe even the right answer).

Taking out the cout statement, that part of memory had different garbage, and a different result.
Team Colleague
Reputation Points: 5862
Solved Threads: 950
Posting Sage
Salem is offline Offline
7,164 posts
since Dec 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: control reaches end of non-void function
Next Thread in C++ Forum Timeline: adding a bunch of random numbers





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC