Question

Homework 6 Problem 2 Translate the third of the following three algorithms to C++. Write a...

Homework 6 Problem 2 Translate the third of the following three algorithms to C++. Write a program to test the function. The first few Fibonacci numbers are 1,1,2,3,5,8,13,21,34,55,89,144,…

Fibonacci(n)

   f1 = 0

   f2 = 1

   fib = 0

   while n > 0

     f1 = f2

     f2 = fib

     fib = f1 + f2

     n = n – 1

   end while

   return fib

end fibonacci

fibonacci(n)

if n < 1 then

     return 0

else if n = 1 or 2 then

     return 1

else

     return fibonacci(n – 1) + fibonacci(n – 2)

end if

end fibonacci

fibonacci(n, f1, f2)

if n = ? then

     return f2

else

     return fibonacci(?, ?, ?)

end if

end fibonacci

Homework Answers

Answer #1

fibonacci(n,f1,f2) is a recursive function where the base case will be

if n==2

return f2

and therecursive call will be

return fibonacci(n-1,f2,f1+f2)

here we have given n-1 in the function because it is a recursive function and thus the function will call on it's subparts.

the value of f2 becomes f1 as in above two codes and f2 becomes f1+f2.

CODE IN C++:

#include<iostream>
using namespace std;
int fibonacci(int n,int f1,int f2)
{

if(n ==2) { //here we have put n==2 because the base case is when n reaches 2 as we know that fib(1) and fib(2) ==1

return f2;
}

else{

return fibonacci(n-1, f2, f1+f2); //recursive call
}
}

int main()
{
int ans;
cout<<"Enter the number : ";
cin>>ans;
cout<<"Fibonacci of number is "<<ans;
}

OUTPUT SNIPPET:

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Please Write the whole program in assembly i am able to calculate the fibonacci series but...
Please Write the whole program in assembly i am able to calculate the fibonacci series but not sure how to print it in reverse order. Please give a Complete code !! Programming Exercise 1 (10 points): [call it Ass2-Q1.asm] Write an ASM program that reads an integer number N and then displays the first N values of the Fibonacci number sequence, described by: Fib(0) = 0, Fib(1) = 1, Fib(N) = Fib(N-2) + Fib(N-1) Thus, if the input is N...
Please complete in MASM (x86 assembly language). Use the code below to get started. Write a...
Please complete in MASM (x86 assembly language). Use the code below to get started. Write a program that uses a loop to calculate the first seven values of the Fibonacci number sequence, described by the following formula: Fib(1) = 1, Fib(2) = 1, Fib(n) = Fib(n – 1) + Fib(n – 2). .386 .model flat,stdcall .stack 4096 ExitProcess PROTO,dwExitCode:DWORD .data    ; define your variables here .code main PROC    ; write your assembly code here    INVOKE ExitProcess,0 main...
In mathematics, the Fibonacci numbers are the numbers in the following integer sequence, called the Fibonacci...
In mathematics, the Fibonacci numbers are the numbers in the following integer sequence, called the Fibonacci sequence, and characterized by the fact that every number after the first two is the sum of the two preceding ones: 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, … The sequence Fn of Fibonacci numbers is defined by the recurrence relation: Fn = Fn-1 + Fn with seed values F1 = 1 F2 = 1 For more information on...
The following is Algorithm 8 from §5.4. Note that it uses the following definition of the...
The following is Algorithm 8 from §5.4. Note that it uses the following definition of the fibonacci sequence: fn = fn−1 + fn−2, f1 = 1, f0 = 0. procedure iterative fibonacci(n: nonnegative integer) if n = 0 then return 0 else x := 0 y := 1 for i := 1 to n − 1 do z := x + y x := y y := z end for return y end if end procedure Prove this algorithm is...
Solution.The Fibonacci numbers are defined by the recurrence relation is defined F1 = 1, F2 =...
Solution.The Fibonacci numbers are defined by the recurrence relation is defined F1 = 1, F2 = 1 and for n > 1, Fn+1 = Fn + Fn−1. So the first few Fibonacci Numbers are: 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, . . . There are numerous curious properties of the Fibonacci Numbers Use the method of mathematical induction to verify a: For all integers n > 1 and m > 0 Fn−1Fm + FnFm+1...
Written in MASM Assembly Problem Definition: Write a program to calculate Fibonacci numbers. • Display the...
Written in MASM Assembly Problem Definition: Write a program to calculate Fibonacci numbers. • Display the program title and programmer’s name. Then get the user’s name, and greet the user. • Prompt the user to enter the number of Fibonacci terms to be displayed. Advise the user to enter an integer in the range [1 .. 46]. • Get and validate the user input (n). • Calculate and display all of the Fibonacci numbers up to and including the nth...
Write the following in C: 2. An integer is said to be prime if it is...
Write the following in C: 2. An integer is said to be prime if it is divisible by only 1 and itself. Write a function called prime() that takes in one integer number, and returns 1 (true) if the number is prime and 0 (false) otherwise. Write a program to generate six random numbers between 1 to 100 and calls function prime() on each one to determine if it is prime or not.
WRITE C++ PROGRAM FOR 1,2,3,4 PARTS of question, DO ADD COOMENTS AND DISPLAY THE OUTPUT OF...
WRITE C++ PROGRAM FOR 1,2,3,4 PARTS of question, DO ADD COOMENTS AND DISPLAY THE OUTPUT OF A RUNNING COMPILER QUESTION: 1) Fibonacci sequence is a sequence in which every number after the first two is the sum of the two preceding ones. Write a C++ program that takes a number n from user and populate an array with first n Fibonacci numbers. For example: For n=10 Fibonacci Numbers: 1, 1, 2, 3, 5, 8, 13, 21, 34, 55 2): Write...
let a, b and c be constants. For the first problem, a sine wave is any...
let a, b and c be constants. For the first problem, a sine wave is any function of the type f(x)=asin(bx+c) and a cosine wave is any function of the type g(x)=acos(bx+c). a. find 3 distinct sine waves f1,f2,f3, all which satisfy f(0)=f(1)=0, amplitude 1/2 b. find 3 distinct cosine waves g1, g2, g3 all of which satisfy g(0)=g(1)=0 and have amplitude 2
Part A Write a 6-8 sentence summary explaining how you can use the Heap Sort algorithms...
Part A Write a 6-8 sentence summary explaining how you can use the Heap Sort algorithms to solve a real world problem. In your summary, provide a descriptive or visual explanation of your heapsort solution. Part B For this portion of the assignment you will design an algorithm to implement the solution described in part A. Your algorithm must be written in pseudocode. Note: Sample HeapSort Algorithm Pseudocode: Heapsort(A as array) BuildHeap(A) for i = n to 1 swap(A[1], A[i])...