I have written a basic program to find the largest number in an array and print it:
#include <stdio.h>
int main(void)
{
int numbers[] = {3, 5, 12, 1, 4};
int largest;
largest = numbers[0];
for (int i = 0; i < sizeof(numbers) / sizeof(numbers[0]); i++)
{
if (largest < numbers[i])
{
largest = numbers[i];
}
}
printf("The largest number in the array is %i\n", largest);
}
What would I need to do so that instead of printing The largest number in the array is 12
, it prints The largest number in the array is numbers[2]
?
Instead of using the value, you would use the index: