// Example 10.5. Tally numbers between RANGE_LOW and RANGE_HIGH #include #include const int RANGE_LOW = 80; const int RANGE_HIGH = 89; const int SIZE = (RANGE_HIGH - RANGE_LOW + 1); int main(void) { void frequency(const int ar[], int NumEls); int ar[] = {88, 83, 88, 79, 85, 94, 88, 82, 87, 76, 87, 83, 89, 90, 80, 86, 86, 89, 87, 88}, NumEls = 20, // number of elements in ar tally[SIZE]; // array to tally ar elements from // RANGE_LOW to RANGE_HIGH frequency(ar, NumEls); return 0; } // // Function to print the frequency of values between RANGE_LOW // and RANGE_HIGH in array ar from ar[0] to ar[NumEls - 1] // Pre: ar is a constant array of integers. // NumEls is the number of elements in ar. // Post: The frequency of values from ar between // RANGE_LOW and RANGE_HIGH was displayed. // void frequency(const int ar[], int NumEls) { int tally[SIZE], // one bin for each number in the range bin; // index for tally // initialize array tally to zero for (bin = 0; bin < SIZE; bin++) tally[bin] = 0; // tally elements of array ar for (int i = 0; i < NumEls; i++) if (RANGE_LOW <= ar[i] && ar[i] <= RANGE_HIGH) tally[ar[i] - RANGE_LOW]++; // print tally results cout << "\n Frequency Distribution\n\n"; cout << "\tValue\tFrequency\n"; cout << "\t-----\t---------\n"; for (bin = 0; bin < SIZE; bin++) cout << "\t" << setw(3) << bin + RANGE_LOW << "\t" << setw(3) << tally[bin] << endl; }