Sudoku Source Code

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Aug 2005
Posts: 15
Reputation: nnhamane is an unknown quantity at this point 
Solved Threads: 0
nnhamane nnhamane is offline Offline
Newbie Poster

Sudoku Source Code

 
0
  #1
Jun 28th, 2006
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..


  1. /*PROGRAM FOR SOLVING THE SUDOKU USING 'C'*/
  2.  
  3. #include<stdio.h>
  4. #include<conio.h>
  5. #include<iostream.h>
  6. #include<stdlib.h>
  7.  
  8. int i,j,k,a[10][10],o,x[100],y[100];
  9.  
  10. void display();
  11. int getnum();
  12. void solve(int [],int [],int);
  13. int check(int ,int );
  14.  
  15. void main()
  16. {
  17. clrscr();
  18. printf("\n\nEnter the elements of SUDOKU in rowwise.\n[ Enter '0' if element is absent. ]");
  19. for(i=1;i<=9;i++)
  20. for(j=1;j<=9;j++)
  21. scanf("%d",&a[i][j]);
  22. printf("\n\nEntered SUDOKU\n\n");
  23. display();
  24. printf("\nEnter any key for solution....\n");
  25. getch();
  26. o=getnum();
  27. solve(x,y,1);
  28. }
  29.  
  30. int getnum()
  31. {
  32. int c=0;
  33. for(i=1;i<=9;i++)
  34. {
  35. for(j=1;j<=9;j++)
  36. {
  37. if(a[i][j]==0)
  38. {
  39. c++;
  40. x[c]=i;
  41. y[c]=j;
  42. }
  43. }
  44. }
  45. return(c);
  46. }
  47.  
  48. void display()
  49. {
  50. for(i=1;i<=9;i++)
  51. {
  52. for(j=1;j<=9;j++)
  53. {
  54. if(a[i][j]!=0)
  55. printf(" %d",a[i][j]);
  56. else
  57. printf(" ");
  58. }
  59. printf("\n\n");
  60. }
  61. }
  62.  
  63.  
  64. void solve(int p[100],int q[100],int n)
  65. {
  66. for(k=1;k<=9;k++)
  67. for(i=p[n];i<=p[n];i++)
  68. for(j=q[n];j<=q[n];j++)
  69. {
  70. a[i][j]=k;
  71. if(n<0)
  72. solve(p,q,n++);
  73. int ch=check(1,0);
  74. if(ch!=0)
  75. {
  76. display();
  77. getch();
  78. exit(0);
  79. }
  80. }
  81. }
  82. }
  83.  
  84. int check(int n,int r)
  85. {
  86. int f=0,cont=0;
  87. if(r==1)
  88. {
  89. for(k=1;k<=9;k++)
  90. {
  91. for(i=n;i<=n;i++)
  92. for(j=1;j<=9;j++)
  93. {
  94. if(k==a[i][j])
  95. f++;
  96. }
  97. if(f!=1)
  98. return(0);
  99. else
  100. cont++;
  101. f=0;
  102. }
  103. if(cont!=9)
  104. return(0);
  105. else if(n==9)
  106. check(1,0);
  107. else
  108. check(n++,1);
  109. }
  110. else
  111. {
  112. for(k=1;k<=9;k++)
  113. {
  114. for(i=1;i<=9;i++)
  115. for(j=n;j<=n;j++)
  116. {
  117. if(k==a[i][j])
  118. f++;
  119. }
  120. if(f!=1)
  121. return(0);
  122. else
  123. cont++;
  124. f=0;
  125. }
  126. if(cont!=9)
  127. return(0);
  128. else if(n!=9)
  129. check(n++,1);
  130. }
  131. }
Last edited by WolfPack; Jun 28th, 2006 at 6:54 am. Reason: added code tags and deleted email id
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,335
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 236
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: Sudoku Source Code

 
0
  #2
Jun 28th, 2006
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
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 1,673
Reputation: Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all 
Solved Threads: 261
Lerner Lerner is offline Offline
Posting Virtuoso

Re: Sudoku Source Code

 
0
  #3
Jun 28th, 2006
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.
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 4
Reputation: mynameisor is an unknown quantity at this point 
Solved Threads: 1
mynameisor mynameisor is offline Offline
Newbie Poster

Re: Sudoku Source Code

 
0
  #4
Dec 9th, 2008
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.
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 1,673
Reputation: Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all 
Solved Threads: 261
Lerner Lerner is offline Offline
Posting Virtuoso

Re: Sudoku Source Code

 
0
  #5
Dec 9th, 2008
Wow! It's amazing to see how many views a two year old thread has gotten. I'da ne'r thunk it.
Klatu Barada Nikto
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC