Math Is Fun Forum
  Discussion about math, puzzles, games and fun.   Useful symbols: ÷ × ½ √ ∞ ≠ ≤ ≥ ≈ ⇒ ± ∈ Δ θ ∴ ∑ ∫ • π ƒ -¹ ² ³ °

You are not logged in.

#1 2011-09-28 21:23:56

anonimnystefy
Real Member
From: The Foundation
Registered: 2011-05-23
Posts: 15,657

Another programming problem!!!

hi guys

i am trying to make a program to solve a Sudoku.i hope you know what that is.

now i have finished the code but there seems to be a logical error because i entered a valid one and it outputted the message 'The Sudoku cannot be solved!!!'

here's the code:

program Sudoku_solver;

{$mode objfpc}{$H+}

uses
  {$IFDEF UNIX}{$IFDEF UseCThreads}
  cthreads,
  {$ENDIF}{$ENDIF}
  Classes
  { you can add units after this };

{$IFDEF WINDOWS}{$R Sudoku.rc}{$ENDIF}

const
  max=9;

type
  niz=array[1..max] of integer;
  matrica=array[1..max] of niz;

var
  a:matrica;
  i,j:integer;
  ok:boolean;

function row(a:matrica;i1,j1:integer):boolean;
var
  j:integer;
  b:boolean;
begin
  b:=true;
  for j:=1 to 9 do
    if j<>j1 then
      if a[i1][j]=a[i1][j1] then
        b:=false;
  red:=b;
end;

function column(a:matrica;i1,j1:integer):boolean;
var
  i:integer;
  b:boolean;
begin
  b:=true;
  for i:=1 to 9 do
    if i<>i1 then
      if a[i][j1]=a[i1][j1] then
        b:=false;
  vrsta:=b;
end;

function square(a:matrica;i1,j1:integer):boolean;
var
  i,j,k,l:integer;
  b:boolean;
begin
  b:=true;
  case i1 of
    1,2,3: begin
             k:=0;
             i:=3
           end;
    4,5,6: begin
             k:=3;
             i:=6;
           end;
    7,8,9: begin
             k:=6;
             i:=9;
           end;
  end;
  case j1 of
    1,2,3: begin
             l:=0;
             j:=3
           end;
    4,5,6: begin
             l:=3;
             j:=6;
           end;
    7,8,9: begin
             l:=6;
             j:=9;
           end;
  end;
  while (k<=i) do
  begin
    k:=k+1;
    while (l<=j) do
    begin
      l:=l+1;
      if (k<>i1) and (l<>j1) then
        if a[k][l]=a[i1][j1] then
          b:=false;
    end;
  end;
  kvadrat:=b;
end;

function pos(a:matrica;i,j:integer):boolean;
begin
  poz:=row(a,i,j) and column(a,i,j) and square(a,i,j);
end;

procedure sudoku(var a:matrica;n,i1,j1:integer;ok:boolean);
var
  i,j,k:integer;
  b:boolean;
begin
  i:=i1;
  j:=j1;
  a[i][j]:=n;
  ok:=false;
  b:=true;
  if poz(a,i,j) then
  begin
    ok:=true;
    i:=0;
    j:=0;
    while (b=true) and (i<=max) do
    begin
      i:=i+1;
      while (b=true) and (j<=max) do
      begin
        j:=j+1;
        if a[i][j]=0 then
          b:=false;
      end;
    end;
    if b=false then
    begin
      k:=0;
      ok:=false;
      while (k<=9) and not ok do
      begin
        k:=k+1;
        sudoku(a,k,i,j,ok);
      end;
    end;
  end;
end;

begin
  writeln('Enter the Sudoku: ');
  for i:=1 to 9 do
  begin
    for j:=1 to 9 do
      read(a[i][j]);
    readln;
  end;
  k:=0;
  ok:=false;
  while (k<=9) and not ok do
  begin
    k:=k+1;
    sudoku(a,k,i,j,ok);
  end;
  if ok then
  begin
    writeln('The solution is: ')
    for i:=1 to 9 do
    begin
      for j:=1 to 9 do
        write(a[i][j]);
      writeln;
    end;
  end
  else writeln('Sudoku cannot be solved!!!');
  readln;
