Smallest value in string array
I need to find the index of the smallest value in a string array alphabetically. Here is what I have but this is not working.
1 2 3 4 5 6 7 8 9 10
|
int smallest(const string source[], int low, int high)
{
int index;
for(int i=low;i<=high;i++)
{
if(source[i]<=source[i+1])
index=i;
}
return index;
}
|
if(source[i]<=source[i+1])
shouldn't you be comparing the current smallest to the current element?
Something like:
1 2 3 4 5 6 7 8 9 10 11 12
|
unsigned smallest(std::string const source[], unsigned const low, unsigned const high)
{
unsigned index = low;
for(unsigned i = low + 1; i <= high; ++i)
{
if(source[i] < source[index])
{
index = i;
}
}
return index;
}
|
THANK YOU!
Topic archived. No new replies allowed.