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

  • Comp Sci
  • Thread starter Dylicious
  • Start date
  • Tags
    Fortran
In summary, the conversation is about a person seeking help in writing a Towers of Hanoi program in Fortran 90 using a subroutine. They share their code and the output they are getting, and ask for suggestions on where they may have gone wrong. The problem is identified as uninitialized variables and the solution is to set them equal to a value before using them.
  • #1
Dylicious
5
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
  • #2
Variables 'source' and 'dest' are never initialized nor set equal to any value.
 
  • #3
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
 

1. What is Towers of Hanoi Fortran 90?

Towers of Hanoi Fortran 90 is a computer program used to solve the Towers of Hanoi puzzle, which is a mathematical game or puzzle that involves moving a stack of disks from one peg to another, following specific rules.

2. How does Towers of Hanoi Fortran 90 work?

Towers of Hanoi Fortran 90 uses recursive programming to solve the puzzle. It breaks down the problem into smaller subproblems and solves them in a step-by-step manner until the puzzle is solved.

3. What are the benefits of using Fortran 90 for Towers of Hanoi?

Fortran 90 is a high-level programming language that is specifically designed for scientific and engineering applications. It has efficient mathematical and logical operations, making it an ideal choice for solving complex problems like the Towers of Hanoi puzzle.

4. Can Towers of Hanoi Fortran 90 be used for other purposes?

Yes, Towers of Hanoi Fortran 90 can be modified and used for other applications besides solving the puzzle. It can be adapted to solve similar problems that involve recursive algorithms.

5. Is Towers of Hanoi Fortran 90 difficult to learn?

It depends on the individual's prior knowledge and experience with programming. For someone with basic understanding of Fortran 90, learning Towers of Hanoi Fortran 90 may not be too challenging. However, for someone with no prior programming experience, it may require some time and effort to understand the logic and syntax of the program.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
2
Views
7K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
2K
  • Programming and Computer Science
Replies
25
Views
5K
  • Engineering and Comp Sci Homework Help
Replies
10
Views
2K
  • Programming and Computer Science
Replies
5
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
12
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
5
Views
5K
  • Programming and Computer Science
Replies
8
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
3K
  • Advanced Physics Homework Help
Replies
1
Views
2K
Back
Top