06_07 was modifiesd

This commit is contained in:
davoudn 2024-04-16 11:38:36 +03:30
parent 23ff98814f
commit ad3dfdc83b
4 changed files with 16 additions and 6 deletions

View File

@ -8,11 +8,15 @@ int main() {
constexpr size_t arraySize{5}; // must initialize in declaration constexpr size_t arraySize{5}; // must initialize in declaration
std::array<int, arraySize> values{}; // array values has 5 elements std::array<int, arraySize> values{}; // array values has 5 elements
/*
for (size_t i{0}; i < values.size(); ++i) { // set the values for (size_t i{0}; i < values.size(); ++i) { // set the values
values.at(i) = 2 + 2 * i; values.at(i) = 2 + 2 * i;
} }
*/
for ( int index{0}; int& value : values) {
value = 2 + 2 * index;
index++;
}
// output contents of array values in tabular format // output contents of array values in tabular format
for (const int& value : values) { for (const int& value : values) {
std::cout << value << "\n"; std::cout << value << "\n";

View File

@ -1,6 +1,6 @@
// fig06_06.cpp // fig06_06.cpp
// Printing a student grade distribution as a primitive bar chart. // Printing a student grade distribution as a primitive bar chart.
#include <format>
#include <iostream> #include <iostream>
#include <array> #include <array>

View File

@ -11,19 +11,25 @@ int main() {
std::default_random_engine engine{rd()}; // rd() produces a seed std::default_random_engine engine{rd()}; // rd() produces a seed
std::uniform_int_distribution randomDie{0, 6}; std::uniform_int_distribution randomDie{0, 6};
std::uniform_real_distribution<double> randomReal{0, 1};
constexpr size_t arraySize{7}; // ignore element zero constexpr size_t arraySize{7}; // ignore element zero
std::array<int, arraySize> frequency{}; // initialize to 0s std::array<int, arraySize> frequency{}; // initialize to 0s
constexpr size_t N{7};
std::array<double, N> histogram{};
// roll die 60,000,000 times; use die value as frequency index // roll die 60,000,000 times; use die value as frequency index
for (int roll{1}; roll <= 60'000'000; ++roll) { for (int roll{1}; roll <= 60'000'000; ++roll) {
++frequency.at(randomDie(engine)); randomReal(engine);
} }
std::cout << "Face " << " Frequency \n"; std::cout << "Face " << " Frequency \n";
// output each array element's value // output each array element's value
for (size_t face{1}; face < frequency.size(); ++face) { for (size_t face{1}; face < histogram.size(); ++face) {
std::cout << face << " " << frequency.at(face) << "\n"; std::cout << face << " " << histogram.at(face) << "\n";
} }
} }