// File: curve.cxx // Program to read scores in a class and print // how many students fall in each ten-point category #include #include const int MANY_CATS = 11; const int CATEGORY_SIZE = 10; const char GRADE_FILE[] = "grades.txt"; //---------------------------------------------------- // Prototypes void read_scores(int cat[]); // This function sets all categories to zero, and then // reads the grade file; each time it reads a score, // it figures out which category that score is in // and adds one to the category. void print_results(int cat[]); // Print a table of how many students are in each // category. //---------------------------------------------------- //---------------------------------------------------- int main( ) { int categories[MANY_CATS]; read_scores(categories); print_results(categories); return 0; } //---------------------------------------------------- //---------------------------------------------------- void read_scores(int cat[]) { int i; // Loop control variable ifstream grade_in; // The grade file int next; // A grade from the grade file int category; // The category for the grade for (i = 0; i < MANY_CATS; i++) { cat[i] = 0; } grade_in.open(GRADE_FILE); while (grade_in && grade_in.peek( ) != EOF) { grade_in >> next; grade_in.ignore( ); category = next / CATEGORY_SIZE; if ((0 <= category) && (category < MANY_CATS)) { cat[category]++; } else { cerr << "Illegal input: " << next << endl; } } } //---------------------------------------------------- void print_results(int cat[]) { int i; for (i = 0; i < MANY_CATS; i++) { if (cat[i] > 0) { cout << "Number of scores in range of " << i*CATEGORY_SIZE << " to " << i*CATEGORY_SIZE + CATEGORY_SIZE - 1 << ": " << cat[i] << endl; } } }