Question

In: Computer Science

Write a C++ program to computer the nth Fibonacci Number in 3 ways. Clearly define which...

Write a C++ program to computer the nth Fibonacci Number in 3 ways. Clearly define which way you are solving in your code using comments. You must provide an algorithm to solve for the nth Fibonacci Number in 1. a straight-forward recursive solution 2. a top-down memoized solution and 3. a bottom-up dynamic programming solution. These algorithms will be tested with randomly generated input, and a single non-negative number will be given through command line.

Solutions

Expert Solution

Method 1

//Fibonacci Series using Recursion
#include<iostream>

using namespace std;
int fib(int n)
{
if (n <= 1)
   return n;
return fib(n-1) + fib(n-2);
}

int main (int argc, char *argv[])
{
int n = atoi(argv[1]);


printf("%d", fib(n));
return 0;
}

Method 2

#include <iostream>

using namespace std;

int F[50]; //array to store fibonacci terms

// Initially, all elements of array F are -1.

void init_F() {

int i;

for(i=0; i<50; i++) {

F[i] = -1;

}

}

int dynamic_fibonacci(int n) {

if (F[n] < 0) {

if (n==0) {

F[n] = 0;

}

else if (n == 1) {

F[n] = 1;

}

else {

F[n] = dynamic_fibonacci(n-1) + dynamic_fibonacci(n-2);

}

}

return F[n];

}

int main(int argc, char *argv[]){

int n = atoi(argv[1]);

printf("%d\n",dynamic_fibonacci(n));

return 0;

}

Method 3

#include <iostream>

using namespace std;

int F[50]; //array to store fibonacci terms

int fibonacci_bottom_up(int n) {

F[n] = 0;

F[1] = 1;

int i;

for(i=2; i<=n; i++) {

F[i] = F[i-1] + F[i-2];

}

return F[n];

}


int main(int argc, char *argv[]){

int n = atoi(argv[1]);

printf("%d\n",fibonacci_bottom_up(n));

return 0;

}


Related Solutions

. (a) Write a C++ program to find Fibonacci numbers. (For the definition of Fibonacci numbers...
. (a) Write a C++ program to find Fibonacci numbers. (For the definition of Fibonacci numbers and the recursive formula please refer to problem 5 of Chapter 2 at page 81 in the text-book.) The program asks the user to input an integer and outputs the corresponding Fibonacci number. For example, if the user inputs 4, the program outputs the fifth Fibonacci number, which is 3. Introduce two local variables in the recursion function (suppose we call it fib(n)), one...
( Assembly Language ) Write a program that computes the 7th fibonacci number. The fibonacci sequence...
( Assembly Language ) Write a program that computes the 7th fibonacci number. The fibonacci sequence - 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, … what is the initialization of a, b, and d? - a b d 1 ? ? 1 2 ? ? 1 3 1 1 2 4 1 2 3 5 2 3 5 6 3 5 8 7 5 8 13 wrong initialization - a b d 1 0 1 1 2...
Write a recursive and an iterative function to calculate the nth element in a Fibonacci sequence....
Write a recursive and an iterative function to calculate the nth element in a Fibonacci sequence. A Fibonacci sequence is defined as the element 1, followed by another 1, and each element thereafter is the sum of the previous two elements. For example, the first 9 elements of a Fibonacci sequence are: 1 2 3 5 8 13 21 34 This famous sequence was originally used to predict the growth of rabbit populations! Once you have each of the functions...
In Python Write a Fibonacci class to calculate next number in the 'Fibonacci' class by the...
In Python Write a Fibonacci class to calculate next number in the 'Fibonacci' class by the 'nxt' method. In this class, the 'val' member is a Fibonacci number. The 'nxt' method will return a 'Fibonacci' object whose value is the next number in Fibonacci series. class Fibonacci (): """A Fibonacci number. >>>a = Fibonacci(): >>>a 0 >>>a.nxt() 1 >>>a.nxt().nxt() 1 >>>a.nxt().nxt().nxt() 2 >>>a.nxt().nxt().nxt().nxt() 3 >>>a.nxt().nxt().nxt().nxt().nxt() 5 >>>a.nxt.nxt().nxt().nxt().nxt().nxt() 8 """ def __init__(self): self.val = 0 def nxt(self): """YOUR SOURCE CODE HERE"""...
Part 3 Write a C++ program that prompts the user for an Account Number(a whole number)....
Part 3 Write a C++ program that prompts the user for an Account Number(a whole number). It will then prompt the user for the initial account balance (a double). The user will then enter either w, d, or z to indicate the desire to withdraw an amount from the bank, deposit an amount or end the transactions for that account((accept either uppercase or lowercase). You must use a nested if statements instead of a Switch construct. Test Run: Enter the...
1)Write a C++ program which clearly demonstrates how the dangling else ambiguity is resolved. Your program...
1)Write a C++ program which clearly demonstrates how the dangling else ambiguity is resolved. Your program should print distinct results depending on if C++ uses inner-if or outer-if resolution. Turn in a listing of your program, the output, and a description of how your program demonstrates the semantic resolution of the ambiguity. 2) Give a context-free grammar describing the syntax of the following; Non-empty strings of 0’s and 1’s that represent binary numbers that have odd parity
Code in C++ please You are going to write a program for Computer test which will...
Code in C++ please You are going to write a program for Computer test which will read 10 multiple choice questions from a file, order them randomly and provide the test to the user. When the user done the program must give the user his final score
Write a c++ program of the Fibonacci Sequence. Have the user enter a positive integer n...
Write a c++ program of the Fibonacci Sequence. Have the user enter a positive integer n and compute the nth Fibonacci number. The program should end when the user enters a number less than or equal to zero
Write a program that counts how many Fibonacci numbers are divisible by 3 and smaller than...
Write a program that counts how many Fibonacci numbers are divisible by 3 and smaller than 1000. The program prints the resulting number. You may only use while loops. Use python language
Write a program that counts how many Fibonacci numbers are divisible by 3 and smaller than...
Write a program that counts how many Fibonacci numbers are divisible by 3 and smaller than 1000. The program prints the resulting number. You may only use while loops. Use python language
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT