1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

"bug" in code

Discussion in 'C' started by Jellaiah, Jan 8, 2007.

  1. Jellaiah

    Jellaiah New Member

    Joined:
    Jan 8, 2007
    Messages:
    7
    Likes Received:
    0
    Trophy Points:
    0
    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 a moderator: Jan 8, 2007
  2. Jellaiah

    Jellaiah New Member

    Joined:
    Jan 8, 2007
    Messages:
    7
    Likes Received:
    0
    Trophy Points:
    0
    Hmmm I just noticed this might well be in the wrong section... if so my apologies and uhm mods...HELP!! ;)
     
  3. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,283
    Likes Received:
    364
    Trophy Points:
    83
    You should be assigning the tel = 0 for each row.
     
  4. Jellaiah

    Jellaiah New Member

    Joined:
    Jan 8, 2007
    Messages:
    7
    Likes Received:
    0
    Trophy Points:
    0
    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!
     
  5. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,283
    Likes Received:
    364
    Trophy Points:
    83
    You need some fresh eyes to have a look at it.
     
  6. Jellaiah

    Jellaiah New Member

    Joined:
    Jan 8, 2007
    Messages:
    7
    Likes Received:
    0
    Trophy Points:
    0
    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 ;)
     
  7. Jellaiah

    Jellaiah New Member

    Joined:
    Jan 8, 2007
    Messages:
    7
    Likes Received:
    0
    Trophy Points:
    0
    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...
     
  8. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,283
    Likes Received:
    364
    Trophy Points:
    83
    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[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.
     
  9. Jellaiah

    Jellaiah New Member

    Joined:
    Jan 8, 2007
    Messages:
    7
    Likes Received:
    0
    Trophy Points:
    0
    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.
     
  10. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,283
    Likes Received:
    364
    Trophy Points:
    83
    The prob is not with <11 but it should start from 1 and not 0
     

Share This Page