"bug" in code

Jellaiah's Avatar, Join Date: Jan 2007
Light Poster
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;
}

Last edited by shabbir; 8Jan2007 at 17:37.. Reason: Code formating.
0
Jellaiah's Avatar, Join Date: Jan 2007
Light Poster
Hmmm I just noticed this might well be in the wrong section... if so my apologies and uhm mods...HELP!!
0
shabbir's Avatar, Join Date: Jul 2004
Go4Expert Founder
You should be assigning the tel[i] = 0 for each row.
0
Jellaiah's Avatar, Join Date: Jan 2007
Light Poster
Hooverdam! 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!
0
shabbir's Avatar, Join Date: Jul 2004
Go4Expert Founder
You need some fresh eyes to have a look at it.
0
Jellaiah's Avatar, Join Date: Jan 2007
Light Poster
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
0
Jellaiah's Avatar, Join Date: Jan 2007
Light Poster
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...
0
shabbir's Avatar, Join Date: Jul 2004
Go4Expert Founder
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.
0
Jellaiah's Avatar, Join Date: Jan 2007
Light Poster
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.
0
shabbir's Avatar, Join Date: Jul 2004
Go4Expert Founder
The prob is not with <11 but it should start from 1 and not 0