Answered step by step
Verified Expert Solution
Link Copied!

Question

00
1 Approved Answer

QUES) Write Program in c The Fibonacci sequence is given by 1, 1, 2, 3, 5, 8, 13, 21, . The first two numbers are

QUES) Write Program in c

The Fibonacci sequence is given by 1, 1, 2, 3, 5, 8, 13, 21, . The first two numbers are 1, and subsequent numbers are the sum of the previous two. If Fib(k) represents the kth number in the sequence, then we have Fib(1) = Fib(2) = 1, and Fib(k) = Fib(k-1) + Fib(k-2) for k > 2.

a) Consider the Fib and main functions below.

image text in transcribed

Draw the progression of the function call stack for the program. Each time there is a new function call or a function returns, there should be a depiction of the call stack. For example, if the first line of the main function is int m = 2;, then the first few call stacks may look like this:

image text in transcribed

Note that in the last row of the above table, the function Fib called with parameter k = 2 has finished execution, and returns a value of 1. Fib with parameter k = 2 has been popped from the call stack, which is why it is not shown. Answer this question in the format of the 5-column table shown above. Make sure to keep track of all local variables in every function currently in the call stack.

b) Write a different version of Fib, called FibIter, that also returns the kth Fibonacci sequence, but only uses iteration. Use a loop; no recursion allowed in this part! The function prototype is as follows. Please use comments to help the reader understand your code.

int FibIter(int k) {

}

int Fib(int k) - if(k(z2) { return 1; return Fib(k-1) Fib(k-2); int main() int m = 4; int n Fib(m); return e; Call stack Last function call Last function return Function parameters Function (for function that isreturn value (if applicable) (if applicable) called or returned) (if applicable) Main main N/A N/A N/A Fib N/A Fib N/A main N/A Fib main

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions