-
More fun with vectors
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!
-
"The question I have is how to pass the Vector and all of it's values/elements properly into this function?? "
Your problem has nothing to do with vectors. To easily prove that, you could print out the contents of the vector inside the function. I'll leave that for you to try. Look at your for-loop:
Code:
for (int i=lindex; i <rindex; i++){
if (A[i]==key) {
return i;
} else {
return rindex+1;
}
}
What happens after the first value in the vector is examined?
-
When I pass List as the parameter 1, 0 for lindex, 4 for rindex, and 6 for key (as in the code), it outputs 5 (which is basically rindex+1). It's goes to the else branch when I would expect it to output 2 since List[2] equals 6. I must be offbase in my expectations...
-
Hi,
"When I pass List as the parameter 1, 0 for lindex, 4 for rindex, and 6 for key (as in the code), it outputs 5 (which is basically rindex+1). "
Yes, I read your initial post.
You can tell your function to print out the number 100,000 instead of rindex +1, and it will display 100,000, and similarly it won't tell you anything about what happened in the function. rindex + 1 is a constant, and calling the function never alters that value.
Try this: get a sheet of paper and write "i" and "List[x]" across the top so that you have two columns. Then, go through the function changing the values in the columns as each line of the code is executed until the function ends and see what happens--I think you will be surprised.
-
Ahhh I think I see what you're saying... when I dropped the else branch completely from the function, it returns "2", as I would expect it to. I think I understand now... unless I just got lucky on that one? :) Thanks!
-
The problem was you made the function return in both branches of the if statement, so no matter what the value was in the first position of the vector, the function ended before it could check the whole vector.
You could write the for-loop like this to accomplish what you want:
Code:
for (int i=lindex; i <rindex; i++)
{
if (A[i]==key) return i;
}
return i + 1; // can only be executed if 'key' isn't found
-
The design of the function is severely flawed, what if I wanted to use the function in this way:
Code:
cout << Find(List,0,1,6);
Logically, this would mean that I am looking for the value 6 within the first two elements of the vector, i.e. in either the List[0] or List[1] positions. Because of the way the function is designed, it would not find 6 in those positions and therefore it would return rindex+1 which is 2 and a correct answer except that is does not occur within the range that you specified. You should probably return a value such as -1, an index that would never exist in an array, when the value is not found.
To this end, you can either modify your function to return this -1 value, or as a learning experience using the built in STL functions in the <algorithm> header, you could change the function to this:
Code:
int Find(const vector<int>& A, int lindex, int rindex, int key)
{
int dist = distance(A.begin(),find(A.begin()+lindex,A.end(),key));
return (dist > rindex) ? -1: dist;
}
This would correctly return the value -1 if called like Find(List,0,1,6).