#include <vector>
#include <iostream>
using namespace std;
vector<int> fibonacci(int x, int y,vector<int> vi,int n){
vi[0]=x;
vi[1]=y;
for(int i=2;i<n;i++){
x=vi[i-2];
y=vi[i-1];
vi[i]=x+y;
cout<<vi[i]<<" "<<endl;
}
return vi;
}
void printv(string label, vector<int> vi, int n){
cout<<label<<" "<<endl;
for (int j=0; j<n; j++)
cout<<vi[j]<<" "<<endl;
}
/*void reverseV(vector<int> vi,int n)
{
vector<int> rv= vi;
for (int i=rv.size()-1; i>=0; i--) {
cout << rv[i] << endl;
}*/
//}
int main()
{
int x;
int y;
vector<int> vi;
vector<int> rv;
int n=100;
string label;
cout<<"enter the max number of times you want it to add"<<endl;
cin>>n;
cout<<"enter the first two fibonacci numbers"<<endl;
cin>>x>>y;
fibonacci(x,y,vi,n);
printv("vector: ",vi,n);
printv("vector: ",rv,n);
//vi.swap(rv);
}

- 114,404
- 31
- 268
- 329

- 1
-
please fix your code formatting – Sam Miller Feb 18 '11 at 22:24
-
^^^ What he said. Highlight your code and click the <101010> button. – Emile Cormier Feb 18 '11 at 22:25
-
2Welcome to Stack Overflow! While I'd like to help out, your question is so vague that I really don't know what it is that you're trying to do. Can you clarify your question? – templatetypedef Feb 18 '11 at 22:26
4 Answers
You shouldn't even be passing a vector in, just create a new one and return that:
std::vector<int> fibonacci(int x, int y, int n)
{
std::vector<int> vi(n);
vi[0] = x;
vi[1] = y;
//etc...
return vi;
}
Then, in main, you just need to capture this return value:
vi = fibonacci(x,y,n);

- 101,917
- 9
- 204
- 274
Your program contains several errors, for example:
std::vector
already knows the number of contained elements, if you pass a vector there's no need to specify the size.Elements in a vector must be allocated, you can't just assign to them with
v[i]=x
like you can do e.g. in javascript. Either you first give a size to the vector or you add elements withpush_back
.Apparently you don't know the difference between passing by value and by reference.
But the biggest error is that you are trying to learn C++ by experimentation, and this is a suicidal approach for many reasons.
Much easier and better is to learn C++ by first reading a good book. You can find a list here.
-
+1 for "learning C++ by experimentation is a suicidal approach". It becomes much more effective after some expertise is developed. – Ben Voigt Feb 18 '11 at 22:44
For the immediate question you just missed assigning the return value from your function back to vi
.
rv = fibonacci(x,y,vi,n);
EDIT:
There are a few other problems though, like you never actually allocated space on your vector, and you passed by value into the function AND returned it. Since you seem to want to not change the original in place, just use a local vector
.
vector<int> fibonacci(int x, int y,int n){
vector<int> vi(n);

- 95,107
- 10
- 109
- 188
You could pass your vector by reference rather than by value. To achieve this, change the declaration of your function to
void fibonacci(int x, int y,vector<int>& vi,int n){ ...
This way you are working on the same vector object inside the function, not just on a copy of it. Thus every change you make to the vector is visible outside the function too.

- 114,404
- 31
- 268
- 329