I am new here and to programming in general so I hope I'm posting this correctly. I have a homework assignment with several parts to it, and I've got most of the program working, but I'm stuck. Was wondering if someone could lead me in the right direction. I'll post the question below and the code I've come up with so far. Thanks in advance for your help!!
Exercise:
Write a C program that reads a list of double-precision grades from the keyboard into an array named grades. The grades are to be counted as they are read, and entry is to be terminated when a negative value has been entered. Once all of the grades have been input, your program should find and display the sum and average of the grades. The grades should then be listed with an asterisk (*) in front of each grade that is below the average.
The teacher has told us to set the size of the array to something specific in the beginning(100 etc.), but to keep track of how many grades have been entered. I've been using 5 for now for testing purposes. I've gotten through most of the exercise, accepting grades, determining average and displaying total, averages and grades with/without the asterisk, but can't for the life of me figure out how to make the sentinel value work. My compiler keep crashing. I've tried a while statement before the first for loop and am just at a loss for what else to try at this point.
#include <stdio.h>
#include <stdlib.h>
int main()
{
#define SENTINEL -1
#define NUMGRADES 5
float grades[NUMGRADES];
float total =0;
float gradecount =0;
float average;
int i;
for (i=0; i< NUMGRADES; i++)
{
printf ("Enter a grade: ");
scanf ("%f", &grades[i]);
total += grades[i];
gradecount ++;
}
average = total/gradecount;
printf("The sum of all the grades is: %.2f\n", total);
printf("The average grade is: %.2f\n", average);
printf("The grades are as follows: \n");
for (i=0; i< NUMGRADES; i++)
if (grades[i] < average)
printf("* %5.2f\n", grades[i]);
else
printf(" %6.2f\n", grades[i]);
system("PAUSE");
return 0;
}

New Topic/Question
Reply
MultiQuote








|