//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); }
//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;
int matrix[4][4];
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.
can you post the corrected code??
i.e. the code after using matrices??
//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?
// 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; } } }
cout <<"Enter the number of columns and rows in the sudoku?\n"; cin >> columns >> rows;
| DaniWeb Message | |
| Cancel Changes | |