More fun with vectors

This is a discussion on More fun with vectors within the C++ Programming forums, part of the General Programming Boards category; Hi again, I have a different question on vectors. The following code is simply used to demonstrate the use of ...

  1. #1
    Registered User
    Join Date
    Mar 2003
    Posts
    72

    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!

  2. #2
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    "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?
    Last edited by 7stud; 05-13-2003 at 08:13 PM.

  3. #3
    Registered User
    Join Date
    Mar 2003
    Posts
    72
    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...

  4. #4
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    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.
    Last edited by 7stud; 05-13-2003 at 08:23 PM.

  5. #5
    Registered User
    Join Date
    Mar 2003
    Posts
    72
    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!

  6. #6
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    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
    Last edited by 7stud; 05-13-2003 at 10:14 PM.

  7. #7
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,806
    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).
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Vectors
    By naseerhaider in forum C++ Programming
    Replies: 11
    Last Post: 05-09-2008, 09:21 AM
  2. How can i made vectors measuring program in DevC++
    By flame82 in forum C Programming
    Replies: 1
    Last Post: 05-07-2008, 03:05 PM
  3. How properly get data out of vectors of templates?
    By 6tr6tr in forum C++ Programming
    Replies: 4
    Last Post: 04-15-2008, 11:35 AM
  4. How to use Vector's in C++ !?!
    By IndioDoido in forum C++ Programming
    Replies: 3
    Last Post: 10-14-2007, 12:13 PM
  5. Points, vectors, matrices
    By subnet_rx in forum Game Programming
    Replies: 17
    Last Post: 01-11-2002, 02:29 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21