Thread: sudoku program

  1. #1
    Registered User
    Join Date
    Nov 2007
    Posts
    73

    sudoku program

    guys ive written a sudoku 9x9 prg.....
    can you please tell me where i am going wrong.....

    Code:
    #include<iostream.h>
    #include<stdio.h>
    int chk_pos(int i,int j,int n);
    void sol_pos(void);
    int a[10][10][10]={0};
    int chk(int i,int j,int n)
    {
    	int suc=0;
    	for(int col=0;col<9;col++)
    		if(a[i][col][0]==n)
    		{
    			suc=1;break;
    		}
    	for(int row=0;row<9;row++)
    		if(a[row][j][0]==n)
    		{
    			suc=1;break;
    		}
    	i=i/3;j=j/3;
    	for(row=3*i;row<3*(i+1);row++)
    		for(col=3*j;col<3*(j+1);col++)
    			if(a[row][col][0]==n)
    				suc=1;
    	if(suc==0)
    		return n;
    	else
    		return 0;
    
    }
    
    
    void sol_pos(void)
    {
    	int i,j,k,fail=0;
    	for(i=0;i<9;i++)
    		for(j=0;j<9;j++)
    			for(k=1;k<9;k++)
    				a[i][j][k]=0;
    	for(i=0;i<9;i++)
    	{
    		for(j=0;j<9;j++)
    		{
    			k=1;
    			if(a[i][j][0]==0)
    			{
    				for(int num=1;num<=9;num++)
    				{
    					if(chk(i,j,num)!=0)
    						a[i][j][k++]=num;
    				}
    			}
    		}
    	}
    	for(i=0;i<9;i++)
    	{
    		for(j=0;j<9;j++)
    		{
    			if(a[i][j][0]==0 && a[i][j][2]==0)
    			{
    				fail=1;
    				a[i][j][0]=a[i][j][1];
    			}
    		}
    	}
    	if(fail==1)
    		sol_pos();
    }
    
    
    
    
    int main()
    {
    	int rown,coln,n;
    	cout<<"enter rown coln n :0 to exit:";
    	do{
    		cin>>rown>>coln>>n;
    		if(rown>9 || coln>9 || n>9 || n<1)
    			cout<<"invalid input";
    		else
    			a[rown-1][coln-1][0]=n;
    	}while(n!=0);
    	sol_pos();
    	for(int i=0;i<9;i++)
    	{
    		for(int j=0;j<9;j++)
    			cout<<a[i][j][0]<<" ";
    		cout<<"\n";
    	}
    	return 0;
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    1. What is it supposed to do?
    2. How does it not work?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help with a program, theres something in it for you
    By engstudent363 in forum C Programming
    Replies: 1
    Last Post: 02-29-2008, 01:41 PM
  2. Replies: 4
    Last Post: 02-21-2008, 10:39 AM
  3. Using variables in system()
    By Afro in forum C Programming
    Replies: 8
    Last Post: 07-03-2007, 12:27 PM
  4. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM