What is the error in my Towers of Hanoi Fortran 90 program using recursion?

  • Context: Comp Sci 
  • Thread starter Thread starter Dylicious
  • Start date Start date
  • Tags Tags
    Fortran
Click For Summary
SUMMARY

The forum discussion centers on a Towers of Hanoi program written in Fortran 90 that utilizes recursion. The user reports that the program compiles but produces incorrect output, specifically displaying garbage values for the destination pegs. The issue is identified as uninitialized variables 'source' and 'dest', leading to undefined behavior during execution. The solution involves ensuring that these variables are properly initialized before use.

PREREQUISITES
  • Understanding of Fortran 90 syntax and structure
  • Familiarity with recursion and its implementation in programming
  • Knowledge of the Towers of Hanoi problem and its algorithm
  • Experience with debugging techniques for identifying uninitialized variables
NEXT STEPS
  • Review Fortran 90 variable initialization practices
  • Study recursion in Fortran 90 with examples
  • Learn about debugging tools available for Fortran programs
  • Explore the Towers of Hanoi algorithm in detail, including its recursive solution
USEFUL FOR

This discussion is beneficial for Fortran developers, students learning recursion, and anyone interested in algorithm implementation and debugging in Fortran 90.

Dylicious
Messages
5
Reaction score
0

Homework Statement


Write a Towers of Hanoi program in Fortran 90 using a subroutine.


Homework Equations





The Attempt at a Solution


hello all

Ive been trying to write a simple Towers of Hanoi problem in FORTRAN 90 dealing with recursion.
Ive got my program to compile but it doesn't do the right thing, if you know the Towers of Hanoi problem than you'll understand why, what I need is someone to show me where I went wrong in my code. Ill post the code I wrote as well as what happens when I run it. THanks again guys!

Program Hanoi
! A Program for generating the sequence of moves to play the Towers of Hanoi game.
! Filename: Hanoi.f90 Src: ESS 22/09/2003
Implicit NONE
INTEGER :: n
character (len=1) :: A='A',B='B',C='C'

Interface
recursive subroutine MoveTower( n, source, dest, spare)
Implicit NONE
integer, intent(in) :: n
character (len=1) :: source, dest, spare
end subroutine MoveTower
End Interface

print *,'n: '
read *,n

! Check validity of input

IF (n <= 0) THEN
print *,'Invalid Input, n must be positive'
STOP
End IF

call MoveTower(n,A,B,C)
end Program Hanoi

Recursive Subroutine MoveTower( n, source, dest, spare)
IF (n == 1) THEN
print *,'Move disk 1 from source peg to destination peg'
ELSE
call MoveTower( n-1, source, spare, dest)
print *,'Move disk ',n,'from peg ',source,'to peg ',dest
call MoveTower( n-1, spare, dest, source)
END IF
RETURN
END Subroutine MoveTower


Heres what happens when I run it...

Hanoi
n:
3
Move disk 1 from source peg to destination peg
Move disk 2 from peg 9.1084400E-44 to peg 9.3886997E-44
Move disk 1 from source peg to destination peg
Move disk 3 from peg 9.1084400E-44 to peg 9.2485699E-44
Move disk 1 from source peg to destination peg
Move disk 2 from peg 9.3886997E-44 to peg 9.2485699E-44
Move disk 1 from source peg to destination peg


any ideas??
Ps Happy Thanksgiving if you're awesome!
 
Physics news on Phys.org
Variables 'source' and 'dest' are never initialized nor set equal to any value.
 
Garbage values as in the following line are usually an indication that a variable was used before it was initialized.

Move disk 2 from peg 9.1084400E-44 to peg 9.3886997E-44
 

Similar threads

Replies
2
Views
8K
  • · Replies 7 ·
Replies
7
Views
2K
Replies
4
Views
3K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 25 ·
Replies
25
Views
5K
  • · Replies 8 ·
Replies
8
Views
3K
  • · Replies 10 ·
Replies
10
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 7 ·
Replies
7
Views
3K
  • · Replies 12 ·
Replies
12
Views
4K