Kodewick
Start learning →
Starter←→0 / 50

Max and its index

#040Startera[i] > a[best]4 min

Find which cell holds the biggest number in an array (index from 0); the max itself is then a[index]. Write max_index(a, n) to return that index; if several are tied for biggest, return the first one. 3 9 2 7 returns 1, and 5 1 5 returns 0. The array has at least 1 number.

Examples
max_index({3, 9, 2, 7}, 4)→1
max_index({5, 1, 5}, 3)→0
The functionint max_index(int a[], int n)
main.c
int max_index(int a[], int n) {
    int best = 0;
    for (int i = 1; i < n; i++) {
        // your code here: if a[i] is bigger than the biggest so far, remember i
    }
    return best;
}
Ctrl + Enter runs too
Result

Run tries the examples. When they look right, Submit.