Well frankly speaking, it seems that your asking to break the rules, or modify, the Fibonacci sequence and algorithm in order to retrieve every other number??
And btw your calculation of the Fibanacci-numbers is wrong.
n= n+1 + n+2 is not the propper calculation
The next Fibanacci number is the sum of the previous nuber and the one previous to that.
So, in terms of c++:
I do think you should try and figure it out for yourself. If you want to be a programmer it's the kind of thing you should be (will be!) able to just write down!
Removing the values is lame!
Andy
P.S. You just need a single extra int, and a little bit more code...
A few algebraic substitutions can help here.
If I write:
1)
Fn = Fn-1 + Fn-2
and 2)
Fn+1 = Fn + Fn-1
Then I can make substitutions into:
Fn+2 = Fn+1 + Fn
Use the first 2 identities to eliminate Fn+1 in the last.
Using 2)
Fn+2 = ( Fn + Fn-1 ) + Fn
Then, from 1) solved for Fn-1
Fn+2 = Fn + ( Fn - Fn-2 ) + Fn
Or finally:
Fn+2 = 3*Fn - Fn-2
Example: Given F0 = 1 and F2 = 2 this can be used to obtain F4 directly.
For n=2: F4 = 3*F2 - F0 = 3*2-1 = 5.
and so on for F6, F8, etc...
To get The odd numbered ones start from F1 and F3 instead.
My solution uses the standard equation, pretty ,much exactly like your original code.
But my loop calculate two new values at a time. I keep the first and "skip" the second (you can't totally skip the value, but it doesn't go in the array).
#include <iostream>
#include <vector> //STL cointainer of self escalable vector
class Fibonacci
{
public:
void fibonacci(int x)
{
int y = x+1; //'x' = desired number of terms
//vector of integers named 'seq'
std::vector< int > seq;
//resize the vector for y elements
seq.resize(y);
seq[0]=0;
seq[1]=1;
std::cout << "Fibonacci Sequence\n{ " << seq[1] << ",";
//traverses the vector until the last element
for (int i = 2; i < seq.size(); i++)
{
//evaluate of fibonacci sequence
seq[i] = seq[i-1] + seq[i-2];
if (i==(seq.size()-1))
{
std::cout << " " << seq[i];
}
else
{
std::cout << " " << seq[i] << ",";
}
}
std::cout << "... }";
}
}