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. Display the highest grade */ #include <stdio.h> int main(void){ const int NUM_ELEMS = 10; double grade[NUM_ELEMS]; double h_grade; double average; double sum = 0; int exam; h_grade = grade[0]; //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); scanf("%lf", &grade[exam]); sum = sum + grade[exam]; if (grade[exam] > h_grade){ h_grade = grade[exam]; } } average = sum / NUM_ELEMS; printf("Average %.1lf\n", average); printf("Highest grade %.1lf\n", h_grade); return 0; }
for example, if I just put it like this..it will just give me the last number I entered
But...Code:if (grade[exam] > grade[0]){ h_grade = grade[exam]; }
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.



LinkBack URL
About LinkBacks


