Compare commits
No commits in common. "ad3dfdc83b0c63ef851cc85f2f4b1ad3dac5364f" and "a5f7685dcb3fe7efffde76073bbbc253dde3adcb" have entirely different histories.
ad3dfdc83b
...
a5f7685dcb
Binary file not shown.
|
@ -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 << values.at(i) << "\n";
|
||||
std::cout << std::format("{} ", values.at(i));
|
||||
}
|
||||
|
||||
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 << values2[i] << "\n";
|
||||
std::cout << std::format("{} ", values2.at(i));
|
||||
}
|
||||
|
||||
std::cout << "\n";
|
||||
|
|
|
@ -8,14 +8,10 @@ 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";
|
||||
|
@ -42,6 +38,8 @@ 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) {
|
||||
|
|
|
@ -8,15 +8,11 @@ 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";
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
// fig06_06.cpp
|
||||
// Printing a student grade distribution as a primitive bar chart.
|
||||
|
||||
#include <format>
|
||||
#include <iostream>
|
||||
#include <array>
|
||||
|
||||
|
|
|
@ -11,25 +11,19 @@ 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) {
|
||||
randomReal(engine);
|
||||
++frequency.at(randomDie(engine));
|
||||
}
|
||||
|
||||
std::cout << "Face " << " Frequency \n";
|
||||
|
||||
// output each array element's value
|
||||
for (size_t face{1}; face < histogram.size(); ++face) {
|
||||
std::cout << face << " " << histogram.at(face) << "\n";
|
||||
for (size_t face{1}; face < frequency.size(); ++face) {
|
||||
std::cout << face << " " << frequency.at(face) << "\n";
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue