Runtime error!! ACM problem

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

Join Date: Apr 2004
Posts: 353
Reputation: Asif_NSU is on a distinguished road 
Solved Threads: 2
Asif_NSU's Avatar
Asif_NSU Asif_NSU is offline Offline
Posting Whiz

Runtime error!! ACM problem

 
0
  #1
Nov 18th, 2005
This is (supposed to be) a soln for a ACM problem

But the program produces a runtime error.
Can anyone spot the bug?
  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. }
Reply With Quote Quick reply to this message  
Join Date: May 2004
Posts: 178
Reputation: jim mcnamara is on a distinguished road 
Solved Threads: 10
jim mcnamara jim mcnamara is offline Offline
Junior Poster

Re: Runtime error!! ACM problem

 
0
  #2
Nov 18th, 2005
Have you tried running it in a debugger?
If you do a stack dump (backtrace)
it will show exactly where it bombed.
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 353
Reputation: Asif_NSU is on a distinguished road 
Solved Threads: 2
Asif_NSU's Avatar
Asif_NSU Asif_NSU is offline Offline
Posting Whiz

Re: Runtime error!! ACM problem

 
0
  #3
Nov 18th, 2005
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.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C++ Forum
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC