Question: 1. a) State clearly what you understand by an iterative algorithm and a recursive algorithm. b) Given a function as below: int fun(int n) {

 1. a) State clearly what you understand by an iterative algorithmand a recursive algorithm. b) Given a function as below: int fun(intn) { if (n == 1) return 2; else return (n +

1. a) State clearly what you understand by an iterative algorithm and a recursive algorithm. b) Given a function as below: int fun(int n) { if (n == 1) return 2; else return (n + fun(n/2): } What type of function is this: Iterative or recursive? Give justifications to your answer. C) Evaluate the value of fun(5), by showing all the steps. 2. Given a function as below. int Sum(int N) { if(N == 0 || N == 1) return 5; else return N + F(N-2); } Determine the value of F(9) using this function. Show all the intermediate steps. 3. The tower of Hanoi problem is an interesting problem in Computer Science, the algorithm for Tower of Hanoi is given in the lecture notes, which is given here again: TowerOfHanoi(tower1, tower2, tower3, n) if n=1 print move disk n from tower1 to tower2 else TowerOfHanoi(tower1, tower3, tower2, n-1) Print move disk n from tower 1 to tower2 TowerOfHansitower3, tower2, tower1, n-1) Based on this algorithm, write a C++ program, and call this function from main, by supplying the names of tower1, tower2, tower3, and also passing number of disks (n). 4. The Euclid algorithm for determining GCD of two integers a and b (assuming a >b) was given iteratively in Homework 1. The same algorithm can be written recursively, by following these two steps: Base step: If b = 0, return a Else return GCD(b, a mod b) Where a mod b is written in C++ as: a %b It computes the remainder when a is divided by b You need to write a C++ program that uses this function and from main send values of a and b, and make sure a is greater than b, prior to calling the function GCD

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 Databases Questions!