so for a class assignment, i have to write a function that performs a linear search and is recursive, i have been looking up stuff and just havent been able to figure it out. The function header below is provided for us for this assignment. (I know there is a much better way, but have to do this for proof of concept for later lab).
function call
position = search(teamInfo, teamInfo.size(), value);
teamInfo is a vector filled with ids and member names
teamInfo.size() passes in the size of my vector
value is what i am looking for
i think my issue is how do i keep the index(i) from going back to zero when the recursive function call takes place. I have tried using static int i; but when i do a search again it doesn't reset to zero. Over all im trying to return the subscript to position
any help with this would be great
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
|
int search(const vector <Team> & tV, unsigned int size, int id )
{
static int i = 0;
if (tV[i].idNum == id)
{
return position = i;
}
else if ((unsigned)(i+1) < size)
{
i+=1;
return search( tV, size, id);
}
else
{
return i = -1;
}
}
|
thanks