# Thread: Finding largest number in array

1. ## Finding largest number in array

Why can't I just put grade[0] in the if statement below inside the for loop?

Different code from my project but illustrates what I'm talking about.
Code:
```/*
Determine the average of a elements of an array.
*/

#include <stdio.h>

int main(void){
const int NUM_ELEMS = 10;

double average;
double sum = 0;
int exam;

//for some weird reason I can't just put grade[0] in if statement below

for (exam = 1; exam <= NUM_ELEMS; exam++){
printf("Enter grade for exam %d: ", exam);

}

}

average = sum / NUM_ELEMS;

printf("Average %.1lf\n", average);

return 0;
}```

for example, if I just put it like this..it will just give me the last number I entered

Code:
```		if (grade[exam] > grade[0]){
}```
But...

when I assigned grade[0] to a variable, it does the trick. It displays the highest value of the array with no problems.

I'm sure I'm not understanding a concept somewhere but it's not that obvious to me right now. Thanks.

2. Because when you use grade[0] you are no longer trying to find the largest item in the list, but the last item on the list larger than the first one. If you want to find the largest item in the list, you must compare with the largest-found-so-far, and the largest-found-so-far is not (always) grade[0].

3. That way, you test whether the current value is higher than the first value. If it is, it will be stored. The proper version compares it to the current highest value. If this one exceeds it, it is saved, and it is compared to this one later.

Imagine the array:
1 8 3
It will compare 3 to 1. Since 3>1, it will assume it's the highest so far in the wrong version.

In the correct version, it will set the highest to the first value (which is 1). Then, since 8>1, it will set the highest to 8. Then, it will compare the highest - not the first - with the 3. But since 3<8, it will not change the current highest value.

Try to find out what the loop does for those values for yourself.

Hope that makes sense