Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Program help

Status
Not open for further replies.

radawi

Technical User
Oct 15, 2005
4
CA
hi i am trying to write a program to solve a sudoku puzzle. I have some problems when compiling the program can anyone help me pls.

the program is
Program Assignment3
implicit none
integer :: A(9,9), vanish, g,h,i,j,xs,ys,k,m
logical :: issolved

open(unit=20,file="sudoku.txt")
do g=1,9

read (20,*) (A(g,h),h=1,9)

end do
close(20)



issolved= .true.
do while (issolved)
issolved= .false.
do x=1,9
do y=1,9

if( A(x,y)>10) then
issolved= .true.
do j=1,9
if (A(x,j)<10) then
A(x,y)= vanish( A(x,y),A(x,j))
end if
end do

do i=1,9
if (A(i,y)<10) then
A(x,y)= vanish( A(x,y),A(i,y))
end if
end do


xs=(x-1)/3+1
ys=(y-1)/3+1
do k=(xs*3-2),(xs*3)
do m=(ys*3-2),(ys*3)


do m=1,9
if (A(x,m)<10) then
A(x,y)= vanish( A(x,y),A(x,m))
end if
end do

do k=1,9
if (A(k,y)<10) then
A(x,y)= vanish( A(x,y),A(k,y))
end if
end do

end do
end do

end if
end do
end do

do x=1,9
write(*,*) (A(x,y),y=1,9)
end do

end Program Assignment3



integer function vanish(s,r)
implicit none
integer ::no,temps,digit
integer ::s,r,u
temps=s
u=0
no=0
do while(temps>0)

digit= mod(temps,10)
temps= temps/10

if (digit/=r) then
no=no+digit*10**u
end if

end do

vanish=no

end function vanish

and the input file sudolu.txt contains
5 3 0 0 7 0 0 0 0
6 0 0 1 9 5 0 0 0
0 9 8 0 0 0 0 6 0
8 0 0 0 6 0 0 0 3
4 0 0 8 0 3 0 0 1
7 0 0 0 2 0 0 0 6
0 6 0 0 0 0 2 8 0
0 0 0 4 1 9 0 0 5
0 0 0 0 8 0 0 7 9


 
Around line 43, Where the code says
Code:
                            do k=(xs*3-2),(xs*3)
                                    do m=(ys*3-2),(ys*3)


                                        do m=1,9
                                            if (A(x,m)<10) then
                                            A(x,y)= vanish( A(x,y),A(x,m))
                                            end if
                                        end do

                                        do k=1,9
                                            if (A(k,y)<10) then
                                            A(x,y)= vanish( A(x,y),A(k,y))
                                            end if
                                        end do

                                end do
                            end do
what exactly are you trying to do? The problem here is that you have two lots of do m... and two lots of do k... which are nested. Think about yourself in the compiler's shoes. When you refer to m or k in the if statement, which m or k are you referring to? Is it the one in the inner loop or the one in the outer loop? That is what it is moaning about when it says that the DO-iterator cannot be redefined. Fix this one first and see what happens.
 
Also, you have a missing end do at the end of your do while before the end of program.
 
thanks for the help. I managed to fix my code but it is still not solving the sudoku puzzle the output of the puzzle is supposed to be something like this:

5 3 4 6 7 8 9 1 2
6 7 2 1 9 5 3 4 8
1 9 8 3 4 2 5 6 7
8 5 9 7 6 1 4 2 3
4 2 6 8 5 3 7 9 1
7 1 3 9 2 4 8 5 6
9 6 1 5 3 7 2 8 4
2 8 7 4 1 9 6 3 5
3 4 5 2 8 6 1 7 9
 
Can you show us what the output of your code is?
 
5 3 0 0 7 0 0 0 0
6 0 0 1 9 5 0 0 0
0 9 8 0 0 0 0 6 0
8 0 0 0 6 0 0 0 3
4 0 0 8 0 3 0 0 1
7 0 0 0 2 0 0 0 6
0 6 0 0 0 0 2 8 0
0 0 0 4 1 9 0 0 5
0 0 0 0 8 0 0 7 9
>Exit code: 0
 
Looks exactly the same as the input.

Your logic is upside down - the while loop should read
Code:
! start with
issolved = .false.
do while (.not. issolved)
 ...
   ! don't change the state of issolved to .true. until
   ! you have solved it
 ...
end do
It is going once through the loop and exiting because A(x,y)>10 is never true. It never gets into the if statement.
 
this is what it looks like now...
issolved= .false.
do while (.not. issolved)
issolved= .true.
do x=1,9
do y=1,9

if( A(x,y)>10) then
issolved= .true.
do j=1,9
if (A(x,j)<10) then
A(x,y)= vanish( A(x,y),A(x,j))
end if
end do

do i=1,9
if (A(i,y)<10) then
A(x,y)= vanish( A(x,y),A(i,y))
end if
end do



but it is still not working
 
That is because you've just reversed everything - remove the issolved = .true. after the while loop.

You're assuming that the system will take numbers up to 10^10. Not sure if that will work or not. I am assuming it does work and you do not get a numeric overflow otherwise vanish will just explode.

Have you done a dry run i.e. go through your code manually to check that it is doing what you think it ought to do? You can normally figure out what has gone wrong by going round the loop a few times.
 
do you still have the program? Do you even get notified when someone replies?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top