using namespace std; void fibonacci(int x, int y, vector<int>& v, int n) { if (n
ID: 3542693 • Letter: U
Question
using namespace std;
void fibonacci(int x, int y, vector<int>& v, int n)
{
if (n<1) return;
if (1<=n) v.push_back(x);
if (1<=2) v.push_back(y);
for (int i=2; i<n; ++i) {
int z = x+y;
v.push_back(z);
x = y;
y = z;
}
}
int main()
try
{
vector<int> vtest;
vtest.push_back(2);
vtest.push_back(4);
vtest.push_back(2);
vtest.push_back(-1);
print(vtest,"vtest");
cout<< "please enter two integer values (to be used to start a Fibinacci series) and the number of elements of the series: ";
int val1 = 0;
int val2 = 0;
int n;
while (cin>>val1>>val2>>n) {
vector<int> vf;
fibonacci(val1,val2,vf,n);
cout << "fibonnaci(" << val1 << "," << val2 << ") ";
print(vf/n,"");
cout << "Try again: ";
}
}
Explanation / Answer
#include <iostream>
#include <vector>
using namespace std;
void fibonacci(int x, int y, vector<int>& v, int n)
{
if (n<1) return;
if (1<=n)
v.push_back(x);
if (1<=2) v.push_back(y);
for (int i=2; i<n; ++i)
{
int z = x+y;
v.push_back(z);
x = y;
y = z;
}
}
int main()
{
cout<< "please enter two integer values (to be used to start a Fibinacci series) and the number of elements of the series: ";
int val1 = 0;
int val2 = 0;
int n;
//cin>>val1>>val2>>n;
while (cin>>val1>>val2>>n)
{
vector<int> vf;
fibonacci(val1,val2,vf,n);
//cout << "fibonnaci(" << val1 << "," << val2 << ") ";
cout <<"Generated fibonacci series are :";
for(int i=0; i<vf.size(); i++)
cout << vf[i] << " ";
cout << endl;
cout << static_cast<double> (vf[vf.size()-2])/static_cast<double> (vf[vf.size()-1]) << endl;
}
system("pause");
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.