fork download
  1. // Timothy Price CS1A Ch5 Challenge 17
  2. /*******************************************************************************
  3.  * Create chart
  4.  *
  5.  * This program receives values for a number of stores and creates a
  6.  * displayable bar graph with the received values.
  7.  *
  8.  * Inputs:
  9.  * Daily sales for each store
  10.  *
  11.  * Output:
  12.  * Bar graph
  13.  ******************************************************************************/
  14.  
  15. #include <iostream>
  16. #include <cmath>
  17.  
  18. using namespace std;
  19.  
  20. int main() {
  21.  
  22. // Data dictionary:
  23. const int storenumber = 5;
  24. int sales[storenumber]; // [ ] arrays used to avoid congestion
  25.  
  26. // Receive the sales for each store:
  27. for (int base = 0; base < storenumber; base++) {
  28. cout << "Enter today's sales for store " << (base + 1) << ": ";
  29. cin >> sales[base];
  30. cout << "$" << sales[base] << endl;
  31. }
  32.  
  33. // Chart header:
  34. cout << "__________________________________" << endl;
  35. cout << "SALES BAR CHART" << endl << "(Each * = $100)" << endl;
  36.  
  37. // Create the bar graph for each store:
  38. for (int base1 = 0; base1 < storenumber; base1++) {
  39. cout << "Store " << (base1 + 1) << ": ";
  40.  
  41. // Round numbers down to the nearest $100 interval:
  42. int numasterisks = sales[base1] / 100;
  43.  
  44. for (int base2 = 0; base2 < numasterisks; base2++) {
  45. cout << "*";
  46. }
  47. cout << endl;
  48. }
  49.  
  50. return 0;
  51. }
Success #stdin #stdout 0.01s 5316KB
stdin
123
234
345
456
567
stdout
Enter today's sales for store 1: $123
Enter today's sales for store 2: $234
Enter today's sales for store 3: $345
Enter today's sales for store 4: $456
Enter today's sales for store 5: $567
__________________________________
SALES BAR CHART
(Each * = $100)
Store 1: *
Store 2: **
Store 3: ***
Store 4: ****
Store 5: *****