Notice that this histogram states that two scores are between 90 and 100, 4 scores are between 80 and 89, 1 score is between 50 and 59, 1 score is between 40 and 49, 2 scores are between 30 and 39, and 1 score is between 10 and 19.
Be sure that when planning your solution, you make use of an array and multiple functions.
Just like the other exercises, it is important to proceed with the following steps: program specification, the algorithm and basic design of the program, and then the implementation of the C code. When you have finished the coding portion, be sure to test your program on some sample input.
Problem Specification:
The Algorithm:
The C Program:
/* prototypes */
void read_values (int grade[], int grade_size);
void print_histogram (int grade[], int grade_size);
void print_X (int num_X);
int main (void) {
/* declarations */
int grade_hist[10] = {0,0,0,0,0,0,0,0,0,0};
/* zero out the histogram which keeps
counts of grades in each range of
ten, grade[0] represents
the number of grades between 0 and 9,
grade[1] represents
the number of grades between 10 and 19, etc.
*/
read_values (grade_hist, 10);
print_histogram (grade_hist, 10);
return 0;
}
/* read_values */
/* parameters: int grade[], int grade_size
*/
/* notice that grade[] will be passed by
reference, so any updating done
by this function will remain
when the function returns */
/* return: void (we will update the array
grade[] */
/* functionality: reads grades entered by
user and increments the appropriate
10's location in the array grade[]
*/
void read_values (int grade[], int grade_size) {
/* declarations */
int current_grade;
/* current grade entered by user */
/* prompt user to enter grade */
printf("Please enter a grade: ");
scanf("%d", ¤t_grade);
while (current_grade != -1)
{
/* note here that -1 is
a sentinel value */
/* increment appropriate
bin in grade[] */
current_grade = current_grade / 10;
/* gives bin number */
if (current_grade
== 10) {
/* we must
put this in location 9 of array instead of location 10 since
location
10 is off the end of the array */
grade[9]++;
/* increment the count at location 9 */
}
else {
grade[current_grade]++; /*
increment the count for the current tens */
}
/* read new grade */
printf("Please enter a
grade: ");
scanf("%d", ¤t_grade);
}
}
/* print_histogram */
/* parameters: int grade[], int grade_size
*/
/* return: void */
/* functionality: this function prints the
histogram in the format of high
scores to low scores */
void print_histogram (int grade[], int grade_size) {
/* declarations */
int i;
/* looping variable */
/* top bin is different from the rest
since it goes from 90 to 100, so
we will print this
outside the loop */
printf("90 - 100: ");
print_X(grade[9]);
/* print appropriate number of X's */
printf("\n");
for (i = 8; i >= 0; i--) {
/* go in reverse order of grade array indices */
printf("%d0 - %d9: ",
i, i);
print_X(grade[i]);
printf("\n");
}
}
/* print_X */
/* parameters: int num_X */
/* return: void */
/* functionality: prints num_X number of
capital X's on a single line
not followed by a newline */
void print_X (int num_X) {
/* declarations */
int i;
/* looping variable */
for (i = 0; i < num_X; i++){
printf("X");
}
}