| | |
Problem with loops
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Oct 2007
Posts: 12
Reputation:
Solved Threads: 0
Hi,
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
C++ Syntax (Toggle Plain Text)
1. // Sudoku 2. 3. 4. #include<stdio.h> 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. 14. int suma(int); 15. 16. //factorial sum 17. int suma(int dimensions){ 18. 19. if (dimensions<2) return dimensions; 20. return dimensions+suma(dimensions-1); 21. 22. } 23. 24. 25. void main(){ 26. 27. double a[MAX_ROWS][MAX_COLUMNS]; 28. int dimensions=0; 29. int i,j; 30. int rows, columns; 31. int q; 32. int k; 33. 34. rows=0; 35. columns=0; 36. dimensions=0; 37. i=0;j=0;k=0; 38. 39. srand(time(0)); 40. 41. 42. 43. cout <<"How many cloumns and rows are there in the sudoku? (put the value 3 times, 44. please):\n"; 45. cin >> rows >> columns >> dimensions; 46. 47. 48. q=rows/2; 49. 50. 51. 52. while ((a[i][k]=!suma(dimensions))||(a[k][j]=!suma(dimensions))){ 53. 54. 55. 56. //first box 57. do{ 58. for (i=0;i<q;i++){ 59. for(j=0;j<q;j++){ 60. a[i][j]=(rand()%dimensions)+1; 61. } 62. } 63. 64. }while ((a[i][j])=!suma(dimensions)); 65. 66. 67. //second box 68. do{ 69. for (i=(q-1);i<rows;i++){ 70. for(j=0;j<q;j++){ 71. a[i][j]=(rand()%dimensions)+1; 72. } 73. } 74. }while ((a[i][j])=!suma(dimensions)); 75. 76. 77. //third box 78. do{ 79. for (i=0;i<q;i++){ 80. for(j=(q-1);j<columns;j++){ 81. a[i][j]=(rand()%dimensions)+1; 82. } 83. } 84. }while ((a[i][j])=!suma(dimensions)); 85. 86. 87. //fourth box 88. do{ 89. for (i=(q-1);i<rows;i++){ 90. for(j=(q-1);j<columns;j++){ 91. a[i][j]=(rand()%dimensions)+1; 92. } 93. } 94. }while ((a[i][j])=!suma(dimensions)); 95. 96. 97. if ((a[i][k]==suma(dimensions))&&(a[k][j]==suma(dimensions))) { 98. break; 99. } 100. 101. 102. 103. 104. 105. 106. 107. 108. 109. 110. } 111. 112. // All sudoku is showed 113. for (i=0;i<rows;i++){ 114. for(j=0;j<columns;j++){ 115. cout <<""<< a[i][j] <<endl; 116. } 117. } 118. 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
•
•
Join Date: Oct 2007
Posts: 12
Reputation:
Solved Threads: 0
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?
Can you help me telling me what's wrong, please?
C++ Syntax (Toggle Plain Text)
// Sudoku #include<stdio.h> #include<fstream> #include<stdlib.h> #include<time.h> #include<iostream> #define MAX_FILES 10 #define MAX_COLUMNES 10 using namespace std; void main(){ int a[MAX_FILES][MAX_COLUMNES]; int i=0; int j=0; int dimensions=4; int rows=4; int columns=4; // Initialization for (i=0;i<rows;i++){ for(j=0;j<columns;j++){ a[i][j]=0; } } time_t seconds; srand((unsigned)time(&seconds)); 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)){ for (i=0;i<rows;i++){ for(j=0;j<columns;j++){ a[i][j]=(rand()%dimensions)+1; } } 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)){ cout <<"First box"<<endl; cout <<""<< a[0][0] <<endl; cout <<""<< a[0][1] <<endl; cout <<""<< a[1][0] <<endl; cout <<""<< a[1][1] <<endl; cout <<"----------------"<<endl; cout <<"Second box"<<endl; cout <<""<< a[2][0] <<endl; cout <<""<< a[2][1] <<endl; cout <<""<< a[3][0] <<endl; cout <<""<< a[3][1] <<endl; cout <<"----------------"<<endl; cout <<"Thirth box"<<endl; cout <<""<< a[0][2] <<endl; cout <<""<< a[0][3] <<endl; cout <<""<< a[1][2] <<endl; cout <<""<< a[1][3] <<endl; cout <<"----------------"<<endl; cout <<"Fourth box"<<endl; cout <<""<< a[2][2] <<endl; cout <<""<< a[2][3] <<endl; cout <<""<< a[3][2] <<endl; cout <<""<< a[3][3] <<endl; cout <<"----------------"<<endl; } } }
![]() |
Similar Threads
- hello i have a problem (VB.NET)
- problem with output values (Assembly)
- Loops (PHP)
- Words Into Array (C++)
- Help with Linked Lists with digits (C++)
- Theta Notation for algorithms question! (Computer Science)
- Please help, while loop wont end (C++)
- Help w/ for loop. (C++)
- Please Help!!! (Java)
- infinite loop... (C++)
Other Threads in the C++ Forum
- Previous Thread: Beginner needs help
- Next Thread: Trying to get a header file to compile with source files
| Thread Tools | Search this Thread |
api array arrays based beginner binary bitmap c++ c/c++ calculator char class classes code compile compiler console conversion count delete deploy desktop directshow dll download dynamic dynamiccharacterarray encryption error file forms fstream function functions game getline givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux list loop looping loops map math matrix memory news node output parameter pointer problem program programming project proxy python read recursion recursive reference return rpg string strings struct temperature template templates test text text-file tree unix url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets





