# Thread: Please help with last detail on this

1. ## Please help with last detail on this

Why does this code return all the grades as F, and not A B C or D

Code:
```#include <stdio.h>

#define N 10

int main(void)

{

double grade[N], highscore, average_score;
int sum, numraise, studentname, i;
char score;
highscore=0.0;
sum=0.0;

printf("Enter %d grades: ", N);
for ( i=0 ; i<N ;i++ )

{
scanf("%lf", &grade[i]);
sum += grade[i];
if (highscore < grade[i])
highscore = grade[i];

if (i >= 90.0)
score='A';
else if (i >= 80.0 && i< 90.0)
score='B';
else if (i >= 70.0 && i< 80.0)
score='C';
else if (i >= 60.0 && i< 70.0)
score='D';
else if (i < 60.0)
score='F';
}

average_score= sum/10;

for (studentname = 0; studentname < 10; studentname++)
{

printf("Student %d's grade is %.1lf, this is an %c\n", studentname+1, grade[stu
dentname], score);
}
printf("The highest score is %.1lf\n", highscore);
printf("The average score is %.2lf\n", average_score);

return 0;
}```

Thank you!

2. Why is every grade returned an A?

3. Okay it's taking the last entered grade and using that to compute the letter grade for all the inputs, I see that much...

4. You're using i as your loop counter, and then comparing it to your grade constants.

There is no point within your current loop (when N is defined as 10) where i is anything greater than 60.

I think you want to compare the constants with grade[i] instead.

Oh, and in the interest of flexibility/ease-of-change, you might want to change average = sum / 10 to average = sum / N. Otherwise, if you change N you will get the wrong average.

Also, you will probably want to change N to a less common name. I think that the C preprocessor will still replace defined constants if they are in the middle of a word. For example, if you had a variable called Number, it would give you a compiler error because it would replace the N with 10. I don't think this is actually an issue in this particular piece of code, but it's good practice.

5. This is a duplicate thread, please don't post in it.

See the real thread "need guidance on project", also by Lightyear, and up to page 5 already.

Popular pages Recent additions