Hi again,
I have a different question on vectors. The following code is simply used to demonstrate the use of vectors and passing values into a function. I have initialized a vector<int> called List and initalized it with 5 elements intially set to 0. I subsequently hard-coded values for for these 5 elements, then attempted to pass the values of the List vector into the Find function. I know that I am not doing something right on this because when I run this it returns "5" (which is rindex+1) meaning that it did not find the key (6) in any of the elements of List. The question I have is how to pass the Vector and all of it's values/elements properly into this function?? Thanks! The code is below:
Code:
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int Find(vector<int> A, int lindex, int rindex, int key);
int main () {
vector<int> List(5,0);
List[0]=2;
List[1]=4;
List[2]=6;
List[3]=8;
List[4]=10;
cout << Find(List,0,4,6);
cout << "\n";
return 0;
}
int Find(vector<int> A, int lindex, int rindex, int key){
for (int i=lindex; i <rindex; i++){
if (A[i]==key) {
return i;
} else {
return rindex+1;
}
}
return 0;
}
Thanks again!