Question: 1 . Write and test a C + + program to implement the following algorithms: GCD computation using iterative and recursive algorithms Tower of Hanoi

1. Write and test a C++ program to implement the following algorithms:
GCD computation using iterative and recursive algorithms
Tower of Hanoi displaying the order of moving disks using recursive algorithm.
The pseudocode for algorithms are given as below:
GCD_Iterative(a, b)
While b !=0
temp a mod b
a b
b temp
return a
GCD_Recursive(a, b)
If b =0
Return a
Else
Return GCD_Recursive(b, a mod b)
Tower_Of_Hanoi(N, From, To, Via)
// From, To, Via are string variables, N is integer
If N =0
Return
Tower_Of_Hanoi(N-1, From, Via, To)
Print Move Disk n from From To To
Tower_Of_Hanoi(N-1, Via, To, From)
In all these functions, introduce a runtime for computations of runtime.
Initialize the runtime for each one to 0 in the beginning of main.
Then increment by 1 relevant runtime in each of the three functions
From the main, call each of the functions with suitable inputs.
For GCD, a and b must be user inputs
For the Tower problem, send names of tower (say A, B, C or tower1, tower2, tower3) and number of disks as user inputs (this must be done either at the time of calling the function or prior to it.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Finance Questions!