end.

note that functions row,column and square check if there are same numbers as the number we are looking at in the same row,column and square.

my second question is: is there another (better) way to make the Sudoku solver,because this one is fairly long and complex !?

Last edited by anonimnystefy (2011-09-30 09:14:47)


“Here lies the reader who will never open this book. He is forever dead.

“Taking a new step, uttering a new word, is what people fear most.” ― Fyodor Dostoyevsky, Crime and Punishment

Offline

#2 2011-09-29 00:27:16

bobbym
Administrator
From: Bumpkinland
Registered: 2009-04-12
Posts: 90,562

Re: Another programming problem!!!

Hi anonimnystefy;

That is not long at all for a sudoku solver. What is the sudoku problem that you are
testing this on?


In mathematics, you don't understand things. You just get used to them.

I agree with you regarding the satisfaction and importance of actually computing some numbers. I can't tell you how often I see time and money wasted because someone didn't bother to run the numbers.

Offline

#3 2011-09-29 07:36:34

anonimnystefy
Real Member
From: The Foundation
Registered: 2011-05-23
Posts: 15,657

Re: Another programming problem!!!

hi bobbym

it's:
0 4 7 0 0 0 3 0 0
8 0 6 0 7 4 0 0 0
0 0 0 2 0 3 8 4 0
0 7 0 0 0 0 4 0 3
9 0 0 4 0 8 0 0 2
4 0 3 0 0 0 0 9 0
0 2 8 6 0 1 0 0 0
0 0 0 8 2 0 7 0 9
0 0 9 0 0 0 2 1 8

the answer should be:

2 4 7 1 8 9 3 5 6
8 3 6 5 7 4 9 2 1
5 9 1 2 6 3 8 4 7
1 7 2 9 5 6 4 8 3
9 6 5 4 3 8 1 7 2
4 8 3 7 1 2 6 9 5
7 2 8 6 9 1 5 3 4
3 1 4 8 2 5 7 6 9
6 5 9 3 4 7 2 1 8


“Here lies the reader who will never open this book. He is forever dead.

“Taking a new step, uttering a new word, is what people fear most.” ― Fyodor Dostoyevsky, Crime and Punishment

Offline

#4 2011-09-29 08:07:24

anonimnystefy
Real Member
From: The Foundation
Registered: 2011-05-23
Posts: 15,657

Re: Another programming problem!!!

just for practice:

Last edited by anonimnystefy (2011-09-29 08:13:23)


“Here lies the reader who will never open this book. He is forever dead.

“Taking a new step, uttering a new word, is what people fear most.” ― Fyodor Dostoyevsky, Crime and Punishment

Offline

#5 2011-09-29 08:35:11

bobbym
Administrator
From: Bumpkinland
Registered: 2009-04-12
Posts: 90,562

Re: Another programming problem!!!

Funny thing is my program does noot get that one either.


In mathematics, you don't understand things. You just get used to them.

I agree with you regarding the satisfaction and importance of actually computing some numbers. I can't tell you how often I see time and money wasted because someone didn't bother to run the numbers.

Offline

#6 2011-09-29 08:53:23

anonimnystefy
Real Member
From: The Foundation
Registered: 2011-05-23
Posts: 15,657

Re: Another programming problem!!!

but why won't it work?


“Here lies the reader who will never open this book. He is forever dead.

“Taking a new step, uttering a new word, is what people fear most.” ― Fyodor Dostoyevsky, Crime and Punishment

Offline

#7 2011-09-29 08:59:44

bobbym
Administrator
From: Bumpkinland
Registered: 2009-04-12
Posts: 90,562

Re: Another programming problem!!!

Hi;

I do not know. I checked the rows and columns of the solution as well as
every 3 x 3 grid. It should have got that answer?!


In mathematics, you don't understand things. You just get used to them.

I agree with you regarding the satisfaction and importance of actually computing some numbers. I can't tell you how often I see time and money wasted because someone didn't bother to run the numbers.

Offline

#8 2011-09-29 09:14:34

