Lost Password?

Go Back   CodeCall Programming Forum > Software Development > C and C++

C and C++ C and C++ forum for discussing all forms of C except for C#. These languages are powerful low level languages used for creating Operating Systems, Device Drivers, compilers and much more.

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 07-09-2008, 11:59 AM
vaibhav_cbeginner vaibhav_cbeginner is offline
Newbie
 
Join Date: Jul 2008
Posts: 1
Rep Power: 0
vaibhav_cbeginner is on a distinguished road
Default sudoku solver !!help!!

i was trying to make a sudoku solver using c..since i hav just started learning c, i dont know anything about pointers and functions..
so i am trying to make it using simple loops and array..(is it actually possible to make it without functions??)
i have written a code for it(its not complete yet) ..when i compile the program ,it shows no error..but when i run it,
no output is coming..the program goes as:
Code:
#include<stdio.h>
#include<conio.h>

void main()
{
 int a[10][10],i,j,k,b,l,ans;
 clrscr();
 for(i=1;i<10;i++)              
 {
  for(j=1;j<10;j++)
  {
   printf("a(%d,%d)\t",i,j);
  }
  printf("\n\n\n");
 }
 for(i=1;i<10;i++)                     // input from user
 {
  for(j=1;j<10;j++)
  {
   printf(" a[%d,%d]?\n",i,j);         // i hav used 0 to represent a blank box
   scanf("%d",&a[i][j]);
  }
 }
 for(i=1;i<10;i++)                     //
 {
  for(j=1;j<10;j++)
  {
   b=0;
   if(a[i][j]==0)                      //checks whether the box is empty
   {
    end:
    ans=0;
    b=b+1;                             //gives a value to empty box
    for(k=1;k<10;k++)
    {
     if(a[i][k]==b||a[k][j]==b)        //checks whether that value is already occupied in the same row and column
     {
      ans=1;
     }
    }
    for(k=1;k<4;k++)                   //
    { 
     for(l=1;l<4;l++)
     {
      if(a[3*(i/3)+l][3*(j/3)+k]==b)   //checks whether that value is occupied by the bigger box containing nine boxes
      {
       ans=1;
      }
     }
    } 
    if(ans=1)
    {
     goto end;                         //if yes, than it goes back and assign a higher value to the box 
    }
    a[i][j]=b;
   }  
  }   
}
 for(i=1;i<10;i++)                     //prints the result
 {
  for(j=1;j<10;j++)
  {
   printf("%d\t",a[i][j]);
  }
  printf("\n");
 }
getch();
}
the program is not yet complete as i havnt introduced the feedback yet..
can u tell me what is the problem with the code so far??why is it not giving any output??
when i remove the part which checks 'if the value lies in bigger box' it works fine..why??


srry i am not gud at explaining but hope u hav understood..

Last edited by WingedPanther; 07-12-2008 at 07:01 AM. Reason: add code tags
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

Sponsored Links
  #2 (permalink)  
Old 07-09-2008, 07:15 PM
Aereshaa's Avatar   
Aereshaa Aereshaa is offline
Programmer
 
Join Date: Apr 2008
Posts: 159
Rep Power: 5
Aereshaa has a spectacular aura aboutAereshaa has a spectacular aura aboutAereshaa has a spectacular aura about
Default Re: sudoku solver !!help!!

Here, let me put code tags around that, and indent it...
Code:
#include<stdio.h>
#include<conio.h>

