Good job LightYear! It works now.
Good job LightYear! It works now.
The inner loop refers to each individual score.
If you want a letter grade as a summary, then do it after that inner loop.
Code:#include <stdio.h> #define N 10 int main(void) { double grade[N], highscore, average_score, sum; int i, numraise, studentname, 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]; } average_score= sum/10; for (studentname = 0; studentname < 10; studentname++) if (score >= 90) score='A'; else if (score >= 80) score='B'; else if (score>= 70) score='C'; else if (score>=60) score='D'; else score='F'; { printf("Student %d's grade is %.1lf, this is a %d", studentname+1, grade[studen tname], grade); } printf("The highest score is %.1lf\n", highscore); printf("The average score is %.2lf\n", average_score); return 0; }
hmmm it returns this when executed:
admiral% gcc testgrades.c
admiral% a.out
Enter 10 grades: 10 10 10 10 10 10 10 10 10 90
Student 11's grade is 0.0, this is a -4195384The highest score is 90.0
The average score is 18.00
What do you think? (thanks for the help btw this is very interesting)
So average score looks good, and highest, but where did this student number 11 stuff come from?
NOTE: oh crap that bracket is misplaced after the for loop isnt it let me go fix that
1) score is undefined. You need to compare grade[i] not score. Also, when comparing make sure you compare double values to double values:
i.e. if(grade[i] >= 90.0)
2) when printing out the grade the correct flag is %c not %d because it is a char not an int. SCORE should be type char.
I'm going offline now, it's late here and it's time for my beauty sleep.
Good luck!
now with the bracket at
producesCode:average_score= sum/10; for (studentname = 0; studentname < 10; studentname++) { // Bracket moved here if (score >= 90) score='A'; else if (score >= 80) score='B'; else if (score>= 70) score='C'; else if (score>=60) score='D'; else score='F'; { //This removed printf("Student %d's grade is %.1lf, this is a %d", studentname+1, grade[studen tname], grade); }
admiral% gcc testgrades.c
admiral% a.out
Enter 10 grades: 10 10 10 30 30 30 90 90 90 100
Student 1's grade is 10.0, this is a -4195384Student 2's grade is 10.0, this is a -4195384Student 3's grade is 10.0, this is a -4195384Student 4's grade is 30.0, this is a -4195384Student 5's grade is 30.0, this is a -4195384Student 6's grade is 30.0, this is a -4195384Student 7's grade is 90.0, this is a -4195384Student 8's grade is 90.0, this is a -4195384Student 9's grade is 90.0, this is a -4195384Student 10's grade is 100.0, this is a -4195384The highest score is 100.0
The average score is 49.00
Oh I thought characters were treated as integers oops
How's this
Code:#include <stdio.h> #define N 10 int main(void) { double grade[N], highscore, average_score, sum; int i, numraise, studentname,; 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]; } average_score= sum/10; for (studentname = 0; studentname < 10; studentname++) { if (score >= 90) sum += grade[i]; if (highscore < grade[i]) highscore = grade[i]; } average_score= sum/10; for (studentname = 0; studentname < 10; studentname++) { if (i >= 90.0) score='A'; else if (i >= 80.0) score='B'; else if (i>= 70.0) score='C'; else if (i>=60.0) score='D'; else score='F'; printf("Student %d's grade is %.1lf, this is a %c", studentname+1, grade[studen tname], score); //Replaced grade here with score } printf("The highest score is %.1lf\n", highscore); printf("The average score is %.2lf\n", average_score); return 0; }
Still getting an error, but it's only some syntactical error. The logic is right tho yes?
Last edited by LightYear; 04-08-2010 at 11:01 PM.
last question I promise!
Here is near final code
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]; } average_score= sum/10; for (studentname = 0; studentname < 10; studentname++) { 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'; 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; }
Why does it print all grades as being F?
Should I move the if then statements into the first for loop?
Because i is never more than 10, let alone 60.
Oh so it would be grade[i] > 90 ?
I substituted grade[i] in for every i in the if then statements and now all grades are A's !!!
score is a double, so you want to add up a student's marks, and put them into score.
First, set score to 0.0 (I believe you do that)
Then every time through the loop, you should be having:
score += grade[i];
that line of code in your for loop.
Letters play no part in this. Letters should go to your char variable you want to use for the student's finalGrade.
Make sense?
Just replace "studentName" with i, FCOL. Use i for your loop iterator, if possible!
so at the beginning,
char score, A, B, C, D, F;
but score is a char, not a double?
I'm a little confused as to what the score += grade[i] will do for me being that score is a char letter.
I am going to rename score as "Lettergrade" to make it easier to understand.
Code:#include <stdio.h> #define N 10 int main(void) { double grade[N], highscore, average_score; int sum, numraise, studentname, i; char lettergrade; 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 (grade[i] >= 90.0) lettergrade='A'; else if (grade[i] >= 80.0 && grade[i] <90.0) lettergrade='B'; else if (grade[i] >= 70.0 && grade[i] <80.0) lettergrade='C'; else if (grade[i] >= 60.0 && grade[i] <70.0) lettergrade='D'; else if (grade[i] < 60.0) lettergrade='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], lettergrade); } printf("The highest score is %.1lf\n", highscore); printf("The average score is %.2lf\n", average_score); return 0; }
see what i mean?
Last edited by LightYear; 04-08-2010 at 11:51 PM.