Question: here is a sample program that he provided us for reference / / Function Prototypes int fib _ iterative ( int n ) ; int
here is a sample program that he provided us for reference Function Prototypes
int fibiterativeint n;
int fibrecursiveint n;
main Definition
int main
int n; Desired position of the fibonacci sequence.
cout "Enter desired position of the Fibonacci sequence: ;
cin n;
cout endl endl;
cout "Calculating using the Iterative method: endl;
auto iStart highresolutionclock::now; Records the current time, storing it in iStart. auto specifies the type will be determined by initialization.
cout "Position n calculated to be fibiterativen endl; Run the actual fibiterativen function.
auto iStop highresolutionclock::now; Records the time after the function has executed, storing the value in iStop.
The line below displays the time taken to run the function by counting the microseconds elapsed between iStop and iStart.
cout "Value calculated in durationcastiStop iStartcount microseconds.";
cout endl endl;
cout endl endl;
cout "Calculating using the Recursive method: endl;
auto rStart highresolutionclock::now; Same as above, but for the recursive function now.
cout "Position n calculated to be fibrecursiven endl;
auto rStop highresolutionclock::now;
cout "Value calculated in durationcastrStop rStartcount microseconds.";
cout endl endl;
systempause;
return ;
Other Definitions
int fibiterativeint n
int currentVal ;
int nextVal ;
for int i ; i n; i
currentVal currentVal nextVal;
nextVal currentVal nextVal;
return currentVal;
int fibrecursiveint n
if n First base case test
return ;
else if n Second base case test
return ;
else
Recursively calculates the sum of the sequence values at n and n
return fibrecursiven fibrecursiven ;
Recursive functions, or functions that call themselves, can be an alternative to loops to solve tasks that
can easily be broken down into smaller subproblems of the original. In this lab, you will begin to
practice with writing and calling recursive functions.
EXAMPLE PROGRAM
The example program this week implements a recursive solution to calculate the values of the Fibonacci
Sequence. It also calculates those same values iteratively, using only a loop, and displays information
about the amount of time required to calculate each solution.
In the case of the Fibonacci sequence, we'll see from this program that recursion is not always the most
efficient answer the extra overhead of managing all of those repeated function calls slows things down
considerably as values increase.
YOUR PROGRAM
The Collatz Conjecture is a conjecture in mathematics that concerns a sequence sometimes known as
hailstone numbers. Given any positive integer the following term, is calculated as follows:
If is even, then is defined as
If is odd, then is defined as
The Collatz Conjecture states that, for any value of the sequence will always reach Once the pattern
reaches it repeats indefinitely
For your lab, you will write a recursive function to calculate and display the sequence of hailstone
numbers from any initial starting point, until it reaches Additionally, after the sequence terminates
at you should print out the number of steps that were required to reach that point.
Notes:
The Collatz Conjecture is unproven, but works for every case ever tested. For the purpose of this
lab, we will assume it works in all cases, so you can rely on the sequence always reaching
Counting the number of recursions required can be handled in a number of different ways,
including additional parameters to the function or a static local variable. The choice is yours.
When you are done, submit your completed cpp file through the Blackboard submission tool. Please
name your file according to the usual convention, Last InitialFirst NameLabcpp
Lab Output Screenshot Example:
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
