Problem with loops

Please support our C++ advertiser: Intel Parallel Studio Home
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

Re: Problem with loops

 
0
  #11
Dec 10th, 2007
Hi,
  1. 1. // Sudoku
  2. 2.
  3. 3.
  4. 4. #include<stdio.h>
  5. 5. #include<stdlib.h>
  6. 6. #include<time.h>
  7. 7. #include<iostream>
  8. 8. #define MAX_FILES 10
  9. 9. #define MAX_COLUMNES 10
  10. 10.
  11. 11. using namespace std;
  12. 12.
  13. 13.
  14. 14. int suma(int);
  15. 15.
  16. 16. //factorial sum
  17. 17. int suma(int dimensions){
  18. 18.
  19. 19. if (dimensions<2) return dimensions;
  20. 20. return dimensions+suma(dimensions-1);
  21. 21.
  22. 22. }
  23. 23.
  24. 24.
  25. 25. void main(){
  26. 26.
  27. 27. double a[MAX_ROWS][MAX_COLUMNS];
  28. 28. int dimensions=0;
  29. 29. int i,j;
  30. 30. int rows, columns;
  31. 31. int q;
  32. 32. int k;
  33. 33.
  34. 34. rows=0;
  35. 35. columns=0;
  36. 36. dimensions=0;
  37. 37. i=0;j=0;k=0;
  38. 38.
  39. 39. srand(time(0));
  40. 40.
  41. 41.
  42. 42.
  43. 43. cout <<"How many cloumns and rows are there in the sudoku? (put the value 3 times, 44. please):\n";
  44. 45. cin >> rows >> columns >> dimensions;
  45. 46.
  46. 47.
  47. 48. q=rows/2;
  48. 49.
  49. 50.
  50. 51.
  51. 52. while ((a[i][k]=!suma(dimensions))||(a[k][j]=!suma(dimensions))){
  52. 53.
  53. 54.
  54. 55.
  55. 56. //first box
  56. 57. do{
  57. 58. for (i=0;i<q;i++){
  58. 59. for(j=0;j<q;j++){
  59. 60. a[i][j]=(rand()%dimensions)+1;
  60. 61. }
  61. 62. }
  62. 63.
  63. 64. }while ((a[i][j])=!suma(dimensions));
  64. 65.
  65. 66.
  66. 67. //second box
  67. 68. do{
  68. 69. for (i=(q-1);i<rows;i++){
  69. 70. for(j=0;j<q;j++){
  70. 71. a[i][j]=(rand()%dimensions)+1;
  71. 72. }
  72. 73. }
  73. 74. }while ((a[i][j])=!suma(dimensions));
  74. 75.
  75. 76.
  76. 77. //third box
  77. 78. do{
  78. 79. for (i=0;i<q;i++){
  79. 80. for(j=(q-1);j<columns;j++){
  80. 81. a[i][j]=(rand()%dimensions)+1;
  81. 82. }
  82. 83. }
  83. 84. }while ((a[i][j])=!suma(dimensions));
  84. 85.
  85. 86.
  86. 87. //fourth box
  87. 88. do{
  88. 89. for (i=(q-1);i<rows;i++){
  89. 90. for(j=(q-1);j<columns;j++){
  90. 91. a[i][j]=(rand()%dimensions)+1;
  91. 92. }
  92. 93. }
  93. 94. }while ((a[i][j])=!suma(dimensions));
  94. 95.
  95. 96.
  96. 97. if ((a[i][k]==suma(dimensions))&&(a[k][j]==suma(dimensions))) {
  97. 98. break;
  98. 99. }
  99. 100.
  100. 101.
  101. 102.
  102. 103.
  103. 104.
  104. 105.
  105. 106.
  106. 107.
  107. 108.
  108. 109.
  109. 110. }
  110. 111.
  111. 112. // All sudoku is showed
  112. 113. for (i=0;i<rows;i++){
  113. 114. for(j=0;j<columns;j++){
  114. 115. cout <<""<< a[i][j] <<endl;
  115. 116. }
  116. 117. }
  117. 118.
  118. 119.
  119. 120.}

With defining columns and rows separately and some more changes (incializin 0 to i and j) the code works but when it prits the sudoku's solutions it print one value for all members of array -9.25596e+061
How to solve it?
Thanks
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
  #12
