Go4Expert (http://www.go4expert.com/)
-   C (http://www.go4expert.com/forums/c/)
-   -   "bug" in code (http://www.go4expert.com/forums/bug-in-code-t2472/)

 Jellaiah 8Jan2007 17:23

"bug" in code

Hello all,

I have this program where I want to check whether a sudoko is correct (you all know the simple rules:1->9 in each line, column and 3*3 square)
Below you will find the code of a function that gets a nice little 9*9 table and then has to check if all the lines are filled in correctly. However despite my best (thus probably not so great) efforts it still seems to think every wrong sudoku I throw at it is correct.
I am fully aware of the fact that this is totally useless and newbie (school) stuff but I'm trying to learn something and since learning from your mistakes is supposed to help you improve big time any help would be greatly appreciated!

Here's the code:
Code:

```int rijtest (int sud[][9]) {  int i=0,j,k=0,a=0;  int tel[10];  while (k<11)  {         tel[k]=0;         k++;  }  for (i=0;i<9 && !a;i++)  {         for (j=0;j<9;j++)         {           switch (sud[i][j])           {             case 1:                 tel[1]++; break;             case 2:                 tel[2]++; break;             case 3:                 tel[3]++; break;             case 4:                 tel[4]++; break;             case 5:                 tel[5]++; break;             case 6:                 tel[6]++; break;             case 7:                 tel[7]++; break;             case 8:                 tel[8]++; break;             case 9:                 tel[9]++; break;             default:                 tel[0]++;           }         }         if (tel[0])         {                 printf("unvalid number on row %d\n",++i);                 a=100+i;  //errorcode to return to main         }         else         {                 k=0;                 while (k<11 && !a)                 {                     if (tel[k] !=1)                     {                         printf("double number on row %d\n",++i);                         a=100+10+i; //errorcode to return to main                     }                 }         }  }  return a; }```

 Jellaiah 8Jan2007 17:30

Re: "bug" in code

Hmmm I just noticed this might well be in the wrong section... if so my apologies and uhm mods...HELP!! ;)

 shabbir 8Jan2007 17:39

Re: "bug" in code

You should be assigning the tel[i] = 0 for each row.

 Jellaiah 8Jan2007 17:48

Re: "bug" in code

Hooverdam!:eek: I spent hours staring at that code, got some people (probably even worse coders than me) to take a look at it and it turns out to be so obvious... Goes to show the long path to enlightenment in front of me...
Nonetheless thanks a bunch!

 shabbir 8Jan2007 17:52

Re: "bug" in code

You need some fresh eyes to have a look at it.

 Jellaiah 8Jan2007 18:03

Re: "bug" in code

Indeed, fresh eyes help a bunch, and that's where you come in really handy ;)
However, I've now put the while at the beginning of the first for loop (where it should be I reckon) but...same old story...still refuses to grant my wish of just doing what it's supposed to do...
Weird thing is though, i can start with all the counters at 0 and check whether their one, which makes it consider everything wrong, or i can start with counters at one and decrease them to 0, which makes it consider everything right... I'm kind of flabbergasted here, not to mention utterly hopeless ;)

 Jellaiah 8Jan2007 18:18

Re: "bug" in code

Extra info:
From the looks of the debugger it would seem the a value (thus the conditions I put it in) seems to mess things up too... But again I have absolutely no clue as to why and how...

 shabbir 8Jan2007 18:43

Re: "bug" in code

you have a bad code fragment which I dont like
Code:

```switch (sud[i][j]) { case 1:  tel[1]++; break; case 2:  tel[2]++; break; case 3:  tel[3]++; break; case 4:  tel[4]++; break; case 5:  tel[5]++; break; case 6:  tel[6]++; break; case 7:  tel[7]++; break; case 8:  tel[8]++; break; case 9:  tel[9]++; break; default:  tel[0]++; }```
Which is equal to tel[sud[i][j]]++;

I guess you should start on the fresh part of it.

Also you probably dont have k initialized as well.

Here is the modified version.
Code:

```int rijtest (int sud[][9]) {         int i=0,j,k=0,a=0;         int tel[10];         for (i=0;i<9 && !a;i++)         {                 k=0;                 while (k<11)                 {                         tel[k]=0;                         k++;                 }                 for (j=0;j<9;j++)                 {                         tel[sud[i][j]]++;                 }                 if (tel[0])                 {                         printf("unvalid number on row %d\n",++i);                         a=100+i;  //errorcode to return to main                 }                 else                 {                         k=1;                         while (k<11 && !a)                         {                                 if (tel[k] !=1)                                 {                                         printf("double number on row %d\n",++i);                                         a=100+10+i; //errorcode to return to main                                 }                         }                 }         }         return a; }```
You were checking for k=0 which will never be increment for a correct sudoku while printing.

 Jellaiah 8Jan2007 18:50

Re: "bug" in code

Finally got it to work,at about the time of your last post. Thanks a bunch though, i kept the switch/case structure but there was indeed a problem with k (had a while (k<11),had to be <10)
On the upside I learned a lesson (for the 1000th time ;)): check,double check and recheck al conditions.

 shabbir 8Jan2007 20:31

Re: "bug" in code

The prob is not with <11 but it should start from 1 and not 0

 All times are GMT +5.5. The time now is 12:02.