Problem with loops

Reply

Join Date: Oct 2007
Posts: 12
Reputation: Srynx is an unknown quantity at this point 
Solved Threads: 0
Srynx Srynx is offline Offline
Newbie Poster

Problem with loops

 
0
  #1
Oct 20th, 2007
Hi,
I'm working in a 4x4 sudoku solver and i'm a beginner in c++.
I use Microsoft Visual Studio 6.0 and windows xp professional
Variables are:
a1 b1 c1 d1
a2 b2 c2 d2
a3 b3 c3 d3
a4 b4 c4 d4
First i wrote that program
  1. //sudoku.cpp:
  2.  
  3. #include<stdlib.h>
  4. #include<math.h>
  5. #include<stdio.h>
  6. #include<time.h>
  7. #include<algorithm>
  8.  
  9. void main(){
  10.  
  11. srand(time(0));
  12.  
  13. int a1;
  14. int b1;
  15. int c1;
  16. int d1;
  17. int a2;
  18. int b2;
  19. int c2;
  20. int d2;
  21. int a3;
  22. int b3;
  23. int c3;
  24. int d3;
  25. int a4;
  26. int b4;
  27. int c4;
  28. int d4;
  29.  
  30.  
  31. srand(time(0));
  32.  
  33.  
  34. // 1st row
  35.  
  36. a1=rand()*5/32768;
  37. printf("a1 = %d\n",a1);
  38.  
  39. while((b1!=0)||(b1=a1)){
  40. b1=rand()*5/32768;
  41. if ((b1!=0)&&(b1!=a1)){
  42. break;
  43. printf("b1 %d\n",b1);
  44. }}
  45.  
  46. printf("b1 = %d\n",b1);
  47.  
  48. while((c1!=0)||(c1=a1)||(c1=b1)){
  49. c1=rand()*5/32768;
  50. if ((c1!=0)&&(c1!=a1)&&(c1!=b1)){
  51. break;
  52. }}
  53. printf("c1 = %d\n",c1);
  54.  
  55. while((d1!=0)||(d1=a1)||(d1=b1)||(d1=c1)){
  56. d1=rand()*5/32768;
  57. if ((d1!=0)&&(d1!=a1)&&(d1!=b1)&&(d1!=c1)){
  58. break;
  59. }}
  60. printf("d1 = %d\n",d1);
  61.  
  62.  
  63. // Column A
  64.  
  65. while((a2!=0)||(a2=a1)||(a2=b1)){
  66. a2=rand()*5/32768;
  67. if ((a2!=0)&&(a2!=a1)&&(a2!=b1)){
  68. break;
  69. }}
  70.  
  71. printf("a2 = %d\n",a2);
  72.  
  73. while((a3!=0)||(a3=a1)||(a3=a2)){
  74. a3=rand()*5/32768;
  75. if ((a3!=0)&&(a3!=a1)&&(a3!=a2)){
  76. break;
  77. }}
  78.  
  79. printf("a3 = %d\n",a3);
  80.  
  81.  
  82. while((a4!=0)||(a4=a1)||(a4=a2)||(a4=a3)){
  83. a4=rand()*5/32768;
  84. if ((a4!=0)&&(a4!=a1)&&(a4!=a2)&&(a4!=a3)){
  85. break;
  86. }}
  87.  
  88. printf("a4 = %d\n",a4);
  89.  
  90. // Column D
  91.  
  92. while((d2!=0)||(d2=c1)||(d2=d1)||(d2=a2)){
  93. d2=rand()*5/32768;
  94. if ((d2!=0)&&(d2!=c1)&&(d2!=d1)&&(d2!=a2)){
  95. break;
  96. }}
  97.  
  98. printf("d2 = %d\n",d2);
  99.  
  100.  
  101. while((d3!=0)||(d3=d1)||(d3=d2)||(d3=a3)){
  102. d3=rand()*5/32768;
  103. if ((d3!=0)&&(d3!=d1)&&(d3!=d2)&&(d3!=a3)){
  104. break;
  105. }}
  106.  
  107. printf("d3 = %d\n",d3);
  108.  
  109.  
  110. while((d4!=0)||(d4=d1)||(d4=d2)||(d4=d3)||(d4=a4)){
  111. d4=rand()*5/32768;
  112. if ((d4!=0)&&(d4!=d1)&&(d4!=d2)&&(d4!=d3)&&(d4!=a4)){
  113. break;
  114. }}
  115.  
  116. printf("d4 = %d\n",d4);
  117.  
  118. // 4th row
  119.  
  120. while((b4!=0)||(b4=a3)||(b4=a4)||(b4=b1)||(b4=d4)){
  121. b4=rand()*5/32768;
  122. if ((b4!=0)&&(b4!=a3)&&(b4!=a4)&&(b4!=b1)&&(b4!=d4)){
  123. break;
  124. }}
  125.  
  126. printf("b4 = %d\n",b4);
  127.  
  128. while((c4!=0)||(c4=d3)||(c4=d4)||(c4=c1)||(c4=a4)||(c4=b4)){
  129. c4=rand()*5/32768;
  130. if ((c4!=0)&&(c4!=d3)&&(c4!=d4)&&(c4!=c1)&&(c4!=a4)){
  131. break;
  132. }}
  133.  
  134. printf("c4 = %d\n",c4);
  135.  
  136. // Central Square
  137.  
  138. while((b2!=0)||(b2=a1)||(b2=b1)||(b2=a2)||(b2=d2)||(b2=b4)){
  139. b2=rand()*5/32768;
  140. if ((b2!=0)&&(b2!=a1)&&(b2!=b1)&&(b2!=a2)&&(b2!=d2)&&(b2!=b4)){
  141. break;
  142. }}
  143.  
  144. printf("b2 = %d\n",b2);
  145.  
  146. while((c2!=0)||(c2=c1)||(c2=d1)||(c2=a2)||(c2=b2)||(c2=d2)||(c2=c4)){
  147. c2=rand()*5/32768;
  148. if ((c2!=0)&&(c2!=c1)&&(c2!=d1)&&(c2!=a2)&&(c2!=b2)&&(c2!=d2)&&(c2!=c4)){
  149. break;
  150. }}
  151.  
  152. printf("c2 = %d\n",c2);
  153.  
  154. while((b3!=0)||(b3=a3)||(b3=a4)||(b3=b4)||(b3=b1)||(b3=b2||(b3=d3))){
  155. b3=rand()*5/32768;
  156. if ((b3!=0)&&(b3!=a3)&&(b3!=a4)&&(b3!=b4)&&(b3!=b1)&&(b3!=b2)&&(b3!=d3)){
  157. break;
  158. }}
  159.  
  160. printf("b3 = %d\n",b3);
  161.  
  162. while((c3!=0)||(c3=c4)||(c3=d4)||(c3=d3)||(c3=c1)||(c3=c2)||(c3=a3)||(c3=b3)){
  163. c3=rand()*5/32768;
  164. if ((c3!=0)&&(c3!=c4)&&(c3!=d4)&&(c3!=d3)&&(c3!=c1)&&(c3!=c2)&&(c3!=a3)&&(c3!=b3)){
  165. break;
  166. }}
  167. printf("c3 = %d\n",c3);
  168.  
  169. }

