# Thread: Fibonacci Sequence: Dividing Vector HELP!

1. ## Fibonacci Sequence: Dividing Vector HELP!

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...

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";
}```

2. The user inputs the first two numbers followed by how many times the numbers will be added. ... As an example, a valid input is 1 3 5 and the output would be 1,3,4,7,11.

A) 5 additions to take place or
B) Do you want to stop once your reach the 5th element of your sequence?
There is a difference between the two.

Either way, you don't need a vector for what you're trying to do; you just need to keep track of the last two values. Try something like this (warning: no error checking and minimal debugging, assumes you want choice A, but modifying this for choice B should be easy)

Code:
```int n1 = // Get from user
int n2 = // Get from user
int n3 = // Get from user

// We add two values every time
for (int i = 0; i < n3 - 2; i += 2) {
n1 += n2 ;
n2 += n1 ;
}

// Take care of the last values
n1 += n2 ;

float f1 = 0.0 ;

if (n3 & 1) { // If n3 is odd
f1 = (float) n1 / n2 ;
} else { // If n3 is even
n2 += n1 ;
f1 = (float) n2 / n1 ;
}

return f1 ;```