| | |
Problem with loops
![]() |
•
•
Join Date: Oct 2007
Posts: 12
Reputation:
Solved Threads: 0
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
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:
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
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
C++ Syntax (Toggle Plain Text)
//sudoku.cpp: #include<stdlib.h> #include<math.h> #include<stdio.h> #include<time.h> #include<algorithm> void main(){ srand(time(0)); int a1; int b1; int c1; int d1; int a2; int b2; int c2; int d2; int a3; int b3; int c3; int d3; int a4; int b4; int c4; int d4; srand(time(0)); // 1st row a1=rand()*5/32768; printf("a1 = %d\n",a1); while((b1!=0)||(b1=a1)){ b1=rand()*5/32768; if ((b1!=0)&&(b1!=a1)){ break; printf("b1 %d\n",b1); }} printf("b1 = %d\n",b1); while((c1!=0)||(c1=a1)||(c1=b1)){ c1=rand()*5/32768; if ((c1!=0)&&(c1!=a1)&&(c1!=b1)){ break; }} printf("c1 = %d\n",c1); while((d1!=0)||(d1=a1)||(d1=b1)||(d1=c1)){ d1=rand()*5/32768; if ((d1!=0)&&(d1!=a1)&&(d1!=b1)&&(d1!=c1)){ break; }} printf("d1 = %d\n",d1); // Column A while((a2!=0)||(a2=a1)||(a2=b1)){ a2=rand()*5/32768; if ((a2!=0)&&(a2!=a1)&&(a2!=b1)){ break; }} printf("a2 = %d\n",a2); while((a3!=0)||(a3=a1)||(a3=a2)){ a3=rand()*5/32768; if ((a3!=0)&&(a3!=a1)&&(a3!=a2)){ break; }} printf("a3 = %d\n",a3); while((a4!=0)||(a4=a1)||(a4=a2)||(a4=a3)){ a4=rand()*5/32768; if ((a4!=0)&&(a4!=a1)&&(a4!=a2)&&(a4!=a3)){ break; }} printf("a4 = %d\n",a4); // Column D while((d2!=0)||(d2=c1)||(d2=d1)||(d2=a2)){ d2=rand()*5/32768; if ((d2!=0)&&(d2!=c1)&&(d2!=d1)&&(d2!=a2)){ break; }} printf("d2 = %d\n",d2); while((d3!=0)||(d3=d1)||(d3=d2)||(d3=a3)){ d3=rand()*5/32768; if ((d3!=0)&&(d3!=d1)&&(d3!=d2)&&(d3!=a3)){ break; }} printf("d3 = %d\n",d3); while((d4!=0)||(d4=d1)||(d4=d2)||(d4=d3)||(d4=a4)){ d4=rand()*5/32768; if ((d4!=0)&&(d4!=d1)&&(d4!=d2)&&(d4!=d3)&&(d4!=a4)){ break; }} printf("d4 = %d\n",d4); // 4th row while((b4!=0)||(b4=a3)||(b4=a4)||(b4=b1)||(b4=d4)){ b4=rand()*5/32768; if ((b4!=0)&&(b4!=a3)&&(b4!=a4)&&(b4!=b1)&&(b4!=d4)){ break; }} printf("b4 = %d\n",b4); while((c4!=0)||(c4=d3)||(c4=d4)||(c4=c1)||(c4=a4)||(c4=b4)){ c4=rand()*5/32768; if ((c4!=0)&&(c4!=d3)&&(c4!=d4)&&(c4!=c1)&&(c4!=a4)){ break; }} printf("c4 = %d\n",c4); // Central Square while((b2!=0)||(b2=a1)||(b2=b1)||(b2=a2)||(b2=d2)||(b2=b4)){ b2=rand()*5/32768; if ((b2!=0)&&(b2!=a1)&&(b2!=b1)&&(b2!=a2)&&(b2!=d2)&&(b2!=b4)){ break; }} printf("b2 = %d\n",b2); while((c2!=0)||(c2=c1)||(c2=d1)||(c2=a2)||(c2=b2)||(c2=d2)||(c2=c4)){ c2=rand()*5/32768; if ((c2!=0)&&(c2!=c1)&&(c2!=d1)&&(c2!=a2)&&(c2!=b2)&&(c2!=d2)&&(c2!=c4)){ break; }} printf("c2 = %d\n",c2); while((b3!=0)||(b3=a3)||(b3=a4)||(b3=b4)||(b3=b1)||(b3=b2||(b3=d3))){ b3=rand()*5/32768; if ((b3!=0)&&(b3!=a3)&&(b3!=a4)&&(b3!=b4)&&(b3!=b1)&&(b3!=b2)&&(b3!=d3)){ break; }} printf("b3 = %d\n",b3); while((c3!=0)||(c3=c4)||(c3=d4)||(c3=d3)||(c3=c1)||(c3=c2)||(c3=a3)||(c3=b3)){ c3=rand()*5/32768; if ((c3!=0)&&(c3!=c4)&&(c3!=d4)&&(c3!=d3)&&(c3!=c1)&&(c3!=c2)&&(c3!=a3)&&(c3!=b3)){ break; }} printf("c3 = %d\n",c3); }
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:
C++ Syntax (Toggle Plain Text)
//sudoku.cpp: #include<stdlib.h> #include<math.h> #include<stdio.h> #include<time.h> #include<algorithm> #include<iostream> using std::cout; using std::endl; using std::cin; void main(){ int a1; int b1; int c1; int d1; int a2; int b2; int c2; int d2; int a3; int b3; int c3; int d3; int a4; int b4; int c4; int d4; int xval; srand(time(0)); c3=0; while(c3==0) { // 1st row a1=(rand()%4)+1; cout <<"a1 = "<<a1<<endl; do { b1=(rand()%4)+1; }while(b1==a1); { cout <<"b1 = "<<b1<<endl; } do { c1=(rand()%4)+1; }while((c1==a1)||(c1==b1)); { cout <<"c1 = "<<c1<<endl; } do { d1=(rand()%4)+1; }while((d1==a1)||(d1==b1)||(d1==c1)); { cout <<"d1 = "<<d1<<endl; } // Column A do { a2=(rand()%4)+1; }while((a2==a1)||(a2==b1)); { cout <<"a2 = "<<a2<<endl; } do { a3=(rand()%4)+1; }while((a3==a1)||(a3==a2)); { cout <<"a3 = "<<a3<<endl; } do { a4=(rand()%4)+1; }while((a4==a1)||(a4==a2)||(a4==a3)); { cout <<"a4 = "<<a4<<endl; } // Column D do { d2=(rand()%4)+1; }while((d2==c1)||(d2==d1)||(d2==a2)); { cout <<"d2 = "<<d2<<endl; } do { d3=(rand()%4)+1; }while((d3==d1)||(d3==d2)||(d3==a3)); { cout <<"d3 = "<<d3<<endl; } do { d4=(rand()%4)+1; }while((d4==d1)||(d4==d2)||(d4==d3)||(d4==a4)); { cout <<"d4 = "<<d4<<endl; } // 4th row do { b4=(rand()%4)+1; }while((b4==a3)||(b4==a4)||(b4==b1)||(b4==d4)); { cout <<"b4 = "<<b4<<endl; } do { c4=(rand()%4)+1; }while((c4==d3)||(c4==d4)||(c4==c1)||(c4==a4)||(c4==b4)); { cout <<"c4 = "<<c4<<endl; } // Central square do { b2=(rand()%4)+1; }while((b2==a1)||(b2==b1)||(b2==a2)||(b2==d2)||(b2==b4)); { cout <<"b2 = "<<b2<<endl; } do { c2=(rand()%4)+1; }while((c2==c1)||(c2==d1)||(c2==a2)||(c2==b2)||(c2==d2)||(c2==c4)); { cout <<"c2 = "<<c2<<endl; } do { b3=(rand()%4)+1; }while((b3==a3)||(b3==a4)||(b3==b4)||(b3==b1)||(b3==b2)||(b3==d3)); { cout <<"b3 = "<<b3<<endl; } do { c3=(rand()%4)+1; }while((c3==c4)||(c3==d4)||(c3==d3)||(c3==c1)||(c3==c2)||(c3==a3)||(c3==b3)); { cout <<"c3 = "<<c3<<endl; } if (c3!=0) { break; } } // It shows all sudoku cout <<""<<endl; cout <<"Sudoku"<<endl; cout <<a1<<" "<<b1<<" "<<c1<<" "<<d1<<endl; cout <<a2<<" "<<b2<<" "<<c2<<" "<<d2<<endl; cout <<a3<<" "<<b3<<" "<<c3<<" "<<d3<<endl; cout <<a4<<" "<<b4<<" "<<c4<<" "<<d4<<endl; cout <<""<<endl; cout <<a1<<" "<<b1<<" "<<c1<<" "<<d1<<endl; cout <<a2<<" "<<"x"<<" "<<c2<<" "<<d2<<endl; cout <<a3<<" "<<b3<<" "<<"y"<<" "<<d3<<endl; cout <<"z"<<" "<<b4<<" "<<c4<<" "<<d4<<endl; cout <<""<<endl; while (xval!=b2){cout << "What's x value?\n"; cin >> xval; if (xval==b2) { cout << "OK\n"; break; } } cout <<""<<endl; while (xval!=c3){cout << "What's y value?\n"; cin >> xval; if (xval==c3) { cout << "OK\n"; break; } } cout <<""<<endl; while (xval!=a4){cout << "What's x value??\n"; cin >> xval; if (xval==a4) { cout << "OK\n"; break; } } 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
>>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.
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.
C++ Syntax (Toggle Plain Text)
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.
•
•
•
•
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.
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
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
•
•
Join Date: Oct 2007
Posts: 12
Reputation:
Solved Threads: 0
Hi,
here the corrected code:
In Dev the code compiles but when i run it nothing appears in MS-DOS
Thank u 4 help
•
•
•
•
can you post the corrected code??
i.e. the code after using matrices??
C++ Syntax (Toggle Plain Text)
//sudoku.cpp: #include<stdlib.h> #include<math.h> #include<stdio.h> #include<time.h> #include<algorithm> #include<iostream> using std::cout; using std::endl; using std::cin; void main(){ int ninc=0; int xval; int puzzle[4][4]; srand(time(0)); puzzle[2][2]=0; while(puzzle[2][2]==0) { // Fila 1 puzzle[0][0]=(rand()%4)+1; do { puzzle[0][1]=(rand()%4)+1; }while(puzzle[0][1]==puzzle[0][0]); { } do { puzzle[0][2]=(rand()%4)+1; }while((puzzle[0][2]==puzzle[0][0])||(puzzle[0][2]==puzzle[0][1])); { } do { puzzle[0][3]=(rand()%4)+1; }while((puzzle[0][3]==puzzle[0][0])||(puzzle[0][3]==puzzle[0][1])||(puzzle[0][3]==puzzle[0][2])); { } // Columna A do { puzzle[1][0]=(rand()%4)+1; }while((puzzle[1][0]==puzzle[0][0])||(puzzle[1][0]==puzzle[0][1])); { } do { puzzle[2][0]=(rand()%4)+1; }while((puzzle[2][0]==puzzle[0][0])||(puzzle[2][0]==puzzle[1][0])); { } do { puzzle[3][0]=(rand()%4)+1; }while((puzzle[3][0]==puzzle[0][0])||(puzzle[3][0]==puzzle[0][1])||(puzzle[3][0]==puzzle[2][0])); { } // Columna D do { puzzle[1][3]=(rand()%4)+1; }while((puzzle[1][3]==puzzle[0][2])||(puzzle[1][3]==puzzle[0][3])||(puzzle[1][3]==puzzle[1][0])); { } do { puzzle[2][3]=(rand()%4)+1; }while((puzzle[2][3]==puzzle[0][3])||(puzzle[2][3]==puzzle[1][3])||(puzzle[2][3]==puzzle[2][0])); { } do { puzzle[3][3]=(rand()%4)+1; }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])); { } // Fila 4 do { puzzle[3][1]=(rand()%4)+1; }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])); { } do { puzzle[3][2]=(rand()%4)+1; }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])); { } // Quadrat central do { puzzle[1][1]=(rand()%4)+1; }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])); { } do { puzzle[1][2]=(rand()%4)+1; }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])); { } do { puzzle[2][1]=(rand()%4)+1; }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])); { } do { puzzle[2][2]=(rand()%4)+1; }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])); { } if (puzzle[2][2]!=0) { break; } } // Es mostra el sudoku complert cout <<""<<endl; cout <<"Sudoku"<<endl; cout <<puzzle[0][0]<<" "<<puzzle[0][1]<<" "<<puzzle[0][2]<<" "<<puzzle[0][3]<<endl; cout <<puzzle[1][0]<<" "<<puzzle[1][1]<<" "<<puzzle[1][2]<<" "<<puzzle[1][3]<<endl; cout <<puzzle[2][0]<<" "<<puzzle[2][1]<<" "<<puzzle[2][2]<<" "<<puzzle[2][3]<<endl; cout <<puzzle[3][0]<<" "<<puzzle[3][1]<<" "<<puzzle[3][2]<<" "<<puzzle[3][3]<<endl; cout <<""<<endl; cout <<puzzle[0][0]<<" "<<puzzle[0][1]<<" "<<puzzle[0][2]<<" "<<puzzle[0][3]<<endl; cout <<puzzle[1][0]<<" "<<"x"<<" "<<puzzle[1][2]<<" "<<puzzle[1][3]<<endl; cout <<puzzle[2][0]<<" "<<puzzle[2][1]<<" "<<"y"<<" "<<puzzle[2][3]<<endl; cout <<"z"<<" "<<puzzle[3][1]<<" "<<puzzle[3][2]<<" "<<puzzle[3][3]<<endl; cout <<""<<endl; while (xval!=puzzle[1][1]){cout << "Quin \x082s el valor de x?\n"; cin >> xval; if (xval==puzzle[1][1]) { cout << "Felicitats, has encertat\n"; break; } } cout <<""<<endl; while (xval!=puzzle[2][2]){cout << "Quin \x082s el valor de y?\n"; cin >> xval; if (xval==puzzle[2][2]) { cout << "Felicitats, has encertat\n"; break; } } cout <<""<<endl; while (xval!=puzzle[3][0]){cout << "Quin \x082s el valor de z?\n"; cin >> xval; if (xval==puzzle[3][0]) { cout << "Felicitats, has encertat\n"; break; } } cout <<""<<endl; // Es procedeix a esborrar una quantitat de nombres definida per l'usuari cout << "Introdueix quants n\x0A3meros vols esborrar\n"; cin >> ninc; }
•
•
•
•
What happens with Dev? Crashes? Doesn't compile?
Thank u 4 help
•
•
Join Date: Oct 2007
Posts: 12
Reputation:
Solved Threads: 0
Hi,
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
c++ Syntax (Toggle Plain Text)
// Sudoku #include<stdio.h> #include<stdlib.h> #include<time.h> #include<iostream.h> #define MAX_FILES 10 #define MAX_COLUMNES 10 //Factorial of dimensions int suma(int dimensions){ if (dimensions<2) return dimensions; return dimensions+suma(dimensions-1); } int main(){ double a[MAX_ROWS][MAX_COLUMNS]; int dimensions=0; int i,j; int q; int suma(int); int k; cout <<"How many columns or rows there are in the sudoku?\n"; cin >> dimensions; q=dimensions/2; k=0; //introducció dels elements del sudoku while ((a[i][k]=!suma(dimensions))||(a[k][j]=!suma(dimensions))){ //first box do{ for (i=0;i<q;i++){ for(j=0;j<q;j++){ a[i][j]=(rand()%dimensions)+1; } } }while ((a[i][j])=!suma(dimensions)); //second box do{ for (i=(q-1);i<dimensions;i++){ for(j=0;j<q;j++){ a[i][j]=(rand()%dimensions)+1; } } }while ((a[i][j])=!suma(dimensions)); //third box do{ for (i=0;i<q;i++){ for(j=(q-1);j<dimensions;j++){ a[i][j]=(rand()%dimensions)+1; } } }while ((a[i][j])=!suma(dimensions)); //fourth box do{ for (i=(q-1);i<dimensions;i++){ for(j=(q-1);j<dimensions;j++){ a[i][j]=(rand()%dimensions)+1; } } }while ((a[i][j])=!suma(dimensions)); if ((a[i][k]==suma(dimensions))&&(a[k][j]==suma(dimensions))) { break; } } // All sudoku is showed for (i=(q-1);i<dimensions;i++){ for(j=(q-1);j<dimensions;j++){ cout <<""<< a[i,j] <<endl; } } }
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
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:
>>"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:
C++ Syntax (Toggle Plain Text)
cout <<"Enter the number of columns and rows in the sudoku?\n"; 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.
![]() |
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 auto based binary bitmap c++ c/c++ calculator challenge char class classes code coding compile console conversion count delay-loading delete deploy desktop developer directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game garbage givemetehcodez graph gui hmenu homeworkhelp homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer primenumbersinrange problem program programming project python random read recursion reference rpg sockets string strings temperature template templates test text text-file tree url variable vector video win32 windows winsock word wordfrequency wxwidgets






