Sudoku Programmers Forum Index

 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister   ProfileProfile   Log inLog in          Games  Calendar

Log in to check your private messagesLog in to check your private messages   

HELP!!!what's wrong with my programme

 
Post new topic   Reply to topic    Sudoku Programmers Forum Index -> Setting sudoku
View previous topic :: View next topic  
Author Message
littleguy

Joined: 19 Jan 2007
Posts: 5
:
Location: Hong Kong

Items
PostPosted: Fri Jan 19, 2007 4:45 pm    Post subject: HELP!!!what's wrong with my programme Reply with quote

my english is vary bad. if there is any grammatical mistake, i'm sorry about that.

this programme is for generate a solution simply



#include <iostream>
#include <stdlib>
#include <time>


int temp[9][9],q=0, a=0, i=0,j=0,k=0,l=0,m=0, n=0, o=0,p=0, r=0, chance=0,chancer=0,chancec=0,chanceq=0, c1c=0,c1r=0,c1qi=0,c1qj=0;
char QUE[9][9];
bool crow[9][9],ccol[9][9],cq[9][9];

void checking(int i, int j, int n, int *chance) {
if (temp[i][j]==n)
*chance++; }

void checkingQ(int q, int n) {
int i=0,j=0;
switch (q) {
case 0 : o=0;
p=0;
break;
case 1 : o=0;
p=3;
break;
case 2 : o=0;
p=6;
break;
case 3 : o=3;
p=0;
break;
case 4 : o=3;
p=3;
break;
case 5 : o=3;
p=6;
break;
case 6 : o=6;
p=0;
break;
case 7 : o=6;
p=3;
break;
case 8 : o=6;
p=6;
break; }
for (i=o; i<(o+3); i++)
for (j=p; j<(p+3); j++) {
if (temp[i][j]==n) {
chanceq++;
c1qi=o;
c1qj=p; } } }


int main()
{ time_t rawtime;

for (i=0; i<9; i++)
for (j=0; j<9; j++) {
crow[i][j]=ccol[i][j]=cq[i][j]=false; temp[i][j]=0; }




for (a=0; a<40; a++) {
k=0; l=0;
for (q=0; q<9; q++) {
for (i=k; i<(k+3); i++)
for (j=l; j<(l+3); j++) {
rerand :
srand(time(NULL)+r++);
n=rand()%9;
cout << n << endl;
if (temp[i][j] == 0) {
if (crow[i][n]==false && ccol[n][j]==false && cq[q][n]==false) {
temp[i][j]=n;
crow[i][n]=true;
ccol[n][j]=true;
cq[q][n]=true; } } }
if (l+3<9)
l+=3;
else {
k+=3;
l=0; } } }

cout << '\n';
for (i=0; i<9; i++) {
for (j=0; j<9; j++)
cout << temp[i][j] << ' ';
cout << '\n'; }

k=0; l=0;
for (q=0; q<9; q++) {
for (i=k; i<(k+3); i++)
for (j=l; j<(l+3); j++) {

if (temp[i][j] == 0) {

for (o=0; o<9; o++) { <~I found that the problem may started at here, but i don't know what the problem is Sad
for (m=0; m<9; m++) {
checking(m,j,o,&chancec);
if (chancec==1)
c1c=o;
checking(i,m,o,&chancer);
if (chancer==1)
c1r=o; }
checkingQ(q,o); }

if (chancec==1) {
for (m=0; m<9>0)
temp[i][m]=0; }
checkingQ(q,c1c);
if (chanceq>0)
temp[o][p]=0;
temp[i][j]=c1c; }
if (chancer==1) {
for (m=0; m<9>0)
temp[m][j]=0; }
checkingQ(q,c1r);
if (chanceq>0)
temp[o][p]=0;
temp[i][j]=c1r; } } }

if (l+3<9)
l+=3;
else {
k+=3;
l=0; } }

cout << '\n';
for (i=0; i<9; i++) {
for (j=0; j<9; j++)
cout << temp[i][j] << ' ';
cout << '\n'; }

