|
||||||
| 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. |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Display Modes |
|
|||
|
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();
}
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 08:01 AM. Reason: add code tags |
| Sponsored Links |
|
|
|
|||
|
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 03:51 PM. |
|
|||||
|
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 04:51 PM. |
|
|||
|
Yes he is - getch() at the very bottom.
|
| Sponsored Links |
|
|
![]() |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | Search this Thread |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| A simple SUDOKU creation v1.0 | Kernel | Software Development Tools | 0 | 10-02-2006 04:10 AM |
| Sudoku can be solved using SQL..Take a look! | roger | Database & Database Programming | 5 | 07-04-2006 03:20 PM |
| WingedPanther | ........ | 2753.6 |
| Xav | ........ | 2704 |
| Brandon W | ........ | 1702.32 |
| John | ........ | 1207.73 |
| marwex89 | ........ | 1175.24 |
| morefood2001 | ........ | 966.05 |
| dcs | ........ | 655.75 |
| Steve.L | ........ | 475.59 |
| orjan | ........ | 418.58 |
| Aereshaa | ........ | 383.54 |