Jump to content

sudoku solver !!help!!

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
5 replies to this topic

#1
vaibhav_cbeginner

vaibhav_cbeginner

    Newbie

  • Members
  • Pip
  • 1 posts
i was trying to make a sudoku solver using c..since i hav just started learning c, i dont know anything about pointers and functions..
so i am trying to make it using simple loops and array..(is it actually possible to make it without functions??)
i have written a code for it(its not complete yet) ..when i compile the program ,it shows no error..but when i run it,
no output is coming..the program goes as:
#include<stdio.h>
#include<conio.h>

void main()
{
 int a[10][10],i,j,k,b,l,ans;
 clrscr();
 for(i=1;i<10;i++)              
 {
  for(j=1;j<10;j++)
  {
   printf("a(%d,%d)\t",i,j);
  }
  printf("\n\n\n");
 }
 for(i=1;i<10;i++)                     // input from user
 {
  for(j=1;j<10;j++)
  {
   printf(" a[%d,%d]?\n",i,j);         // i hav used 0 to represent a blank box
   scanf("%d",&a[i][j]);
  }
 }
 for(i=1;i<10;i++)                     //
 {
  for(j=1;j<10;j++)
  {
   b=0;
   if(a[i][j]==0)                      //checks whether the box is empty
   {
    end:
    ans=0;
    b=b+1;                             //gives a value to empty box
    for(k=1;k<10;k++)
    {
     if(a[i][k]==b||a[k][j]==b)        //checks whether that value is already occupied in the same row and column
     {
      ans=1;
     }
    }
    for(k=1;k<4;k++)                   //
    { 
     for(l=1;l<4;l++)
     {
      if(a[3*(i/3)+l][3*(j/3)+k]==b)   //checks whether that value is occupied by the bigger box containing nine boxes
      {
       ans=1;
      }
     }
    } 
    if(ans=1)
    {
     goto end;                         //if yes, than it goes back and assign a higher value to the box 
    }
    a[i][j]=b;
   }  
  }   
}
 for(i=1;i<10;i++)                     //prints the result
 {
  for(j=1;j<10;j++)
  {
   printf("%d\t",a[i][j]);
  }
  printf("\n");
 }
getch();
}
the program is not yet complete as i havnt introduced the feedback yet..
can u tell me what is the problem with the code so far??why is it not giving any output??
when i remove the part which checks 'if the value lies in bigger box' it works fine..why??


srry i am not gud at explaining but hope u hav understood..

Edited by WingedPanther, 12 July 2008 - 04:01 AM.
add code tags


#2
Aereshaa

Aereshaa

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 790 posts
Here, let me put code tags around that, and indent it...
#include<stdio.h>

#include<conio.h>


void main(){

    int a[10][10],i,j,k,b,l,ans;

    clrscr();

    for(i=1;i<10;i++){

        for(j=1;j<10;j++){

             printf("a(%d,%d)\t",i,j);

             }

        printf("\n\n\n");

        }

    for(i=1;i<10;i++){ //input from user

         for(j=1;j<10;j++){

              printf(" a[%d,%d]?\n",i,j); // i hav used 0 to represent a blank box

              scanf("%d",&a[i][j]);

              }

         }

         for(i=1;i<10;i++){

              for(j=1;j<10;j++){

                   b=0;

                   if(a[i][j]==0){ //checks whether box is empty

                       end:

                       ans=0;

                       b=b+1; //gives a value to empty box

                       for(k=1;k<10;k++){

                            if(a[i][k]==b||a[k][j]==b){ //checks whether that value is already occupied in the same row and column

                                 ans=1;

                                 }

                            }

                       for(k=1;k<4;k++){

                            for(l=1;l<4;l++){

                                 if(a[3*(i/3)+l][3*(j/3)+k]==b){ //checks whether that value is occupied by the bigger box containing nine boxes

                                      ans=1;

                                      }

                                 }

                            }

                       if(ans=1){

                            goto end; //if yes, than it goes back and assign a higher value to the box

                            }

                        a[i][j]=b;

                        }

                   }

             }

        for(i=1;i<10;i++){ //prints the result

             for(j=1;j<10;j++){

                  printf("%d\t",a[i][j]);

                  }

             printf("\n");

        }

    getch();

    }
I'll take a look, and post again.

#3
dargueta

dargueta

    Writes binary right handed and hex left handed

  • Moderators
  • 4,720 posts
There are a few things that I think you should fix:

1) I strongly suggest you use descriptive variable names, such as row, column, etc.
2) An array of size n is index from 0 to n - 1, not 1 to n. You're leaving out entire rows and columns in your calculations.
3) It's a bad idea to use goto statements; avoid them unless it's absolutely necessary. This only makes your code harder to read.
4) Please use functions. This code is a mess, and difficult to figure out. And please...use descriptive names for your functions.

The reason why no output is coming is because your program got stuck in an infinite loop somewhere.

Edited by dargueta, 11 July 2008 - 11:51 AM.


#4
outsid3r

outsid3r

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 623 posts
I strongly agree with dargueta, you shouldn't use goto statements and you should give descriptive variable names. Well...you don't want to use functions but you should really try to use them, just makes your code better and easier to read, and functions are the basis for programming. Its easy, just use 'void function_name() { //do something }' and call the procedure then necessary, of course that functions can be personalized with parameters and return types, but in this case it isn't necessary, just declare some global variables and then manipulate them with functions.
Also, you don't need to include conio.h lib because you're not using any function that belongs to that lib.

Edited by outsid3r, 22 July 2008 - 12:51 PM.


#5
dargueta

dargueta

    Writes binary right handed and hex left handed

  • Moderators
  • 4,720 posts
Yes he is - getch() at the very bottom.

#6
outsid3r

outsid3r

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 623 posts

dargueta said:

Yes he is - getch() at the very bottom.

Oh, that's right, i didn't saw! :P i apollogy