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: <email nipped>
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);
	    }
	   }

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

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.

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.

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

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

commented: Well you've certainly got cluelessness down to a tee with the bumping of a long-dead thread to say nothing at all. -4

please send me a sudoku source code in c++

commented: Don't bump old threads. And no, I won't. -1
commented: Go and crawl back under your stone. -4

please send me a sudoku source code in c++

  1. You just bumped 4 years old thread
  2. You ignored our rules and announcements like We only give homework help to those who show effort
  3. 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.
  4. Thread closed!
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.