c++ - I can not figure out how to pass a vector back to main after it has been through the loop -


#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;    (int j=0; j<n; j++)        cout<<vi[j]<<" "<<endl;  } /*void reversev(vector<int> vi,int n) {     vector<int> rv= vi;     (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 max number of times want add"<<endl;     cin>>n; cout<<"enter first 2 fibonacci numbers"<<endl; cin>>x>>y;    fibonacci(x,y,vi,n);  printv("vector: ",vi,n);  printv("vector: ",rv,n);  //vi.swap(rv); } 

you shouldn't passing vector in, create new 1 , 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, need capture return value:

vi = fibonacci(x,y,n); 

Comments

Popular posts from this blog

apache - Add omitted ? to URLs -

redirect - bbPress Forum - rewrite to wwww.mysite prohibits login -

php - How can I stop spam on my custom forum/blog? -