anonimnystefy
Real Member
From: The Foundation
Registered: 2011-05-23
Posts: 15,657

Re: Another programming problem!!!

have you checked it compleely.it uses recursion.


“Here lies the reader who will never open this book. He is forever dead.

“Taking a new step, uttering a new word, is what people fear most.” ― Fyodor Dostoyevsky, Crime and Punishment

Offline

#9 2011-09-29 09:17:52

bobbym
Administrator
From: Bumpkinland
Registered: 2009-04-12
Posts: 90,562

Re: Another programming problem!!!

Hi anonimnystefy;

No, I haven't. I am looking at mine as to why it does not get that answer.


In mathematics, you don't understand things. You just get used to them.

I agree with you regarding the satisfaction and importance of actually computing some numbers. I can't tell you how often I see time and money wasted because someone didn't bother to run the numbers.

Offline

#10 2011-09-29 09:27:03

anonimnystefy
Real Member
From: The Foundation
Registered: 2011-05-23
Posts: 15,657

Re: Another programming problem!!!

what does it get?


“Here lies the reader who will never open this book. He is forever dead.

“Taking a new step, uttering a new word, is what people fear most.” ― Fyodor Dostoyevsky, Crime and Punishment

Offline

#11 2011-09-29 15:18:52

bobbym
Administrator
From: Bumpkinland
Registered: 2009-04-12
Posts: 90,562

Re: Another programming problem!!!

Spits it out as if it does not have a solution! Where does that solution come from?


In mathematics, you don't understand things. You just get used to them.

I agree with you regarding the satisfaction and importance of actually computing some numbers. I can't tell you how often I see time and money wasted because someone didn't bother to run the numbers.

Offline

#12 2011-09-30 06:39:40

anonimnystefy
Real Member
From: The Foundation
Registered: 2011-05-23
Posts: 15,657

Re: Another programming problem!!!

from me! smile


“Here lies the reader who will never open this book. He is forever dead.

“Taking a new step, uttering a new word, is what people fear most.” ― Fyodor Dostoyevsky, Crime and Punishment

Offline

#13 2011-09-30 07:27:28

bobbym
Administrator
From: Bumpkinland
Registered: 2009-04-12
Posts: 90,562

Re: Another programming problem!!!

That is what I was thinking. Did you try it on any others?


In mathematics, you don't understand things. You just get used to them.

I agree with you regarding the satisfaction and importance of actually computing some numbers. I can't tell you how often I see time and money wasted because someone didn't bother to run the numbers.

Offline

#14 2011-09-30 08:37:54

anonimnystefy
Real Member
From: The Foundation
Registered: 2011-05-23
Posts: 15,657

Re: Another programming problem!!!

nope! :embarrassed!


“Here lies the reader who will never open this book. He is forever dead.

“Taking a new step, uttering a new word, is what people fear most.” ― Fyodor Dostoyevsky, Crime and Punishment

Offline

#15 2011-09-30 08:38:56

bobbym
Administrator
From: Bumpkinland
Registered: 2009-04-12
Posts: 90,562

Re: Another programming problem!!!

I can give you some examples if you do not have any.


In mathematics, you don't understand things. You just get used to them.

I agree with you regarding the satisfaction and importance of actually computing some numbers. I can't tell you how often I see time and money wasted because someone didn't bother to run the numbers.

Offline

#16 2011-09-30 08:56:06

anonimnystefy
Real Member
From: The Foundation
Registered: 2011-05-23
Posts: 15,657

Re: Another programming problem!!!

i have bunch of them and i tried this one:
0 5 6 0 8 0 0 1 0
8 3 0 2 0 7 0 0 0
0 1 2 0 0 0 0 0 0
0 0 0 6 0 0 7 0 9
0 0 0 1 0 4 0 0 0
2 0 3 0 0 8 0 0 0
0 0 0 0 0 0 3 9 0
0 0 0 4 0 9 0 2 6
0 7 0 0 5 0 1 4 0

it won't work!


“Here lies the reader who will never open this book. He is forever dead.

“Taking a new step, uttering a new word, is what people fear most.” ― Fyodor Dostoyevsky, Crime and Punishment

Offline