system("PAUSE");
return 0;
}
Back to top
View user's profile Send private message
humble_programmer

Joined: 27 Jun 2006
Posts: 69
:
Location: Colorado Springs, USA

Items
PostPosted: Sat Jan 20, 2007 2:38 pm    Post subject: Reply with quote

Here are some things to look into:

(1) You are using the variables chancec, chancer, and chanceq as flags, but you don't appear to ever reset them back to zero. Once they're set, they stay set for the rest of the program...not a very effective flag!

(2) You've got some syntax errors in later for() loops, but I'll assume that occured in the copy/paste process.

(3) I would recommend that you avoid using the letter 'o' as a variable name, 'cause it is easily confused with zero and makes your code harder to read.
_________________
Cheers!
Humble Programmer
,,,^..^,,,
www.humble-programmer.com
Back to top
View user's profile Send private message Visit poster's website
littleguy

Joined: 19 Jan 2007
Posts: 5
:
Location: Hong Kong

Items
PostPosted: Sun Jan 21, 2007 6:29 am    Post subject: Reply with quote

humble_programmer wrote:
Here are some things to look into:

(1) You are using the variables chancec, chancer, and chanceq as flags, but you don't appear to ever reset them back to zero. Once they're set, they stay set for the rest of the program...not a very effective flag!

(2) You've got some syntax errors in later for() loops, but I'll assume that occured in the copy/paste process.

(3) I would recommend that you avoid using the letter 'o' as a variable name, 'cause it is easily confused with zero and makes your code harder to read.

I know that it is not a effective way but i am just a form 5 student in hong kong. i am doing my CE exam homework. i think it is enough.

which for() loop?? could you point it out for me?

i had changed my programme. Please help my to find out what the other problem is.
#include <iostream>
#include <stdlib>
#include <time>


int temp[9][9],q=0, a=0,b=0, i=0,j=0,k=0,l=0,m=0, n=0, o=0,p=0, r=0, s=0,t=0, chance=0,chancer=0,chancec=0,chanceq=0, c1c=0,c1r=0,c1qi=0,c1qj=0;
char QUE[9][9];
bool crow[9][9],ccol[9][9],cq[9][9];

void checking(int i, int j, int n, int *chance) {
if (temp[i][j]==n)
*chance++; }

void checkingQ(int q, int n) {
int i=0,j=0;
switch (q) {
case 0 : s=0;
t=0;
break;
case 1 : s=0;
t=3;
break;
case 2 : s=0;
t=6;
break;
case 3 : s=3;
t=0;
break;
case 4 : s=3;
t=3;
break;
case 5 : s=3;
t=6;
break;
case 6 : s=6;
t=0;
break;
case 7 : s=6;
t=3;
break;
case 8 : s=6;
t=6;
break; }
for (i=s; i<(s+3); i++)
for (j=t; j<(t+3); j++) {
if (temp[i][j]==n) {
chanceq++;
c1qi=i;
c1qj=j; } } }