void main(){
    int a[10][10],i,j,k,b,l,ans;
    clrscr();
    for(i=1;i<10;i++){
        for(j=1;j<10;j++){
             printf("a(%d,%d)\t",i,j);
             }
        printf("\n\n\n");
        }
    for(i=1;i<10;i++){ //input from user
         for(j=1;j<10;j++){
              printf(" a[%d,%d]?\n",i,j); // i hav used 0 to represent a blank box
              scanf("%d",&a[i][j]);
              }
         }
         for(i=1;i<10;i++){
              for(j=1;j<10;j++){
                   b=0;
                   if(a[i][j]==0){ //checks whether box is empty
                       end:
                       ans=0;
                       b=b+1; //gives a value to empty box
                       for(k=1;k<10;k++){
                            if(a[i][k]==b||a[k][j]==b){ //checks whether that value is already occupied in the same row and column
                                 ans=1;
                                 }
                            }
                       for(k=1;k<4;k++){
                            for(l=1;l<4;l++){
                                 if(a[3*(i/3)+l][3*(j/3)+k]==b){ //checks whether that value is occupied by the bigger box containing nine boxes
                                      ans=1;
                                      }
                                 }
                            }
                       if(ans=1){
                            goto end; //if yes, than it goes back and assign a higher value to the box
                            }
                        a[i][j]=b;
                        }
                   }
             }
        for(i=1;i<10;i++){ //prints the result
             for(j=1;j<10;j++){
                  printf("%d\t",a[i][j]);
                  }
             printf("\n");
        }
    getch();
    }
I'll take a look, and post again.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 07-11-2008, 02:49 PM
dargueta dargueta is offline
Guru
 
Join Date: Oct 2007
Age: 18
Posts: 509
Last Blog:
Programs Under the Hoo...
Rep Power: 10
dargueta is a jewel in the roughdargueta is a jewel in the roughdargueta is a jewel in the roughdargueta is a jewel in the rough
Default Re: sudoku solver !!help!!

There are a few things that I think you should fix:

1) I strongly suggest you use descriptive variable names, such as row, column, etc.
2) An array of size n is index from 0 to n - 1, not 1 to n. You're leaving out entire rows and columns in your calculations.
3) It's a bad idea to use goto statements; avoid them unless it's absolutely necessary. This only makes your code harder to read.
4) Please use functions. This code is a mess, and difficult to figure out. And please...use descriptive names for your functions.

The reason why no output is coming is because your program got stuck in an infinite loop somewhere.

Last edited by dargueta; 07-11-2008 at 02:51 PM.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 07-22-2008, 03:49 PM
outsid3r's Avatar   
outsid3r outsid3r is offline
Newbie
 
Join Date: Jul 2008
Posts: 6
Rep Power: 0
outsid3r is on a distinguished road
Default Re: sudoku solver !!help!!

I strongly agree with dargueta, you shouldn't use goto statements and you should give descriptive variable names. Well...you don't want to use functions but you should really try to use them, just makes your code better and easier to read, and functions are the basis for programming. Its easy, just use 'void function_name() { //do something }' and call the procedure then necessary, of course that functions can be personalized with parameters and return types, but in this case it isn't necessary, just declare some global variables and then manipulate them with functions.
Also, you don't need to include conio.h lib because you're not using any function that belongs to that lib.

Last edited by outsid3r; 07-22-2008 at 03:51 PM.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 07-22-2008, 05:42 PM
dargueta dargueta is offline
Guru
 
Join Date: Oct 2007
Age: 18
Posts: 509
Last Blog:
Programs Under the Hoo...
Rep Power: 10
dargueta is a jewel in the roughdargueta is a jewel in the roughdargueta is a jewel in the roughdargueta is a jewel in the rough
Default Re: sudoku solver !!help!!

Yes he is - getch() at the very bottom.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

Sponsored Links
  #6 (permalink)  
Old 07-22-2008, 05:45 PM
outsid3r's Avatar   
outsid3r outsid3r is offline
Newbie
 
Join Date: Jul 2008
Posts: 6
Rep Power: 0
outsid3r is on a distinguished road
Default Re: sudoku solver !!help!!

Quote:
Originally Posted by dargueta View Post
Yes he is - getch() at the very bottom.
Oh, that's right, i didn't saw! :P i apollogy
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
A simple SUDOKU creation v1.0 Kernel Software Development Tools 0 10-02-2006 03:10 AM
Sudoku can be solved using SQL..Take a look! roger Database & Database Programming 5 07-04-2006 02:20 PM


All times are GMT -5. The time now is 09:47 AM.

Contest Stats

GoogleKeyw ........ 20.00000

Contest Rules

CodeCall Goal

Goal: 100,000 Posts
Complete: 67%

Ads