Join 136,102 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,677 people online right now. Registration is fast and FREE... Join Now!
Hi, i'm making a 4x4 sudoku and i need to randomly pick a determinate number of items from a set in order to choose some numbers to erase. Later the user have to guess these numbers. I've already done the sudoku and I've posted the code here:
// The program choose and erase a number of items determinated by the user
printf("Enter how many items do you want to erase, please\n"); scanf("%s",ninc);
Now I have to erase an amount of numbers determinated by the user. For example i have this set int puzzle[5] = { a1,b1,c1,d1,a2 }; and i want to randomly pick 2 of these items in order to erase them. (final set will contain all 16 numbers) How to do that? Is there an specific function do that? Thank you
you can use memcpy to copy the memory following the positions you want to erase into the current position. that will result in those values being removed from the array, but you'd also have to set the end value(s) of the array to zero so that they don't show up. e.g. if you want to erase positions 3 and 5 from an array source that is 16 elements long:
if you actually want to make the array smaller, you need to first allocate it at runtime using malloc, then resize it using realloc. but remember, with both of these approaches, you're not simply leaving an empty spot in the array - you're removing that position completely.
I mean by erase leaving an empty place in my sudoku but no completely removing the position so later user have to input the solution and program will compare the input with the value (hiden). So my question is how to choose randomly a determinate number of values entered by the user in order to just don't printf them when the program show the sudoku again. I've thought about to take the number of values to "erase" entered by the user (n) and printf 16-n variables choosed randomly. I've thougt about randomly choose these 16-n variables from a set containg all 16 variablesn but anyway i'm open to any other solucion bearing in mind i'm a begginer and i don't know how to work with arrays very well so i prefer to don't use them yet. Thank u
Hi, I'm working in a 4x4 sudoku solver and i'm a beginner in c++. Variables are: a1 b1 c1 d1 a2 b2 c2 d2 a3 b3 c3 d3 a4 b4 c4 d4 First i wrote that program
your method of constructing the sudoku array doesn't guarantee that you'll be able to find a solution that works. you're just assigning a random value to a cell, then trying to fit the other numbers in around it. this does not guarantee that a valid puzzle can be found; it just guarantees that you won't create an invalid puzzle, since the while loop ion the assignment will never exit.
you may want to do a search for constructing latin squares. they have many of the same properties as valid sudoku puzzles.
you may also want to do a test in your assignment statements that causes the assignment to start over if an infinite loop occurs. this can be as simple as testing whether any of 1-4 can be validly assigned.
and since this thread is directly related to a previous post, you should probably just reply in the existing thread - a moderator will probably merge the two topics eventually.
Hi, I've tried to write the code in a most effective way using that sum of all members of a box, row or column should be sudoku's dimensions factorial sum. However, when the code prints the sudoku's solutions it print one value for all members of array: -9.25596e+061. How to solve that? Thank you! My code:
CODE
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.}
Well, I get results, but nothing useful. I suggest you revisit your logic routines and come up with a different way. Or at least find a way to make it more readable. If you have a mistake in your if statements you might never find it.
You're getting closer. However, there are still a number of issues. Have you ever heard the phrase, "Monkeys typing Shakespeare?" It's a little like that.
In your grid, you will need four ones, four twos, four threes, and four fours to have a valid grid at all. So, in addition to looping until you have a valid sudoku, you are also looping until you have four of each number. That's quite an order.
The other problem is that your check for valid allows invalid results. While (1+2+3+4=10), so does (1+1+4+4), etc. Here's an example that's valid according to the rule of 10:
// Initialization for (int row=0; row<ROWS; row++) { for(int col=0; col<COLS; col++) { a[row][col]=row+1; } } showBoard(a); // this is the initialized board
int count=0; while(!isValid(a)) { swap(a); count++; }
cout <<"count = " << count <<endl;
showBoard(a); return 0; }
The reason this works is that we pre populate the array with the numbers we want. They're in the wrong places, but that's fine. We then start swapping two positions at random. In this way, when you come up with a valid test condition, it should work.