7.4.5. Using Bar Charts to Display array Data Graphically

Many programs present data to users in a graphical manner. For example, numeric values are often displayed as bars in a bar chart. In such a chart, longer bars represent proportionally larger numeric values. One simple way to display numeric data graphically is with a bar chart that shows each numeric value as a bar of asterisks (*).

Professors often like to examine grade distributions on an exam. A professor might graph the number of grades in each of several categories to visualize the grade distribution. Suppose the grades were 87, 68, 94, 100, 83, 78, 85, 91, 76 and 87. There was one grade of 100, two grades in the 90s, four grades in the 80s, two grades in the 70s, one grade in the 60s and no grades below 60. Our next program (Fig. 7.9) stores this data in an array of 11 elements, each corresponding to a grade category. For example, n[0] indicates the number of grades in the range 0–9, n[7] indicates the number of grades in the range 70–79 and n[10] indicates the number of grades of 100. The GradeBook versions in Figs. 7.157.16 and Figs. 7.227.23 contain code that calculates these grade frequencies based on a set of grades. For now, we manually create the array by looking at the set of grades.


 1   // Fig. 7.9: fig07_09.cpp
 2   // Bar chart printing program.
 3   #include <iostream>
 4   #include <iomanip>
 5   #include <array>
 6   using namespace std;
 7
 8   int main()
 9   {
10      const size_t arraySize = 11;
11      array< unsigned int, arraySize > n =
12         { 0, 0, 0, 0, 0, 0, 1, 2, 4, 2, 1 };
13
14      cout << "Grade distribution:" << endl;
15
16      // for each element of array n, output a bar of the chart
17      for ( size_t i = 0; i < n.size(); ++i )
18      {
19         // output bar labels ("0-9:", ..., "90-99:", "100:" )
20         if ( 0 == i )
21            cout << "  0-9: ";
22         else if ( 10 == i )
23            cout << "  100: ";
24         else
25            cout << i * 10 << "-" << ( i * 10 ) + 9 << ": ";
26
27         // print bar of asterisks
28         for ( unsigned int stars = 0; stars < n[ i ]; ++stars )
29            cout << '*';
30
31         cout << endl; // start a new line of output
32      } // end outer for
33   } // end main


Grade distribution:
  0-9:
10-19:
20-29:
30-39:
40-49:
50-59:
60-69: *
70-79: **
80-89: ****
90-99: **
  100: *


Fig. 7.9. Bar chart printing program.

The program reads the numbers from the array and graphs the information as a bar chart, displaying each grade range followed by a bar of asterisks indicating the number of grades in that range. To label each bar, lines 20–25 output a grade range (e.g., "70-79: ") based on the current value of counter variable i. The nested for statement (lines 28–29) outputs the bars. Note the loop-continuation condition in line 28 (stars < n[i]). Each time the program reaches the inner for, the loop counts from 0 up to n[i], thus using a value in array n to determine the number of asterisks to display. In this example, n[0]n[5] contain zeros because no students received a grade below 60. Thus, the program displays no asterisks next to the first six grade ranges.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.133.158.230