Hello!
I have been working on the this code to output a fibonacci sequence:
The user inputs the first two numbers followed by how many times the numbers will be added.
I want to take the last two additions of the sequence and divide the last by the second to last.
As an example, a valid input is 1 3 5 and the output would be 1,3,4,7,11.
I want to divide the 7 by 11 and output that result. (the result should always end up being around 1.6) Thats the part I am having trouble on, where would I call the vectors of n and n-1 and how would I output them? This is the code (the header file is attached as well)
As you can probably tell; im new to the forum and I hope i attached everything in the proper format. I apologize if I didnt...
Thank you for your help!!!
Code:
#include "std_lib_facilities.h"
void print(vector<int>& v, const string& name)
{
cout << name << ": (" << v.size() << ") { ";
for (int i = 0; i<v.size(); ++i) {
cout << v[i];
if (i<v.size()-1) cout << ", ";
}
cout << " }\n";
}
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; // next element
v.push_back(z);
x = y; // move the sequence on
y = z;
}
}
int main()
try
{
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) { // read two integers
vector<int> vf;
fibonacci(val1,val2,vf,n);
cout << "fibonnaci(" << val1 << "," << val2 << ") ";
print(vf,"");
cout << "The value of the quotient between the last n and n-1 is: "<< "\n";
cout << "Try again: ";
}
}
catch (runtime_error e) {
cout << e.what() << '\n';
}
catch (...) {
cout << "exiting\n";
}