int main()
{ time_t rawtime;

for (i=0; i<9; i++)
for (j=0; j<9; j++) {
crow[i][j]=ccol[i][j]=cq[i][j]=false; temp[i][j]=0; }

for (a=0; a<4000; a++) {
k=0; l=0;
for (q=0; q<9; q++) {
for (i=k; i<(k+3); i++)
for (j=l; j<(l+3); j++) {
srand(time(NULL)+r++);
n=rand()%9;
if (temp[i][j] == 0) {
/*if (crow[i][n]==true || ccol[n][j]==true || cq[q][n]==true)
goto rerand;*/
if (crow[i][n]==false && ccol[n][j]==false && cq[q][n]==false) {
temp[i][j]=n+1;
crow[i][n]=true;
ccol[n][j]=true;
cq[q][n]=true; } } }
if (l+3<9)
l+=3;
else {
k+=3;
l=0; } } }

cout << '\n';
for (i=0; i<9; i++) {
for (j=0; j<9; j++)
cout << temp[i][j] << ' ';
cout << '\n'; }

Solve :
k=0; l=0;
for (q=0; q<9; q++) {
for (i=k; i<(k+3); i++)
for (j=l; j<(l+3); j++) {

chance=0; chancer=0; chancec=0; chanceq=0;

if (temp[i][j] == 0) {
for (o=0; o<9; o++) {
for (m=0; m<9; m++) {
checking(m,j,o,&chancec);
if (chancec==1)
c1c=o;
checking(i,m,o,&chancer);
if (chancer==1)
c1r=o; }
checkingQ(q,o); }

if (chancec==1) {
for (m=0; m<9>0)
temp[c1qi][c1qj]=0;
temp[i][j]=c1c+1; }
if (chancer==1) {
for (m=0; m<9; m++) {
chancec=0;
checking(m,j,c1r,&chancec);
if (chancec==1)
temp[m][j]=0; }
chanceq=0;
checkingQ(q,c1r);
if (chanceq==1)
temp[c1qi][c1qj]=0;
temp[i][j]=c1r+1; } } }

if (l+3<9)
l+=3;
else {
k+=3;
l=0; } }

for (a=0; a<9; a++)
for (b=0; b<9; b++)
if (temp[a][b]==0)
goto Solve;


cout << '\n';
for (i=0; i<9; i++) {
for (j=0; j<9; j++)
cout << temp[i][j] << ' ';
cout << '\n'; }

system("PAUSE");
return 0;
}
Back to top
View user's profile Send private message
humble_programmer

Joined: 27 Jun 2006
Posts: 69
:
Location: Colorado Springs, USA

Items
PostPosted: Sun Jan 21, 2007 2:26 pm    Post subject: Reply with quote

The wonky for() loop was this one:
Code:
for (m=0; m<9>0)


Here are some more ideas to improve your program:

(1) Break it into smaller pieces. How about a small function that returns a random box/column/row number? It would look something like this:
Code:

int    randomOffset(void)
{  return abs(rand() % 9); }

You could also create a function that checks a row for a certain value:
Code:

bool    rowHasValue(int row, int value)
{
for (int i = 0; i < 9; ++i)
    if (temp[iRow][i] == value)
        return true;

return false;
}


By breaking your program into smaller pieces, each piece becomes easier to create and test, and the overall program becomes easier to read. I'm not suggesting this is obvious--it's one of the harder parts of learning to program--but if you can figure out how to divide a bigger program into littler pieces, it will make it easier in the long run.
_________________
Cheers!
Humble Programmer
,,,^..^,,,
www.humble-programmer.com
Back to top
View user's profile Send private message Visit poster's website
littleguy

Joined: 19 Jan 2007
Posts: 5
:
Location: Hong Kong

Items
PostPosted: Mon Jan 22, 2007 11:19 am    Post subject: Reply with quote

thank you for your suggestion.

i had improved my programme. could you tell me is there any logic error?
Quote:

#include <iostream>
#include <stdlib>
#include <time>

int temp[9][9], q=0, a=0,b=0, i=0,j=0,k=0,l=0,m=0,n=0, p=0, r=0, s=0,t=0,
iQ=0,jQ=0, jRow=0,iColumn=0, chanceR=0,chanceC=0,chanceQ=0, nChance1R=0,nChance1C=0,nChance1Q=0;
char QUE[9][9];


bool qHasValue(int q, int value) {
int i=0,j=0;
switch (q) {
case 0 : s=0;
t=0;
break;
case 1 : s=0;
t=3;
break;
case 2 : s=0;
t=6;
break;
case 3 : s=3;
t=0;
break;
case 4 : s=3;
t=3;
break;
case 5 : s=3;
t=6;
break;
case 6 : s=6;
t=0;
break;
case 7 : s=6;
t=3;
break;
case 8 : s=6;
t=6;
break; }
for (i=s; i<(s+3); i++)
for (j=t; j<(t+3); j++) {
if (temp[i][j]==value) {
iQ=i;
jQ=j;
return true; } }
return false; }


