Can you please help me with this problem by using C++ 1. Write the fibonacci fun
ID: 3797596 • Letter: C
Question
Can you please help me with this problem by using C++
1. Write the fibonacci function described in Exercise 3 at the end of Chapter 8, and a main which reads x, y, and n, calls fibonacci to fill the vector, and then prints the ratio of the last element of v divided by the next to last element. For example, an input of 1 2 7 prints a ratio of 1.61538.Fun fact: No matter what x and y are, the larger n is, the closer the ratio is to the golden mean!
Exercise 3 is the following (Please Solve the problem above not the exercise ) :
- Write the fibonacci function described in Exercise 3 at the end of Chapter 8, and a main which reads x, y, and n, calls fibonacci to fill the vector, and then prints the ratio of the last element of v divided by the next to last element. For example, an input of 1 2 7 prints a ratio of 1.61538. Fun fact: No matter what x and y are, the larger n is, the closer the ratio is to the golden mean!
Explanation / Answer
// C++ code
#include <iostream>
#include <fstream>
#include <cctype>
#include <cstring>
#include <stdlib.h> /* srand, rand */
#include <iomanip>
#include <limits.h>
#include <cmath>
#include <algorithm>
#include <vector>
#include <stack>
#include <string.h>
using namespace std;
void fibonacci (int x,int y,vector<double> v,int n)
{
v.push_back(x);
v.push_back(y);
double ratio;
int size;
for(int i=2;i<n;i++)
{
v.push_back(v[i-1]+v[i-2]);
}
size=v.size();
ratio= sqrt((v[size-1])/(v[size-3]));
cout<< "Ratio: " << ratio << endl;;
}
int main()
{
int x,y,n;
vector<double> v;
cout<<"Enter x, y, n: ";
cin>>x>>y>>n;
fibonacci(x,y,v,n);
return 0;
}
/*
output:
Enter x, y, n: 1 2 10
Ratio: 1.61791
Enter x, y, n: 1 2 100
Ratio: 1.61803
*/