944,164 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 2585
  • C++ RSS
Nov 18th, 2005
0

Runtime error!! ACM problem

Expand Post »
This is (supposed to be) a soln for a ACM problem

But the program produces a runtime error.
Can anyone spot the bug?
C++ Syntax (Toggle Plain Text)
  1. #include<iostream>
  2. #include<string>
  3.  
  4. #define MAX 25
  5.  
  6. using namespace std;
  7.  
  8. void print_blocks();
  9. int return_initial(int a);//return all the blocks
  10. //on top of block a to their initial pos
  11.  
  12. int track[MAX];//in which stack a block is currently stacked
  13. int blkpos[MAX][MAX];//block stacks
  14. int n;
  15.  
  16. int main()
  17. {
  18. //Initialize Start
  19. cin>>n;
  20. for(int i = 0; i < n; ++i)
  21. for(int j = 0; j < n; ++j)
  22. blkpos[i][j] = -1;
  23.  
  24. for(int i = 0; i < n; ++i)
  25. blkpos[i][0] = i;
  26.  
  27. for(int i = 0; i < n; ++i)
  28. track[i] = i;
  29. //Initialize End
  30.  
  31. string command;
  32. int a,b;
  33. while(cin>>command)
  34. {
  35.  
  36. if(command == "quit")
  37. break;
  38.  
  39. if(command == "move")
  40. {
  41.  
  42. cin>>a>>command>>b;
  43.  
  44. if(track[a] == track[b])//Illegal command
  45. continue;
  46. if(command == "onto")//command == move a onto b
  47. {
  48. int pos = return_initial(a);
  49. blkpos[track[b]][ return_initial(b) + 1] = a;
  50. blkpos[track[a]][pos] = -1;
  51. track[a] = track[b];
  52.  
  53. }
  54. else//command == move a over b
  55. {
  56.  
  57. int pos = return_initial(a);
  58. int i;
  59. for( i = 0; blkpos[track[b]][i] != -1; ++i)
  60. ;
  61. blkpos[track[b]][i] = a;
  62. blkpos[track[a]][pos] = -1;
  63. track[a] = track[b];
  64. }
  65. }
  66. else //command == pile
  67. {
  68. cin>>a>>command>>b;
  69. if(track[a] == track[b])
  70. continue;
  71. if(command == "onto") // command == pile a onto b
  72. {
  73. int pos = return_initial(b);
  74. int i;
  75. for( i = 0; blkpos[track[a]][i] != a; ++i)
  76. ;
  77. int from = track[a];
  78. for( ; blkpos[from][i] != -1; ++i)
  79. {
  80. blkpos[track[b]][++pos] = blkpos[from][i];
  81. track[blkpos[from][i]] = track[b];
  82. blkpos[from][i] = -1;
  83. }
  84. }
  85. else //command == pile a over b
  86. {
  87. int pos;
  88. int i;
  89. for(i = 0; blkpos[track[b]][i] != b; ++i)
  90. ;
  91. pos = i;
  92. for( i = 0; blkpos[track[a]][i] != a; ++i)
  93. ;
  94. int from = track[a];
  95. for( ; blkpos[from][i] != -1; ++i)
  96. {
  97. blkpos[track[b]][++pos] = blkpos[from][i];
  98. track[blkpos[from][i]] = track[b];
  99. blkpos[from][i] = -1;
  100. }
  101. }
  102.  
  103. }
  104.  
  105.  
  106. }
  107.  
  108. print_blocks();
  109. }
  110.  
  111.  
  112. int return_initial(int a)
  113. {
  114.  
  115. int i;
  116. for(i = 0; blkpos[ track[a]][i] != a; ++i )
  117. ;
  118. int temp;
  119. int pos = i;
  120.  
  121. for(++i ; (temp = blkpos[ track[a]][i]) != -1; ++i )
  122. {
  123. blkpos[ track[a]][i] = -1;
  124. int j;
  125. for( j = 0; blkpos[temp][j] != -1; ++j)
  126. ;
  127. blkpos[temp][j] = temp;
  128. track[temp] = temp;
  129.  
  130. }
  131. return pos;
  132.  
  133. }
  134.  
  135.  
  136. void print_blocks()
  137. {
  138.  
  139. for(int i = 0; i < n; ++i)
  140. {
  141. cout<<i<<":";
  142. for(int j = 0; blkpos[i][j] != -1; ++j)
  143. {
  144. cout<<" "<<blkpos[i][j];
  145. }
  146. cout<<"\n";
  147. }
  148. }
Similar Threads
Reputation Points: 113
Solved Threads: 3
Posting Whiz
Asif_NSU is offline Offline
353 posts
since Apr 2004
Nov 18th, 2005
0

Re: Runtime error!! ACM problem

Have you tried running it in a debugger?
If you do a stack dump (backtrace)
it will show exactly where it bombed.
Reputation Points: 62
Solved Threads: 10
Junior Poster
jim mcnamara is offline Offline
179 posts
since May 2004
Nov 18th, 2005
0

Re: Runtime error!! ACM problem

I havent used a debugger, but I think it produces runtime error depending on the input. For some input the online judge uses it produces the error, not for the input i gave to it.
Reputation Points: 113
Solved Threads: 3
Posting Whiz
Asif_NSU is offline Offline
353 posts
since Apr 2004
Aug 7th, 2011
0
Re: Runtime error!! ACM problem
Hi Asif, I don't know if you still need this, but I think the cause of runtime error is the size of MAX. What if the given input for 'n' is greater than 25??? Then you'll get a Run-time Error. (but i'm not sure, obviously if it is mentioned in problem that the input will be less than 25 than this is NOT the cause of the problem)
Reputation Points: 10
Solved Threads: 0
Newbie Poster
blackcloudbd is offline Offline
17 posts
since May 2009

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: Problem in using vector for generating subset of a string.
Next Thread in C++ Forum Timeline: Not declared in this scope, Help!





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


Follow us on Twitter


© 2011 DaniWeb® LLC