sudoku program

This is a discussion on sudoku program within the C Programming forums, part of the General Programming Boards category; guys ive written a sudoku 9x9 prg..... can you please tell me where i am going wrong..... Code: #include<iostream.h> #include<stdio.h> ...

  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
    21,648
    1. What is it supposed to do?
    2. How does it not work?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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, 12:41 PM
  2. Replies: 4
    Last Post: 02-21-2008, 09: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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21