Question

I need to create fibonacci's formula in C++ using a recursive and non-recursive formula. After doing...

I need to create fibonacci's formula in C++ using a recursive and non-recursive formula. After doing so, I have to time each operation for for the following numbers of the sequence:

1, 5, 10, 15, 20, 25

I have to time how long the program took to perform each number recursive vs non recursive. Code in C++

0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include <iostream>

using namespace std;

long FibRecurive(int n){

if(n==0)

return 1;

else if(n==1)

return 1;

else if(n==2)

return 2;

else

return FibRecurive(n-1) + FibRecurive(n-2);

}

long FibIterative(int n){

int a = 0, b = 1, c = 0;

for(int i=1; i<=n ; ++i){

c = a + b;

a = b;

b = c;

}

return c;

}

int main()

{

clock_t start, end;

start = clock();

for(int i=1; i<=5; ++i)

FibRecurive(i);

end = clock();

cout<<"Time Taken by Recurive for 5 element is "<<(end - start)/1000000.0<<endl;

start = clock();

for(int i=1; i<=5; ++i)

FibIterative(i);

end = clock();

cout<<"Time Taken by Iterative for 5 element is "<<(end - start)/1000000.0<<endl;




start = clock();

for(int i=1; i<=20; ++i)

FibRecurive(i);

end = clock();

cout<<"Time Taken by Recurive for 20 element is "<<(end - start)/1000000.0<<endl;

start = clock();

for(int i=1; i<=20; ++i)

FibIterative(i);

end = clock();

cout<<"Time Taken by Iterative for 20 element is "<<(end - start)/1000000.0<<endl;

return 0;

}

======================================================
SEE OUTPUT

PLEASE COMMENT if there is any concern.

===========================================================

Add a comment
Know the answer?
Add Answer to:
I need to create fibonacci's formula in C++ using a recursive and non-recursive formula. After doing...
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. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • I need help with this code. Im using C++ coding. Non Recursive (iterative) Fibonacci Write a...

    I need help with this code. Im using C++ coding. Non Recursive (iterative) Fibonacci Write a program that uses a for loop to calculate a Fibonacci sequence (NOT RECURSIVE!!!) up to a given position, starting with position 0. This function defines a Fibonacci sequence: If the number is 0, the function returns a 0 If the number is 1, the function returns a 1 If the number is higher than 1, it returns the sum of the previous two numbers...

  • Please write code in C++ using recursive function Write a program that computes the sequence of...

    Please write code in C++ using recursive function Write a program that computes the sequence of Fibonacci numbers. The formula for generating the next Fibonacci number is: Fn = Fn−1 +Fn−2, where F1 = 1 and F2 = 2. For example, F3 = F2 + F1 = 2 + 1 = 3. You will notice that at some point Fibonacci numbers are too large and they do not fit in type int. This is called the integer overflow. When they...

  • I need trying to figure out how I can make create a code in Python with this exercise for I am having trouble doing so. Miles Per Gallon Calculator Write a GUI program that calculates a car's gas...

    I need trying to figure out how I can make create a code in Python with this exercise for I am having trouble doing so. Miles Per Gallon Calculator Write a GUI program that calculates a car's gas mileage. The program's window should have Entry widgets that let the user enter the number of gallons of gas the car holds, and the number of miles it can be driven on a full tank. When a Calculate MPG button is clicked,...

  • Using C++ Create a program that performs the Bubble Sort, the Insertion Sort, and the Selection...

    Using C++ Create a program that performs the Bubble Sort, the Insertion Sort, and the Selection Sort with arrays of varying lengths. You will time each algorithm. The algorithms' time complexities should allow us to predict how these algorithms will perform. Program needs Four separate arrays, each with the same length and filled with the same sequence of randomly chosen numbers. Four separate functions, one for each of the four algorithms. All four functions will need to be timed. Be...

  • I need to create a Tic Tac Toe program in C++. These are the requirements Write...

    I need to create a Tic Tac Toe program in C++. These are the requirements Write a program that allows the computer to play TicTacToe against a human player or allow two human players to play one another. Implement the following conditions: The player that wins the current game goes first in the next round, and their symbol is X. The other player will be O. Keep track of the number of games played, wins, and draws for each player....

  • C++ Create three recursive functions that accomplish the following: Recursive Power Function this function will raise...

    C++ Create three recursive functions that accomplish the following: Recursive Power Function this function will raise a number to a power. The function should accept two arguments, the number to be raised and the exponent. Assume that the exponent is a non-negative integer. String Reverser function that accepts a string object as its argument and prints the string in reverse order. Sum of Numbers this function accepts an integer argument and returns the sum of all the integers from 1...

  • C - Language Create a recursive function to print a multi-digit number vertically. For example, 2378...

    C - Language Create a recursive function to print a multi-digit number vertically. For example, 2378 should be printed as 2 3 7 8 Be sure to test your program with numbers of different length. The recursive function should return an int and take an int as a parameter. The function should have a base case where the parameter is 0 and this should return 0 Define a temp variable using the remainder operator where temp is equal to the...

  • 13) Using non-randomized Select(A, n, i) to find the 4h largest element in array A, with A as ท first pivot 8 11 14...

    13) Using non-randomized Select(A, n, i) to find the 4h largest element in array A, with A as ท first pivot 8 11 14 0 9 4 25 33 98 5 22 63 54 2 111 4 I put the first pivot in the right place for you 45 23 19 728 16 116 10 w the array in sequence, one in each row, after each recursive call of the algorithm. You must use blanks (or% out) for the parts...

  • What's wrong with my code? : I'm trying to use recursive functions to display and count...

    What's wrong with my code? : I'm trying to use recursive functions to display and count all the even numbers of an array. However, I can't seem get the program output the number of even integers . Here's the output I want: Here are the 5 even numbers: // Luckily, however, my code does output all the even numbers. But, I also want the program to tell me how may even integers there are. 2 8 14 18 22 MY...

  • Please Only use C language In this assignment, you have to read a text file (in.txt)...

    Please Only use C language In this assignment, you have to read a text file (in.txt) that contains a set of words. The first line of the file contains 3 numbers (N, S, D). These numbers represent the sequence of input words in your file. N: represents the number of words to read to build a binary search tree. You have to write a recursive insert code to create and insert these words into the binary search tree. After inserting...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT