Good day and welcome aboard. A few things.
You could use something like this:
int smallestIndex( int arr[], int size)
{
int smallestIndex=0;
int temp=arr[0];
for(int i=0;i<size;i++)
{
if(arr[i]<temp)
{
smallestIndex = i;
temp=arr[i];
}
}
return i;
}
Hope that helps.
zandiago
Nearly a Posting Maven
2,480 posts since Jun 2007
Reputation Points: 129
Solved Threads: 26
Perhaps return smallestIndex instead?
> but I keep getting a warning at first then it says fatal error that
> variable i hasn't been initialized!
How many i variables are in your function - I count TWO.
Guess which one you're returning - the uninitialised one.
Salem
Posting Sage
11,531 posts since Dec 2005
Reputation Points: 5,862
Solved Threads: 953
you could also
a. make the function const-correct
b. use a size_t instead of an int for the size
size_t smallestIndex( const int arr[], size_t size)
{
size_t smallestIndex=0;
int temp=arr[0];
for(size_t i=1;i<size;i++)
{
if(arr[i]<temp)
{
smallestIndex = i;
temp=arr[i];
}
}
return smallestIndex;
}
vijayan121
Posting Virtuoso
1,606 posts since Dec 2006
Reputation Points: 1,159
Solved Threads: 287
If you've got the correct answer....and the probelm is solved....please post it for the benefit of the entire community.
zandiago
Nearly a Posting Maven
2,480 posts since Jun 2007
Reputation Points: 129
Solved Threads: 26