Finding largest element in array
Hey everyone,
So I'm doing this programming exercise to make an array of ints that changes in size each time the prog is run. So, I'm supposed to create a function that goes through the final array and plucks out the largest number and the index of that number...here's what I came up with...although it seems bulky...I was just wondering if there was a better way to go about this than what I did here:
Code:
int maxint(int bunch[], int count)
{
int i, largest = 0;
for (i = 0; i < count; i++)
{
if (largest < bunch[i])
largest = bunch[i];
}
for (i = 0; i < count; i++)
{
if (largest == bunch[i])
return i;
}
}
I felt like the second for loop was a bit overkill, but I couldnt figure out a way to combine the for loops...this is just a question about various solutions to the same problem. I think it would be cool to see other thought processes in action...
peace, Chap