grades
The Task
- Calculate the section average give the breakdown of grades.
The Method
- Have an array called scores, whose size is the maximum section size
- Have an array called grades, that will hold the number of A's, B's, etc.
Let the index match the quality points for that grade.
- Initialize the elements of both arrays to 0
- Get number of students in the section from user
- Get a score at a time from the user until done
- Store each grade in the scores array, and keep a running total.
- Increment the corresponding grade array element.
- Calculate & print average
- Print chart - frequency of grades
The Program
/*****************************************
** File: grades.c
** Author: S. Bogar
** Date: 8/25/99
** Section: 101
** EMail: bogar@cs.umbc.edu
**
** This program calculates the section average
** and gives a breakdown of the grades (e.g.
** 6 As, 10 Bs ...) for a section of MAX or
** less students.
*****************************************/
#include
#define MAX 75
int main ( )
{
int scores[MAX], grades[5], i, students ;
float total, average;
total = 0;
/* Initialize grades array to zeros */
for (i = 0; i < 5 ; i++)
{
grades[i] = 0 ;
}
/* Initialize scores array to zeros */
for (i = 0; i < MAX ; i++)
{
scores[i] = 0 ;
}
/* Prompt for # of students in the section */
printf ("Enter # of students in this section: ");
scanf ("%d", &students);
/* Catch errors in section size */
while (students > MAX || students < 1)
{
printf ("Invalid # of students\n");
printf ("Section size is 1 - %d\n", MAX);
printf ("Enter # of students in this section: ");
scanf ("%d", &students);
}
/* Get scores from user */
for (i = 0; i < students; i++)
{
printf ("Enter score for student %d: ", i + 1);
scanf ("%d", &scores[i]);
/* Accumulate total */
total += scores[i];
/* Increment appropriate grade frequency*/
switch ( scores[i] / 10 )
{
case 10:
case 9: grades[4]++;
break;
case 8: grades[3]++;
break;
case 7: grades[2]++;
break;
case 6: grades[1]++;
break;
case 5:
case 4:
case 3:
case 2:
case 1:
case 0: grades[0]++;
break;
default: printf ("That's an invalid grade\n");
}
}
/* Print out results */
average = total / students;
printf ("\n\nThe section average is %.3f\n\n",
average);
printf ("There were : %5d A's\n", grades[4]);
printf (" %5d B's\n", grades[3]);
printf (" %5d C's\n", grades[2]);
printf (" %5d D's\n", grades[1]);
printf (" %5d F's\n\n", grades[0]);
return 0;
}
The Sample Run
linux1[77] % gcc -Wall -ansi grades.c
linux1[78] % a.out
Enter # of students in this section: 10
Enter score for student 1: 83
Enter score for student 2: 77
Enter score for student 3: 101
Enter score for student 4: 68
Enter score for student 5: 59
Enter score for student 6: 8
Enter score for student 7: 75
Enter score for student 8: 90
Enter score for student 9: 88
Enter score for student 10: 79
The section average is 72.800
There were : 2 A's
2 B's
3 C's
1 D's
2 F's
linux1[79] %
Last Modified -