#17 2011-09-30 09:15:58

anonimnystefy
Real Member
From: The Foundation
Registered: 2011-05-23
Posts: 15,657

Re: Another programming problem!!!

hi bobbym

i found an error on my side in the main code and i fixed it and i edited it but it still won't do it! sad


“Here lies the reader who will never open this book. He is forever dead.

“Taking a new step, uttering a new word, is what people fear most.” ― Fyodor Dostoyevsky, Crime and Punishment

Offline

#18 2011-09-30 09:19:43

bobbym
Administrator
From: Bumpkinland
Registered: 2009-04-12
Posts: 90,562

Re: Another programming problem!!!

Hi;

Something must be wrong with yours.

View Image: my.png

In mathematics, you don't understand things. You just get used to them.

I agree with you regarding the satisfaction and importance of actually computing some numbers. I can't tell you how often I see time and money wasted because someone didn't bother to run the numbers.

Offline

#19 2011-09-30 09:25:15

anonimnystefy
Real Member
From: The Foundation
Registered: 2011-05-23
Posts: 15,657

Re: Another programming problem!!!

ya think?


“Here lies the reader who will never open this book. He is forever dead.

“Taking a new step, uttering a new word, is what people fear most.” ― Fyodor Dostoyevsky, Crime and Punishment

Offline

#20 2011-09-30 09:27:03

bobbym
Administrator
From: Bumpkinland
Registered: 2009-04-12
Posts: 90,562

Re: Another programming problem!!!

That is about all I can do. I do not speak Java. Even my C++ is too rusty. Use your debugger to go line by line watching the variables as you go.


In mathematics, you don't understand things. You just get used to them.

I agree with you regarding the satisfaction and importance of actually computing some numbers. I can't tell you how often I see time and money wasted because someone didn't bother to run the numbers.

Offline

#21 2011-09-30 22:03:47

anonimnystefy
Real Member
From: The Foundation
Registered: 2011-05-23
Posts: 15,657

Re: Another programming problem!!!

where did you get Java?

i'm using Pascal.very basic.

i changed my code a bit and got a program that solves only the first row.it won't go to the next one!


“Here lies the reader who will never open this book. He is forever dead.

“Taking a new step, uttering a new word, is what people fear most.” ― Fyodor Dostoyevsky, Crime and Punishment

Offline

#22 2011-09-30 22:34:51

bobbym
Administrator
From: Bumpkinland
Registered: 2009-04-12
Posts: 90,562

Re: Another programming problem!!!

Hi anonimnystefy;

Forgive my sense of humor, I know, you told me you were using Lazarus.

In the other thread, just out of curiosity, I asked if lazarus has a debugger? Does it?


In mathematics, you don't understand things. You just get used to them.

I agree with you regarding the satisfaction and importance of actually computing some numbers. I can't tell you how often I see time and money wasted because someone didn't bother to run the numbers.

Offline

#23 2011-09-30 22:36:51

anonimnystefy
Real Member
From: The Foundation
Registered: 2011-05-23
Posts: 15,657

Re: Another programming problem!!!

think so. hmm


“Here lies the reader who will never open this book. He is forever dead.

“Taking a new step, uttering a new word, is what people fear most.” ― Fyodor Dostoyevsky, Crime and Punishment

Offline

#24 2011-09-30 22:39:38

bobbym
Administrator
From: Bumpkinland
Registered: 2009-04-12
Posts: 90,562

Re: Another programming problem!!!

Does it have a variable pane? Or a variable watch?


In mathematics, you don't understand things. You just get used to them.

I agree with you regarding the satisfaction and importance of actually computing some numbers. I can't tell you how often I see time and money wasted because someone didn't bother to run the numbers.

Offline

#25 2011-09-30 22:44:34

anonimnystefy
Real Member
From: The Foundation
Registered: 2011-05-23
Posts: 15,657

Re: Another programming problem!!!

yes,yes it does.


“Here lies the reader who will never open this book. He is forever dead.

“Taking a new step, uttering a new word, is what people fear most.” ― Fyodor Dostoyevsky, Crime and Punishment

Offline

Board footer

Powered by FluxBB