int random_num(void)
{ return abs(rand() % 9); }


bool rowHasValue(int row, int value)
{
int i=0;
for (int i = 0; i < 9; ++i)
if (temp[row][i] == value)
{
jRow=i;
return true;
}

return false;
}


bool columnHasValue(int column, int value)
{
int j=0;
for (int j = 0; j < 9; ++j)
if (temp[j][column] == value)
{
iColumn=j;
return true;
}

return false;
}



int main()
{ time_t rawtime;

for (i=0; i<9; i++)
for (j=0; j<9; j++)
temp[i][j]=0;

for (a=0; a<40; a++) {
k=0; l=0;
for (q=0; q<9; q++) {
for (i=k; i<(k+3); i++)
for (j=l; j<(l+3); j++)
{
srand(time(NULL)+r++);
n=random_num();

if (temp[i][j] == 0) {
if (columnHasValue(j,n+1)==false && rowHasValue(i,n+1)==false && qHasValue(q,n+1)==false)
temp[i][j]=n+1; }
}
if (l+3<9)
l+=3;
else {
k+=3;
l=0; } } }


//display
cout << '\n';
for (i=0; i<9; i++) {
for (j=0; j<9; j++)
cout << temp[i][j] << ' ';
cout << '\n'; }
//display


Solve :
k=0; l=0;
for (q=0; q<9; q++) {
for (i=k; i<(k+3); i++)
for (j=l; j<(l+3); j++) {

chanceR=0; chanceC=0; chanceQ=0;

if (temp[i][j] == 0) {
// cout << i << " " << j << "\n";
for (p=0; p<9; p++)
{
if (columnHasValue(j,p+1)==false && rowHasValue(i,p+1)==false && qHasValue(q,p+1)==false)
temp[i][j]=p+1;

if (columnHasValue(j,p+1)==false)
chanceC++;
if (chanceC==1)
nChance1C=p+1;

if (rowHasValue(i,p+1)==false)
chanceR++;
if (chanceR==1)
nChance1R=p+1;

if (qHasValue(q,p+1)==false)
chanceQ++;
if (chanceQ==1)
nChance1Q=p+1;
}

if (chanceC==1)
{
if (rowHasValue(i,nChance1C)==true)
temp[i][jRow]=0;

if (qHasValue(q,nChance1C)==true)
temp[iQ][jQ]=0;

temp[i][j]=nChance1C;
}
if (chanceR==1)
{
if (columnHasValue(j,nChance1R)==true)
temp[iColumn][j]=0;

if (qHasValue(q,nChance1R)==true)
temp[iQ][jQ]=0;

temp[i][j]=nChance1R;
}
if (chanceQ==1)
{
if (rowHasValue(i,nChance1Q)==true)
temp[i][jRow]=0;

if (columnHasValue(j,nChance1Q)==true)
temp[iColumn][j]=0;

temp[i][j]=nChance1Q;
} } }
if (l+3<9)
l+=3;
else {
k+=3;
l=0; } }

for (a=0; a<9; a++)
for (b=0; b<9; b++)
if (temp[a][b]==0)
goto Solve;


//display
cout << '\n';
for (i=0; i<9; i++) {
for (j=0; j<9; j++)
cout << temp[i][j] << ' ';
cout << '\n'; }
//display


system("PAUSE");
return 0;
}
Back to top
View user's profile Send private message
littleguy

Joined: 19 Jan 2007
Posts: 5
:
Location: Hong Kong

Items
PostPosted: Tue Jan 23, 2007 3:42 pm    Post subject: Reply with quote

i found that the above programme can't stop after it run.
There must be some problem. Could you tell me?

I also want to know is there any possible way to create a solution.
Please express it in a way that is easy to understand.
Thank you very much Smile
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    Sudoku Programmers Forum Index -> Setting sudoku All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
Sudoku Programmers topic RSS feed 


Powered by phpBB © 2001, 2005 phpBB Group

Igloo Theme Version 1.0 :: Created By: Andrew Charron