Compare commits

..

2 Commits

Author SHA1 Message Date
davoudn ad3dfdc83b 06_07 was modifiesd 2024-04-16 11:38:36 +03:30
davoudn 23ff98814f 06_03 was modifiesd 2024-04-16 09:59:20 +03:30
6 changed files with 24 additions and 12 deletions

Binary file not shown.

View File

@ -1,6 +1,6 @@
// fig06_02.cpp
// Initializing an array in a declaration.
#include <format>
//#include <format>
#include <iostream>
#include <array>
@ -9,7 +9,7 @@ int main() {
// output each array element's value
for (size_t i{0}; i < values.size(); ++i) {
std::cout << std::format("{} ", values.at(i));
std::cout << values.at(i) << "\n";
}
std::cout << "\n\n";
@ -19,7 +19,7 @@ int main() {
// output each array element's value
for (size_t i{0}; i < values2.size(); ++i) {
std::cout << std::format("{} ", values2.at(i));
std::cout << values2[i] << "\n";
}
std::cout << "\n";

View File

@ -8,10 +8,14 @@ int main() {
std::array items2{1, 2, 3, 4, 5};
// display items before modification
for (int i{0}; i<items.size();i++)
std::cout << items[i] << "<-- items\n";
std::cout << "items before modification: ";
for (const int& item : items) { // item is a reference to a const int
std::cout << item << "<-- items\n";
}
}
std::cout << "items2 before modification:\n";
for (const int& item : items) { // item is a reference to a const int
std::cout << item << "<-- items2\n";
@ -38,8 +42,6 @@ int main() {
for (const int& item : items2) {
std::cout << item << "<-- items2\n";
}
// sum elements of items using range-based for with initialization
std::cout << "\n\ncalculating a running total of items' values:\n";
for (int runningTotal{0}; const int& item : items) {

View File

@ -8,11 +8,15 @@ int main() {
constexpr size_t arraySize{5}; // must initialize in declaration
std::array<int, arraySize> values{}; // array values has 5 elements
/*
for (size_t i{0}; i < values.size(); ++i) { // set the values
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
for (const int& value : values) {
std::cout << value << "\n";

View File

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

View File

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