c-resources/CPlusPlus20ForProgrammers-m.../examples/ch06/fig06_06.cpp

49 lines
1.8 KiB
C++
Raw Normal View History

2024-04-09 06:45:18 +00:00
// fig06_06.cpp
// Printing a student grade distribution as a primitive bar chart.
2024-04-16 08:08:36 +00:00
2024-04-09 06:45:18 +00:00
#include <iostream>
#include <array>
int main() {
constexpr std::array frequencies{0, 0, 0, 0, 0, 0, 1, 2, 4, 2, 1};
std::cout << "Grade distribution:\n";
// for each element of frequencies, output a bar of the chart
for (int i{0}; const int& frequency : frequencies) {
// output bar labels ("00-09:", ..., "90-99:", "100:")
if (i < 10) {
2024-04-16 06:03:08 +00:00
std::cout << i * 100 << " " << (i * 100) + 9 << "\n";
2024-04-09 06:45:18 +00:00
}
else {
2024-04-16 06:03:08 +00:00
std::cout << 100 << "\n";
2024-04-09 06:45:18 +00:00
}
++i;
// print bar of asterisks
for (int stars{0}; stars < frequency; ++stars) {
std::cout << '*';
}
std::cout << '\n'; // start a new line of output
}
}
/**************************************************************************
* (C) Copyright 1992-2022 by Deitel & Associates, Inc. and *
* Pearson Education, Inc. All Rights Reserved. *
* *
* DISCLAIMER: The authors and publisher of this book have used their *
* best efforts in preparing the book. These efforts include the *
* development, research, and testing of the theories and programs *
* to determine their effectiveness. The authors and publisher make *
* no warranty of any kind, expressed or implied, with regard to these *
* programs or to the documentation contained in these books. The authors *
* and publisher shall not be liable in any event for incidental or *
* consequential damages in connection with, or arising out of, the *
* furnishing, performance, or use of these programs. *
**************************************************************************/