954,535 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Sudoku Source Code

Hi friends..I had written a program on solving sudoku in 'C'..But it is not running...My logic is correct..I think that problem is in solve or check function..Plz check my program..it will take few minutes..n plz tell me what is wrong in program?? my id:
Thx friends..

/*PROGRAM FOR SOLVING THE SUDOKU USING 'C'*/

#include<stdio.h>
#include<conio.h>
#include<iostream.h>
#include<stdlib.h>

int i,j,k,a[10][10],o,x[100],y[100];

void display();
int getnum();
void solve(int [],int [],int);
int check(int ,int );

void main()
{
 clrscr();
 printf("\n\nEnter the elements of SUDOKU in rowwise.\n[ Enter '0' if element is absent. ]");
 for(i=1;i<=9;i++)
 for(j=1;j<=9;j++)
  scanf("%d",&a[i][j]);
 printf("\n\nEntered SUDOKU\n\n");
 display();
 printf("\nEnter any key for solution....\n");
 getch();
 o=getnum();
 solve(x,y,1);
 }

  int getnum()
  {
   int c=0;
   for(i=1;i<=9;i++)
   {
   for(j=1;j<=9;j++)
   {
    if(a[i][j]==0)
    {
     c++;
     x[c]=i;
     y[c]=j;
     }
    }
   }
   return(c);
  }

  void display()
  {
   for(i=1;i<=9;i++)
   {
    for(j=1;j<=9;j++)
    {
     if(a[i][j]!=0)
      printf("  %d",a[i][j]);
     else
      printf("  ");
     }
     printf("\n\n");
    }
   }


   void solve(int p[100],int q[100],int n)
   {
    for(k=1;k<=9;k++)
    for(i=p[n];i<=p[n];i++)
    for(j=q[n];j<=q[n];j++)
    {
     a[i][j]=k;
     if(n<0)
      solve(p,q,n++);
     int ch=check(1,0);
     if(ch!=0)
     {
      display();
      getch();
      exit(0);
      }
     }
    }
   }

   int check(int n,int r)
   {
    int f=0,cont=0;
    if(r==1)
    {
     for(k=1;k<=9;k++)
     {
      for(i=n;i<=n;i++)
      for(j=1;j<=9;j++)
      {
       if(k==a[i][j])
	f++;
       }
       if(f!=1)
	return(0);
       else
	cont++;
       f=0;
	}
	if(cont!=9)
	 return(0);
	else if(n==9)
	 check(1,0);
	else
	 check(n++,1);
	}
	else
	{
	 for(k=1;k<=9;k++)
	 {
	  for(i=1;i<=9;i++)
	  for(j=n;j<=n;j++)
	  {
	   if(k==a[i][j])
	    f++;
	   }
	   if(f!=1)
	    return(0);
	   else
	    cont++;
                   f=0;
	   }
	   if(cont!=9)
	    return(0);
	   else if(n!=9)
	    check(n++,1);
	    }
	   }
nnhamane
Newbie Poster
15 posts since Aug 2005
Reputation Points: 10
Solved Threads: 0
 

You've got an extra } at then end ofsolve, right before check.

Dave Sinkula
long time no c
Team Colleague
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314
 
plz tell me what is wrong in program

Dave Sinula gave you the short answer. In addition I'd list these.

1) Because you aren't sure where the problem is I suspect you may well have written the entire program and then compiled it/ran it. Instead write, compile, test each section of the code as you go. It will save lots of grief along the way.

2) If you are using C, then don't include the iostream.h header.

3) Giving main() a return type of void may work on your compiler, but it is non-standard and may not work on most compilers. Unless your instructor or your employer has told you you must give main() a return value of typee void always give it a return value of type int.

4) Your indentation and placement of brackets style could be better. That's why you weren't able to pick up the error Dave Sinkula pointed out.

5) In solve(), when will n ever be less than zero?

6) In solve(), what's the upper limit of n? If n becomes greater than 99 what will happen to your code?

7) In solve(), how many levels of recursion do you think it will take to solve the problem? Given that each level of recursion is going to set aside memory for 200 ints you will likely run out of room on the stack quite quickly. Can you think of a way to allow increased levels of recursion without changing much of your code?

8) Judicious use of comments will be a godsend to you one day, and will be a godsend for those trying to help you today. If you don't know how to comment with judgement, then overcommenting is better than undercommenting in my opinion.

There may well be others as well, but this should do for a start.

Lerner
Nearly a Posting Maven
2,382 posts since Jul 2005
Reputation Points: 739
Solved Threads: 396
 

For the solve() u need not pass the arrays x[] n y[] at all as they are global according to ur source code.
Also the solve() recursion if it occurs has to be terminated at some point right? wat is condition for termination? u can think about that.

mynameisor
Newbie Poster
4 posts since Dec 2008
Reputation Points: 10
Solved Threads: 1
 

Wow! It's amazing to see how many views a two year old thread has gotten. I'da ne'r thunk it.

Lerner
Nearly a Posting Maven
2,382 posts since Jul 2005
Reputation Points: 739
Solved Threads: 396
 

Sudoku's an addicting game, a lot of people think to themselves "hey! I can make this game with an array of arrays!" and then proceed to google other people's source code after ten minutes of fiddling and realizing that they have no idea what to do. At least, that's what I did :D

Choscura
Newbie Poster
1 post since Apr 2008
Reputation Points: 6
Solved Threads: 0
 

please send me a sudoku source code in c++

tob
Newbie Poster
1 post since Apr 2010
Reputation Points: 5
Solved Threads: 0
 
please send me a sudoku source code in c++
  • You just bumped 4 years old thread
  • You ignored our rules and announcements like We only give homework help to those who show effort
  • If you still interested in participating you better open new thread, explain properly what is your problem and do not forget to include a code you made so far.
  • Thread closed!
peter_budo
Code tags enforcer
Moderator
15,436 posts since Dec 2004
Reputation Points: 2,806
Solved Threads: 902
 

Post: Markdown Syntax: Formatting Help
You