Question

Fibonacci function Fib(n) is given below. Fib(n)= Fib(n-1) + Fib(n-2) for n > 1 Fib(n)= 1...

Fibonacci function Fib(n) is given below.

Fib(n)= Fib(n-1) + Fib(n-2) for n > 1

Fib(n)= 1 for n=1

Fib(n)= 0 for n=0

Using following initialization

unsigned int *Fib = new unsigned int[30];

and function prototypes

void push(int n)
unsigned int pop( )

insert first 30 Fibonacci numbers into dynamic array Fib and then pop them to print out first 30 numbers in descending order.
The output of your program should be
514229, 317811, 196418, ......, 1, 1, 0

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

Hey,

Note: Brother in case of any queries, just comment in box I would be very happy to assist all your queries

#include <iostream>
using namespace std;

class Fibo
{
public:
int ind=0;
unsigned int *Fib = new unsigned int[30];
void push(int n)
{
Fib[ind++]=n;
}
unsigned int pop( )
{
return Fib[--ind];
}
};

int main(){
Fibo f;
f.push(0);
f.push(1);
int f0=0,f1=1;
for(int i=2;i<30;i++)
{
int f2=f0+f1;
f.push(f2);
f0=f1;
f1=f2;

}
for(int i=0;i<29;i++)
{
cout<<f.pop()<<", ";
}
cout<<f.pop()<<endl;
return 0;
}

Kindly revert for any queries

Thanks.

Add a comment
Know the answer?
Add Answer to:
Fibonacci function Fib(n) is given below. Fib(n)= Fib(n-1) + Fib(n-2) for n > 1 Fib(n)= 1...
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
  • #include and define a procedure void fib(int N) which prints the first N Fibonacci numbers. Have...

    #include and define a procedure void fib(int N) which prints the first N Fibonacci numbers. Have its first line be assert (0 <= N && N <= 46); so that your definition looks like void fib(int N) { assert (0 <= N && N <= 46); // Fill in here. } (Why shouldn't you allow 47, 48, 49, . . .?) in C++

  • Fibonacci Sequence The Fibonacci Sequence is the series of numbers: 0, 1, 1, 2, 3, 5,...

    Fibonacci Sequence The Fibonacci Sequence is the series of numbers: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, ... The next number is found by adding up the two numbers before it. The 2 is found by adding the two numbers before it (1+1) The 3 is found by adding the two numbers before it (1+2), And the 5 is (2+3), and so on!         Example: the next number in the sequence above is 21+34 = 55 Source:...

  • Question 32 (Programming) The Fibonacci sequence of number is defined as: In this question we examine...

    Question 32 (Programming) The Fibonacci sequence of number is defined as: In this question we examine a property of this sequence. a) Write a C function definition with header int fib(int [ ] a, int n) which generates an array, a of the first n Fibonacci numbers. (Hint: You do not have to write this recursively. You just have to generate each array entry from the previous two entries.) b) Two numbers are said to be coprime if they have...

  • M.A.R.I.E assembly code for 7th value of Fibonacci sequence: Fib(1) = 1, Fib(2) = 1, Fib(n)...

    M.A.R.I.E assembly code for 7th value of Fibonacci sequence: Fib(1) = 1, Fib(2) = 1, Fib(n) = Fib(n-1) + Fib(n-2). The program calculates Fib(7). I have 4 errors in my code and cannot seem to find the solutions here is the code so far, ORG 100    Input    Store x    Input    Store y FOR_INIT, LOAD one STORE i FOR_COND, LOAD i SUBT seven SKIPCOND 800 JUMP FOR_BODY JUMP END_FOR FOR_BODY, LOAD x ADD y STORE z LOAD...

  • Extra Credit - Fibonacci Function (Lec. 5 topic: Recursive function and runtime stack. Use recursion to...

    Extra Credit - Fibonacci Function (Lec. 5 topic: Recursive function and runtime stack. Use recursion to calculate the Fibonacci Function 1.) Use a recursive function called fib() to calculate the Fibonacci Function for the following values for the variable n. int n = 10; int n = 20; int n = 30; int n = 40; int n = 45; int n = 46; 2.) In addition to calculating and displaying the results, use a "timer" to see how long...

  • The Fibonacci sequence is the sequence of numbers: 0, 1, 1, 2, 3, 5, 8, 13,...

    The Fibonacci sequence is the sequence of numbers: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, … The next number is found by adding up the two numbers before it. For example, the 2 is found by adding the two numbers before it (1+1). The 3 is found by adding the two numbers before it (1+2). The 5 is found by adding the two numbers before it (2+3), and so on! Each number in the sequence is called...

  • In C++ #include<iostream> using namespace std; //Implement the function below. bool is_fibonacci_array(int*,int); //Param 1: pointer to...

    In C++ #include<iostream> using namespace std; //Implement the function below. bool is_fibonacci_array(int*,int); //Param 1: pointer to the beginning of an array of integers //Param 2: size of that array //Returns: true, is every element in the input array is a Fibonacci number //(the order of the numbers in the array need not be ordered //as per the Fibonacci sequence) //false, otherwise //A Fibonacci sequence is generated as follows. //The first two numbers in the sequence are 0 and 1. //The...

  • Add comments on this Fibonacci C++ program in detail. #include <iostream> using namespace std; int fib(int...

    Add comments on this Fibonacci C++ program in detail. #include <iostream> using namespace std; int fib(int n){ int a = 0,b = 1,c; if(n == 0 || n == 1){ return n; } while(n>2){ c = b + a; a = b; b = c; n--; } return c; } int main(){ int n; cout<<"Enter n: "; cin>>n; int result = fib(n); cout<<"Fib("<<n<<") = "<<result<<endl; return 0; }

  • Fibonacci Function

    Define a function called fibonacci() which has at least one parameter called n. The function prints the first n numbers of Fibonacci sequence. Write a program using the fibonacci() function to print the first 10 numbers of Fibonacci sequence.

  • Write a program in MIPs Assembly Language to compute nth number of a fibonacci number sequence....

    Write a program in MIPs Assembly Language to compute nth number of a fibonacci number sequence. Your program should prompt for an integer input n from the user. The program should call a recursive function to compute the nth fibonacci number. Your program must follow programming convention. You should submit program and screenshot of output in a single word/pdf file. You should use following recursive definition of fibonacci function: fib(0) = 0 fib(1) = 1 fib(n) = fib(n-1) +fib(n-2)

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
ADVERTISEMENT