It works but sometimes it reach absurd situations like this:
1 4 2 3
3 4
4 1
2 !

When that happens program stops and don't show the solced variables in that case a1=1 b1=4 c1=2 d1=3 a2=3 a3=4 a4=2 d2=4 d3=1

In order to solve that i've writen another code with a loop that doesn't stop the program until the last variable to be evaluated (c3) is not 0.

I've post it here:
  1. //sudoku.cpp:
  2.  
  3. #include<stdlib.h>
  4. #include<math.h>
  5. #include<stdio.h>
  6. #include<time.h>
  7. #include<algorithm>
  8. #include<iostream>
  9. using std::cout;
  10. using std::endl;
  11. using std::cin;
  12.  
  13. void main(){
  14.  
  15.  
  16. int a1;
  17. int b1;
  18. int c1;
  19. int d1;
  20. int a2;
  21. int b2;
  22. int c2;
  23. int d2;
  24. int a3;
  25. int b3;
  26. int c3;
  27. int d3;
  28. int a4;
  29. int b4;
  30. int c4;
  31. int d4;
  32. int xval;
  33.  
  34. srand(time(0));
  35. c3=0;
  36.  
  37. while(c3==0)
  38. {
  39. // 1st row
  40.  
  41. a1=(rand()%4)+1;
  42.  
  43. cout <<"a1 = "<<a1<<endl;
  44.  
  45. do {
  46. b1=(rand()%4)+1;
  47. }while(b1==a1);
  48. {
  49. cout <<"b1 = "<<b1<<endl;
  50. }
  51.  
  52. do {
  53. c1=(rand()%4)+1;
  54. }while((c1==a1)||(c1==b1));
  55. {
  56. cout <<"c1 = "<<c1<<endl;
  57. }
  58.  
  59. do {
  60. d1=(rand()%4)+1;
  61. }while((d1==a1)||(d1==b1)||(d1==c1));
  62. {
  63. cout <<"d1 = "<<d1<<endl;
  64. }
  65.  
  66. // Column A
  67. do {
  68. a2=(rand()%4)+1;
  69. }while((a2==a1)||(a2==b1));
  70. {
  71. cout <<"a2 = "<<a2<<endl;
  72. }
  73.  
  74. do {
  75. a3=(rand()%4)+1;
  76. }while((a3==a1)||(a3==a2));
  77. {
  78. cout <<"a3 = "<<a3<<endl;
  79. }
  80.  
  81. do {
  82. a4=(rand()%4)+1;
  83. }while((a4==a1)||(a4==a2)||(a4==a3));
  84. {
  85. cout <<"a4 = "<<a4<<endl;
  86. }
  87.  
  88. // Column D
  89. do {
  90. d2=(rand()%4)+1;
  91. }while((d2==c1)||(d2==d1)||(d2==a2));
  92. {
  93. cout <<"d2 = "<<d2<<endl;
  94. }
  95. do {
  96. d3=(rand()%4)+1;
  97. }while((d3==d1)||(d3==d2)||(d3==a3));
  98. {
  99. cout <<"d3 = "<<d3<<endl;
  100. }
  101. do {
  102. d4=(rand()%4)+1;
  103. }while((d4==d1)||(d4==d2)||(d4==d3)||(d4==a4));
  104. {
  105. cout <<"d4 = "<<d4<<endl;
  106. }
  107.  
  108. // 4th row
  109.  
  110. do {
  111. b4=(rand()%4)+1;
  112. }while((b4==a3)||(b4==a4)||(b4==b1)||(b4==d4));
  113. {
  114. cout <<"b4 = "<<b4<<endl;
  115. }
  116.  
  117. do {
  118. c4=(rand()%4)+1;
  119. }while((c4==d3)||(c4==d4)||(c4==c1)||(c4==a4)||(c4==b4));
  120. {
  121. cout <<"c4 = "<<c4<<endl;
  122. }
  123.  
  124. // Central square
  125.  
  126. do {
  127. b2=(rand()%4)+1;
  128. }while((b2==a1)||(b2==b1)||(b2==a2)||(b2==d2)||(b2==b4));
  129. {
  130. cout <<"b2 = "<<b2<<endl;
  131. }
  132.  
  133. do {
  134. c2=(rand()%4)+1;
  135. }while((c2==c1)||(c2==d1)||(c2==a2)||(c2==b2)||(c2==d2)||(c2==c4));
  136. {
  137. cout <<"c2 = "<<c2<<endl;
  138. }
  139.  
  140. do {
  141. b3=(rand()%4)+1;
  142. }while((b3==a3)||(b3==a4)||(b3==b4)||(b3==b1)||(b3==b2)||(b3==d3));
  143. {
  144. cout <<"b3 = "<<b3<<endl;
  145. }
  146.  
  147. do {
  148. c3=(rand()%4)+1;
  149. }while((c3==c4)||(c3==d4)||(c3==d3)||(c3==c1)||(c3==c2)||(c3==a3)||(c3==b3));
  150. {
  151. cout <<"c3 = "<<c3<<endl;
  152. }
  153. if (c3!=0)
  154. {
  155. break;
  156. }
  157. }
  158.  
  159. // It shows all sudoku
  160.  
  161. cout <<""<<endl;
  162. cout <<"Sudoku"<<endl;
  163.  
  164. cout <<a1<<" "<<b1<<" "<<c1<<" "<<d1<<endl;
  165. cout <<a2<<" "<<b2<<" "<<c2<<" "<<d2<<endl;
  166. cout <<a3<<" "<<b3<<" "<<c3<<" "<<d3<<endl;
  167. cout <<a4<<" "<<b4<<" "<<c4<<" "<<d4<<endl;
  168.  
  169. cout <<""<<endl;
  170.  
  171.  
  172. cout <<a1<<" "<<b1<<" "<<c1<<" "<<d1<<endl;
  173. cout <<a2<<" "<<"x"<<" "<<c2<<" "<<d2<<endl;
  174. cout <<a3<<" "<<b3<<" "<<"y"<<" "<<d3<<endl;
  175. cout <<"z"<<" "<<b4<<" "<<c4<<" "<<d4<<endl;
  176.  
  177.  
  178.  
  179. cout <<""<<endl;
  180. while (xval!=b2){cout << "What's x value?\n";
  181. cin >> xval;
  182. if (xval==b2) {
  183. cout << "OK\n";
  184. break;
  185. }
  186.  
  187. }
  188. cout <<""<<endl;
  189. while (xval!=c3){cout << "What's y value?\n";
  190. cin >> xval;
  191. if (xval==c3) {
  192. cout << "OK\n";
  193. break;
  194. }
  195.  
  196. }
  197. cout <<""<<endl;
  198. while (xval!=a4){cout << "What's x value??\n";
  199. cin >> xval;
  200. if (xval==a4) {
  201. cout << "OK\n";
  202. break;
  203. }
  204.  
  205. }
  206. cout <<""<<endl;

First the loop seemed work but later i noticed it reach these absurd situacions too.
Anyone know why? i've no idea what's wrong
thank you
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,171
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: 1439
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Most Valuable Poster

Re: Problem with loops

 
0
  #2
Oct 20th, 2007
>>Microsoft Visual Studio 6.0
That's your first mistake. Unless you are required to use that compiler by your school you should scrap that compiler because of its age and known NON-compilance with c++ standards. You can get a pretty good free VC++ 2005 Express compiler that will let you code most everything you will learn in school and text books.

Why in the world are you using all those variables ? I thought you are supposed to be learning matrices, such as one declared like this ? That will greatly simplify your code because you only have to deal with ONE variable, not 16.
  1. int matrix[4][4];
Last edited by Ancient Dragon; Oct 20th, 2007 at 6:54 pm.
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: Oct 2007
Posts: 12
Reputation: Srynx is an unknown quantity at this point 
Solved Threads: 0
Srynx Srynx is offline Offline
Newbie Poster

Re: Problem with loops

 
0
  #3
Nov 3rd, 2007
Hi,
I've wrote all code using using matrices and the problem happens even more often.
I've tried it in another compiler (DEV-C++) and with this one program just doesn't run.
Reply With Quote Quick reply to this message  
Join Date: Jul 2007
Posts: 54
Reputation: tracethepath is an unknown quantity at this point 
Solved Threads: 4
tracethepath's Avatar
tracethepath tracethepath is offline Offline
Junior Poster in Training

Re: Problem with loops

 
0
  #4
Nov 3rd, 2007
can you post the corrected code??
i.e. the code after using matrices??
With Regards...
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 3,114
Reputation: WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of 
Solved Threads: 281
Moderator
WaltP's Avatar
WaltP WaltP is offline Offline
Posting Sensei

Re: Problem with loops

 
0
  #5
Nov 3rd, 2007
Originally Posted by Srynx View Post
Hi,
I've wrote all code using using matrices and the problem happens even more often.
So rather than fix it, you took out what you were supposed to learn?

Originally Posted by Srynx View Post
I've tried it in another compiler (DEV-C++) and with this one program just doesn't run.
That should tell you something. The code is very wrong. What happens with Dev? Crashes? Doesn't compile?

Start over and slow. Write a small piece of the code, compile, test. Then add the next step, compile, test. Repeat until the program is finished...
Last edited by WaltP; Nov 3rd, 2007 at 2:21 pm.
The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 12
Reputation: Srynx is an unknown quantity at this point 
Solved Threads: 0
Srynx Srynx is offline Offline
Newbie Poster

Re: Problem with loops

 
0
  #6
Nov 3rd, 2007
Hi,
can you post the corrected code??
i.e. the code after using matrices??
here the corrected code:
  1. //sudoku.cpp:
  2.  
  3. #include<stdlib.h>
  4. #include<math.h>
  5. #include<stdio.h>
  6. #include<time.h>
  7. #include<algorithm>
  8. #include<iostream>
  9. using std::cout;
  10. using std::endl;
  11. using std::cin;
  12.  
  13. void main(){
  14.  
  15. int ninc=0;
  16. int xval;
  17. int puzzle[4][4];
  18.  
  19. srand(time(0));
  20. puzzle[2][2]=0;
  21.  
  22. while(puzzle[2][2]==0)
  23. {
  24. // Fila 1
  25.  
  26. puzzle[0][0]=(rand()%4)+1;
  27.  
  28.  
  29.  
  30. do {
  31. puzzle[0][1]=(rand()%4)+1;
  32. }while(puzzle[0][1]==puzzle[0][0]);
  33. {
  34.  
  35. }
  36.  
  37. do {
  38. puzzle[0][2]=(rand()%4)+1;
  39. }while((puzzle[0][2]==puzzle[0][0])||(puzzle[0][2]==puzzle[0][1]));
  40. {
  41.  
  42. }
  43.  
  44. do {
  45. puzzle[0][3]=(rand()%4)+1;
  46. }while((puzzle[0][3]==puzzle[0][0])||(puzzle[0][3]==puzzle[0][1])||(puzzle[0][3]==puzzle[0][2]));
  47. {
  48.  
  49. }
  50.  
  51. // Columna A
  52. do {
  53. puzzle[1][0]=(rand()%4)+1;
  54. }while((puzzle[1][0]==puzzle[0][0])||(puzzle[1][0]==puzzle[0][1]));
  55. {
  56.  
  57. }
  58.  
  59. do {
  60. puzzle[2][0]=(rand()%4)+1;
  61. }while((puzzle[2][0]==puzzle[0][0])||(puzzle[2][0]==puzzle[1][0]));
  62. {
  63.  
  64. }
  65.  
  66. do {
  67. puzzle[3][0]=(rand()%4)+1;
  68. }while((puzzle[3][0]==puzzle[0][0])||(puzzle[3][0]==puzzle[0][1])||(puzzle[3][0]==puzzle[2][0]));
  69. {
  70.  
  71. }
  72.  
  73. // Columna D
  74. do {
  75. puzzle[1][3]=(rand()%4)+1;
  76. }while((puzzle[1][3]==puzzle[0][2])||(puzzle[1][3]==puzzle[0][3])||(puzzle[1][3]==puzzle[1][0]));
  77. {
  78.  
  79. }
  80. do {
  81. puzzle[2][3]=(rand()%4)+1;
  82. }while((puzzle[2][3]==puzzle[0][3])||(puzzle[2][3]==puzzle[1][3])||(puzzle[2][3]==puzzle[2][0]));
  83. {
  84.  
  85. }
  86. do {
  87. puzzle[3][3]=(rand()%4)+1;
  88. }while((puzzle[3][3]==puzzle[0][3])||(puzzle[3][3]==puzzle[1][3])||(puzzle[3][3]==puzzle[2][3])||(puzzle[3][3]==puzzle[3][0]));
  89. {
  90.  
  91. }
  92.  
  93. // Fila 4
  94.  
  95. do {
  96. puzzle[3][1]=(rand()%4)+1;
  97. }while((puzzle[3][1]==puzzle[2][0])||(puzzle[3][1]==puzzle[3][0])||(puzzle[3][1]==puzzle[0][1])||(puzzle[3][1]==puzzle[3][3]));
  98. {
  99.  
  100. }
  101.  
  102. do {
  103. puzzle[3][2]=(rand()%4)+1;
  104. }while((puzzle[3][2]==puzzle[2][3])||(puzzle[3][2]==puzzle[3][3])||(puzzle[3][2]==puzzle[0][2])||(puzzle[3][2]==puzzle[3][0])||(puzzle[3][2]==puzzle[3][1]));
  105. {
  106.  
  107. }
  108.  
  109. // Quadrat central
  110.  
  111. do {
  112. puzzle[1][1]=(rand()%4)+1;
  113. }while((puzzle[1][1]==puzzle[0][0])||(puzzle[1][1]==puzzle[0][1])||(puzzle[1][1]==puzzle[1][0])||(puzzle[1][1]==puzzle[1][3])||(puzzle[1][1]==puzzle[3][1]));
  114. {
  115.  
  116. }
  117.  
  118. do {
  119. puzzle[1][2]=(rand()%4)+1;
  120. }while((puzzle[1][2]==puzzle[0][2])||(puzzle[1][2]==puzzle[0][3])||(puzzle[1][2]==puzzle[1][0])||(puzzle[1][2]==puzzle[1][1])||(puzzle[1][2]==puzzle[1][3])||(puzzle[1][2]==puzzle[3][2]));
  121. {
  122.  
  123. }
  124.  
  125. do {
  126. puzzle[2][1]=(rand()%4)+1;
  127. }while((puzzle[2][1]==puzzle[2][0])||(puzzle[2][1]==puzzle[3][0])||(puzzle[2][1]==puzzle[3][0])||(puzzle[2][1]==puzzle[0][1])||(puzzle[2][1]==puzzle[1][1])||(puzzle[2][1]==puzzle[2][3]));
  128. {
  129.  
  130. }
  131.  
  132. do {
  133. puzzle[2][2]=(rand()%4)+1;
  134. }while((puzzle[2][2]==puzzle[3][2])||(puzzle[2][2]==puzzle[3][3])||(puzzle[2][2]==puzzle[2][3])||(puzzle[2][2]==puzzle[0][2])||(puzzle[2][2]==puzzle[1][2])||(puzzle[2][2]==puzzle[2][0])||(puzzle[2][2]==puzzle[2][1]));
  135. {
  136.  
  137. }
  138. if (puzzle[2][2]!=0)
  139. {
  140. break;
  141. }
  142. }
  143.  
  144.  
  145. // Es mostra el sudoku complert
  146.  
  147. cout <<""<<endl;
  148. cout <<"Sudoku"<<endl;
  149.  
  150. cout <<puzzle[0][0]<<" "<<puzzle[0][1]<<" "<<puzzle[0][2]<<" "<<puzzle[0][3]<<endl;
  151. cout <<puzzle[1][0]<<" "<<puzzle[1][1]<<" "<<puzzle[1][2]<<" "<<puzzle[1][3]<<endl;
  152. cout <<puzzle[2][0]<<" "<<puzzle[2][1]<<" "<<puzzle[2][2]<<" "<<puzzle[2][3]<<endl;
  153. cout <<puzzle[3][0]<<" "<<puzzle[3][1]<<" "<<puzzle[3][2]<<" "<<puzzle[3][3]<<endl;
  154.  
  155.  
  156. cout <<""<<endl;
  157.  
  158.  
  159. cout <<puzzle[0][0]<<" "<<puzzle[0][1]<<" "<<puzzle[0][2]<<" "<<puzzle[0][3]<<endl;
  160. cout <<puzzle[1][0]<<" "<<"x"<<" "<<puzzle[1][2]<<" "<<puzzle[1][3]<<endl;
  161. cout <<puzzle[2][0]<<" "<<puzzle[2][1]<<" "<<"y"<<" "<<puzzle[2][3]<<endl;
  162. cout <<"z"<<" "<<puzzle[3][1]<<" "<<puzzle[3][2]<<" "<<puzzle[3][3]<<endl;
  163.  
  164. cout <<""<<endl;
  165. while (xval!=puzzle[1][1]){cout << "Quin \x082s el valor de x?\n";
  166. cin >> xval;
  167. if (xval==puzzle[1][1]) {
  168. cout << "Felicitats, has encertat\n";
  169. break;
  170. }
  171.  
  172. }
  173.  
  174. cout <<""<<endl;
  175. while (xval!=puzzle[2][2]){cout << "Quin \x082s el valor de y?\n";
  176. cin >> xval;
  177. if (xval==puzzle[2][2]) {
  178. cout << "Felicitats, has encertat\n";
  179. break;
  180. }
  181.  
  182. }
  183. cout <<""<<endl;
  184. while (xval!=puzzle[3][0]){cout << "Quin \x082s el valor de z?\n";
  185. cin >> xval;
  186. if (xval==puzzle[3][0]) {
  187. cout << "Felicitats, has encertat\n";
  188. break;
  189. }
  190.  
  191. }
  192. cout <<""<<endl;
  193. // Es procedeix a esborrar una quantitat de nombres definida per l'usuari
  194.  
  195. cout << "Introdueix quants n\x0A3meros vols esborrar\n";
  196. cin >> ninc;
  197.  
  198.  
  199. }
What happens with Dev? Crashes? Doesn't compile?
In Dev the code compiles but when i run it nothing appears in MS-DOS

Thank u 4 help
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 12
Reputation: Srynx is an unknown quantity at this point 
Solved Threads: 0
Srynx Srynx is offline Offline
Newbie Poster

Re: Problem with loops

 
0
  #7
Dec 8th, 2007
Hi,
  1. // Sudoku
  2.  
  3. #include<stdio.h>
  4. #include<stdlib.h>
  5. #include<time.h>
  6. #include<iostream.h>
  7. #define MAX_FILES 10
  8. #define MAX_COLUMNES 10
  9.  
  10. //Factorial of dimensions
  11. int suma(int dimensions){
  12.  
  13. if (dimensions<2) return dimensions;
  14. return dimensions+suma(dimensions-1);
  15.  
  16. }
  17.  
  18.  
  19. int main(){
  20.  
  21. double a[MAX_ROWS][MAX_COLUMNS];
  22. int dimensions=0;
  23. int i,j;
  24. int q;
  25. int suma(int);
  26. int k;
  27.  
  28. cout <<"How many columns or rows there are in the sudoku?\n";
  29. cin >> dimensions;
  30.  
  31. q=dimensions/2;
  32. k=0;
  33.  
  34. //introducció dels elements del sudoku
  35.  
  36.  
  37. while ((a[i][k]=!suma(dimensions))||(a[k][j]=!suma(dimensions))){
  38.  
  39.  
  40.  
  41. //first box
  42. do{
  43. for (i=0;i<q;i++){
  44. for(j=0;j<q;j++){
  45. a[i][j]=(rand()%dimensions)+1;
  46. }
  47. }
  48.  
  49. }while ((a[i][j])=!suma(dimensions));
  50.  
  51.  
  52. //second box
  53. do{
  54. for (i=(q-1);i<dimensions;i++){
  55. for(j=0;j<q;j++){
  56. a[i][j]=(rand()%dimensions)+1;
  57. }
  58. }
  59. }while ((a[i][j])=!suma(dimensions));
  60.  
  61.  
  62. //third box
  63. do{
  64. for (i=0;i<q;i++){
  65. for(j=(q-1);j<dimensions;j++){
  66. a[i][j]=(rand()%dimensions)+1;
  67. }
  68. }
  69. }while ((a[i][j])=!suma(dimensions));
  70.  
  71.  
  72. //fourth box
  73. do{
  74. for (i=(q-1);i<dimensions;i++){
  75. for(j=(q-1);j<dimensions;j++){
  76. a[i][j]=(rand()%dimensions)+1;
  77. }
  78. }
  79. }while ((a[i][j])=!suma(dimensions));
  80.  
  81.  
  82. if ((a[i][k]==suma(dimensions))&&(a[k][j]==suma(dimensions))) {
  83. break;
  84. }
  85.  
  86.  
  87.  
  88.  
  89. }
  90.  
  91. // All sudoku is showed
  92. for (i=(q-1);i<dimensions;i++){
  93. for(j=(q-1);j<dimensions;j++){
  94. cout <<""<< a[i,j] <<endl;
  95. }
  96. }
  97.  
  98.  
  99. }
I've used sum of all members of a box, row or column should be sudoku's dimensions factorial.
However I get a runtime error when I run the code.
How to solve it?
Thank you
Last edited by Ancient Dragon; Dec 8th, 2007 at 6:51 pm. Reason: add line numbers -- this is not a rules violation of any kind, just edited for convenience
Reply With Quote Quick reply to this message  
Join Date: Jun 2007
Posts: 2,462
Reputation: zandiago is on a distinguished road 
Solved Threads: 25
Featured Poster
zandiago's Avatar
zandiago zandiago is offline Offline
Nearly a Posting Maven

Re: Problem with loops

 
0
  #8
Dec 8th, 2007
Please post the error that you get.
I shot the sheriff....but I didn't shoot the deputy
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 12
Reputation: Srynx is an unknown quantity at this point 
Solved Threads: 0
Srynx Srynx is offline Offline
Newbie Poster

Re: Problem with loops

 
0
  #9
Dec 8th, 2007
I just get an error at runtime when I input how many columns or rows are there in the sudoku. Windows XP warn me to send an errors report and I only know it's a dwin.exe error.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,171
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: 1439
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Most Valuable Poster

Re: Problem with loops

 
0
  #10
Dec 8th, 2007
It doesn't run because it won't compiler -- you have some syntax errors in the code you posted. For example, line 21 is wrong because MAX_ROWS and MAX_COLUMNS have not been defined.

>>"How many columns or rows there are in the sudoku?\n";
Well, which are you asking us to input -- the number of columns or the number of rows? If you want both then you have to have an integer for each one because they can not be both in the same integer, like this:
  1. cout <<"Enter the number of columns and rows in the sudoku?\n";
  2. cin >> columns >> rows;
Last edited by Ancient Dragon; Dec 8th, 2007 at 6:57 pm.
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