| | |
Sudoku Source Code
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Aug 2005
Posts: 15
Reputation:
Solved Threads: 0
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..
Thx friends..
C++ Syntax (Toggle Plain Text)
/*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); } }
Last edited by WolfPack; Jun 28th, 2006 at 6:54 am. Reason: added code tags and deleted email id
You've got an extra
} at then end of solve, right before check. "One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
•
•
Join Date: Jul 2005
Posts: 1,673
Reputation:
Solved Threads: 261
•
•
•
•
plz tell me what is wrong in program
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.
![]() |
Similar Threads
- Source code for printf, scanf, cin, cout? (C)
- Sudoku Source Code. (C#)
- Windows 2K source code? (Windows NT / 2000 / XP)
Other Threads in the C++ Forum
- Previous Thread: quick question
- Next Thread: generation of random numbers quickly...
| Thread Tools | Search this Thread |
api array based binary c++ c/c++ calculator char char* class classes code coding compile console conversion count database delete deploy desktop developer directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux list loop looping loops map math matrix memory multiple news number numbertoword output pointer problem program programming project python random read recursion recursive reference return rpg sorting string strings struct temperature template templates test text text-file tree unix url variable vector video visualstudio win32 windows winsock wordfrequency wxwidgets






