//***************************************************************** // R. A. Hillyard // histogram2.cpp // March 2001 // // See histogram1.cpp for comments // // This version uses a function to initialize the array //***************************************************************** #include <iostream> using namespace std; void initialize(int anyArray[], int size); //pre conditions: anyArray has been declared and size is the size of the array //post condition :all array elements will be set to zero const int NUM_RANGES = 11; int main() { int score; //variable for user input int range[NUM_RANGES]; //range[k] holds the number of scores seen //so far between 10*k and 10*k + 9 //initialize the counters initialize(range, NUM_RANGES); cout << "Enter scores --- (-1 to quit)" << endl; do { int score; cout << "> "; cin >> score; if (score == -1) break; range[score / 10]++; } while (true); cout << endl << "Histogram of scores:" << endl; for (int k = 0; k < NUM_RANGES-1; k++) cout << 10*k << " to " << 10*k + 9 << " : " << range[k] << endl; cout << "100 : " << range[10] << endl; return 0; } //***************************************************************** // initialize - expects a referance to an array of ints and // the size of the array - sets all elements of the array to 0 //***************************************************************** void initialize(int a[], int size) { for(int k = 0; k <size; k++) a[k] = 0; // initialize the counters }//end initialize //**********************Program Output***************************** Enter scores --- (-1 to quit) > 87 > 55 > 44 > 33 > 98 > 12 > 7 > 34 > 33 > 66 > 77 > 88 > 99 > 100 > 100 > 0 > 34 > 94 > 87 > -1 Histogram of scores: 0 to 9 : 2 10 to 19 : 1 20 to 29 : 0 30 to 39 : 4 40 to 49 : 1 50 to 59 : 1 60 to 69 : 1 70 to 79 : 1 80 to 89 : 3 90 to 99 : 3 100 : 2