Dec 18th, 2007
I've wrote a new code. I think it should work but it doesn't give any results when I run the program.
Can you help me telling me what's wrong, please?
  1. // Sudoku
  2.  
  3. #include<stdio.h>
  4. #include<fstream>
  5. #include<stdlib.h>
  6. #include<time.h>
  7. #include<iostream>
  8. #define MAX_FILES 10
  9. #define MAX_COLUMNES 10
  10.  
  11. using namespace std;
  12.  
  13. void main(){
  14. int a[MAX_FILES][MAX_COLUMNES];
  15. int i=0;
  16. int j=0;
  17. int dimensions=4;
  18. int rows=4;
  19. int columns=4;
  20.  
  21. // Initialization
  22. for (i=0;i<rows;i++){
  23. for(j=0;j<columns;j++){
  24. a[i][j]=0;
  25. }
  26. }
  27.  
  28.  
  29. time_t seconds;
  30. srand((unsigned)time(&seconds));
  31.  
  32.  
  33. while(((a[0][0]+a[0][1]+a[0][2]+a[0][3])!=10)||((a[1][0]+a[1][1]+a[1][2]+a[1][3])!=10)||((a[2][0]+a[2][1]+a[2][2]+a[2][3])!=10)||((a[3][0]+a[3][1]+a[3][2]+a[3][3])!=10)||((a[0][0]+a[1][0]+a[2][0]+a[3][0])!=10)||((a[0][1]+a[1][1]+a[2][1]+a[3][1])!=10)||((a[0][2]+a[1][2]+a[2][2]+a[3][2])!=10)||((a[0][3]+a[1][3]+a[2][3]+a[3][3])!=10)||((a[0][0]+a[0][1]+a[1][0]+a[1][1])!=10)||((a[2][0]+a[2][1]+a[3][0]+a[3][1])!=10)||((a[0][2]+a[0][3]+a[1][2]+a[1][3])!=10)||((a[2][2]+a[2][3]+a[3][2]+a[3][3])!=10)){
  34.  
  35. for (i=0;i<rows;i++){
  36. for(j=0;j<columns;j++){
  37. a[i][j]=(rand()%dimensions)+1;
  38. }
  39. }
  40.  
  41. if(((a[0][0]+a[0][1]+a[0][2]+a[0][3])==10)&&((a[1][0]+a[1][1]+a[1][2]+a[1][3])==10)&&((a[2][0]+a[2][1]+a[2][2]+a[2][3])==10)&&((a[3][0]+a[3][1]+a[3][2]+a[3][3])==10)&&((a[0][0]+a[1][0]+a[2][0]+a[3][0])==10)&&((a[0][1]+a[1][1]+a[2][1]+a[3][1])==10)&&((a[0][2]+a[1][2]+a[2][2]+a[3][2])==10)&&((a[0][3]+a[1][3]+a[2][3]+a[3][3])==10)&&((a[0][0]+a[0][1]+a[1][0]+a[1][1])==10)&&((a[2][0]+a[2][1]+a[3][0]+a[3][1])==10)&&((a[0][2]+a[0][3]+a[1][2]+a[1][3])==10)&&((a[2][2]+a[2][3]+a[3][2]+a[3][3])==10)){
  42.  
  43.  
  44. cout <<"First box"<<endl;
  45. cout <<""<< a[0][0] <<endl;
  46. cout <<""<< a[0][1] <<endl;
  47. cout <<""<< a[1][0] <<endl;
  48. cout <<""<< a[1][1] <<endl;
  49. cout <<"----------------"<<endl;
  50. cout <<"Second box"<<endl;
  51. cout <<""<< a[2][0] <<endl;
  52. cout <<""<< a[2][1] <<endl;
  53. cout <<""<< a[3][0] <<endl;
  54. cout <<""<< a[3][1] <<endl;
  55. cout <<"----------------"<<endl;
  56. cout <<"Thirth box"<<endl;
  57. cout <<""<< a[0][2] <<endl;
  58. cout <<""<< a[0][3] <<endl;
  59. cout <<""<< a[1][2] <<endl;
  60. cout <<""<< a[1][3] <<endl;
  61. cout <<"----------------"<<endl;
  62. cout <<"Fourth box"<<endl;
  63. cout <<""<< a[2][2] <<endl;
  64. cout <<""<< a[2][3] <<endl;
  65. cout <<""<< a[3][2] <<endl;
  66. cout <<""<< a[3][3] <<endl;
  67. cout <<"----------------"<<endl;
  68.  
  69. }
  70. }
  71.  
  72. }
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