Math Is Fun Forum

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

You are not logged in.

#9826 Re: Coder's Corner » Another programming problem!!! » 2011-10-06 03:36:14

hi bobbym

sorry for not answering right away,but in my head i pictured you being frustrated,so i thought i should lay of for a day.

i don't think it matters anymore.a problem was that j was stuck at 10 and i should have set it to 1 every time i entered the outer loop.anyway here's the new 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,k:integer;
  ok:boolean;

function red(a:matrica;i1,j1:integer):boolean;
var
  j:integer;
  b:boolean;
begin
  b:=true;
  j:=1;
  while (j<=max) and (b=true) do
  begin
    if j<>j1 then
      if a[i1][j]=a[i1][j1] then
        b:=false;
    j:=j+1;
  end;
  red:=b;
end;

function kolona(a:matrica;i1,j1:integer):boolean;
var
  i:integer;
  b:boolean;
begin
  b:=true;
  i:=1;
  while (i<=max) and (b=true) do
  begin
    if i<>i1 then
      if a[i][j1]=a[i1][j1] then
        b:=false;
    i:=i+1;
  end;
  kolona:=b;
end;

function kvadrat(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:=1;
             i:=3;
           end;
    4,5,6: begin
             k:=4;
             i:=6;
           end;
    7,8,9: begin
             k:=7;
             i:=9;
           end;
  end;
  while (k<=i) and (b=true) do
  begin
    case j1 of
    1,2,3: begin
             l:=1;
             j:=3;
           end;
    4,5,6: begin
             l:=4;
             j:=6;
           end;
    7,8,9: begin
             l:=7;
             j:=9;
           end;
    end;
    while (l<=j) and (b=true) do
    begin
      if (k<>i1) or (l<>j1) then
        if a[k][l]=a[i1][j1] then
          b:=false;
      l:=l+1;
    end;
    k:=k+1;
  end;
  kvadrat:=b;
end;

function poz(a:matrica;i,j:integer):boolean;
begin
  poz:=red(a,i,j) and kolona(a,i,j) and kvadrat(a,i,j);
end;

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

begin
  writeln('Unesite sudoku: ');
  for i:=1 to 9 do
  begin
    for j:=1 to 9 do
      read(a[i][j]);
    readln;
  end;
  for i:=1 to 9 do
  begin
    for j:=1 to 9 do
      write(a[i][j],' ');
    writeln;
  end;
  k:=0;
  ok:=false;
  while (k<=9) and not ok do
  begin
    k:=k+1;
    sudoku(a,k,1,1,ok);
  end;
  if ok then
  begin
    writeln('Resenje je: ');
    for i:=1 to 9 do
    begin
      for j:=1 to 9 do
        write(a[i][j],' ');
      writeln;
    end;
  end
  else writeln('Sudoku se ne moze resiti!!!');
  readln;
end.

but this time the problem seems to be that the functions for checking if a number can be in the given row column and square aren't doing what they should.those are 'red','kolona' and 'kvadrat'.

#9827 Re: Coder's Corner » Another programming problem!!! » 2011-10-05 08:15:32

i don't really understand the question.

#9828 Re: Coder's Corner » Another programming problem!!! » 2011-10-05 08:11:36

you said to print out values of i and j.

besides that it finds the place of the first zero element,so it can try out different values for it.

#9829 Re: Coder's Corner » Another programming problem!!! » 2011-10-05 08:02:16

i do it in my head.and i understand you perfectly.

the writeln(i/j) statements are printing how i and j change.that's what you told me to do!

#9830 Re: Coder's Corner » Another programming problem!!! » 2011-10-05 07:53:38

part of the code in the sudoku procedure finds the first element equal to 0,because the spaces are represented that way.

here it is:

while (b=true) and (i<=max) do
    begin
      i:=i+1;
      while (b=true) and (j<=max) do
      begin
        j:=j+1;
        writeln(i);
        writeln(j);
        if a[i][j]=0 then
          b:=false;
      end;

but i don't see the problem.

#9831 Re: Coder's Corner » Another programming problem!!! » 2011-10-05 07:42:51

no i fixed the infinite loop problem.but there is still this one.

#9832 Re: Coder's Corner » Another programming problem!!! » 2011-10-05 07:40:08

i don't know the code where the problem is in.if i did i would probably know how to correct it.but i don't

#9833 Re: Coder's Corner » Another programming problem!!! » 2011-10-05 07:37:48

yes i did.

the problem is that it solves only the first line.actually it tries to solve,but doesn't mean that the final solution is like that.

it won't go to the next line and i don't know why!

#9834 Re: Coder's Corner » Another programming problem!!! » 2011-10-05 07:34:26

i wrote it.i would never lie to you.

btw the name of the function is 'vrsta'.sorry.

#9835 Re: Coder's Corner » Another programming problem!!! » 2011-10-05 07:28:49

well it's still the same problem but this time thanks to my friend i found an infinite lup in one of the procedures.it's named 'kolona':

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,k:integer;
  ok:boolean;

function red(a:matrica;i1,j1:integer):boolean;
var
  j:integer;
  b:boolean;
begin
  b:=true;
  j:=0;
  while (j<=max) and (b=true) do
  begin
    j:=j+1;
    if j<>j1 then
      if a[i1][j]=a[i1][j1] then
        b:=false;
  end;
  red:=b;
end;

function vrsta(a:matrica;i1,j1:integer):boolean;
var
  i:integer;
  b:boolean;
begin
  b:=true;
  i:=0;
  while(i<=max) and(b=true) do
  begin
    i:=i+1;
    if i<>i1 then
      if a[i][j1]=a[i1][j1] then
        b:=false;
  end;
  vrsta:=b;
end;

function kvadrat(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 poz(a:matrica;i,j:integer):boolean;
begin
  poz:=red(a,i,j) and vrsta(a,i,j) and kvadrat(a,i,j);
end;

procedure sudoku(var a:matrica;n,i1,j1:integer;var ok:boolean);
var
  i,j,k:integer;
  b:boolean;
begin
  i:=i1;
  j:=j1;
  a[i][j]:=n;
  ok:=false;
  if poz(a,i,j) then
  begin
    ok:=true;
    i:=0;
    j:=0;
    b:=true;
    while (b=true) and (i<=max) do
    begin
      i:=i+1;
      while (b=true) and (j<=max) do
      begin
        j:=j+1;
        writeln(i);
        writeln(j);
        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 Sudoku: ');
  for i:=1 to 9 do
  begin
    for j:=1 to 9 do
      read(a[i][j]);
    readln;
  end;
  for i:=1 to 9 do
  begin
    for j:=1 to 9 do
      write(a[i][j],' ');
    writeln;
  end;
  k:=0;
  ok:=false;
  while (k<=9) and not ok do
  begin
    k:=k+1;
    sudoku(a,k,1,1,ok);
  end;
  if ok then
  begin
    writeln('Resenje je: ');
    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.

#9837 Re: Help Me ! » Geometry Hw! » 2011-10-02 07:14:46

hi bob

it's okay.it's a free world!

#9839 Re: Help Me ! » Geometry Hw! » 2011-10-01 02:41:22

hi mmk

ok,so a is correct,then for b just substitute 200 cm² for V and calculate r.

for c you need to check if the diameter of a circle is smaller than the edge of the cube.

#9841 Re: Coder's Corner » Another programming problem!!! » 2011-10-01 00:39:03

i inputted a sudoku and it looks like it is calculating because i cannot type anything and nothing happens when i press enter,but could it mean anything else?

#9842 Re: Coder's Corner » Another programming problem!!! » 2011-10-01 00:26:22

i use a two-dimensional array for the values and variables i and j to run through them.

#9846 Re: Coder's Corner » Another programming problem!!! » 2011-09-30 23:48:08

well do you think it is correct?

i was just confused by the subroutine part.

#9847 Re: Coder's Corner » Another programming problem!!! » 2011-09-30 23:35:57

checked the loops,seem okay.

what's recursive subroutine?

#9848 Re: Coder's Corner » Another programming problem!!! » 2011-09-30 23:24:47

but,do you know why the program won't go to the next line?

#9849 Re: Coder's Corner » Another programming problem!!! » 2011-09-30 22:49:31

yes,but it takes so long!

anyway i noticed that the program acts weirdly around ones.

Board footer

Powered by FluxBB