ch06 samples codes were modified
This commit is contained in:
parent
89880f6615
commit
1196fe88f9
Binary file not shown.
|
@ -1,35 +1,50 @@
|
||||||
// fig06_03.cpp
|
// fig06_03.cpp
|
||||||
// Using range-based for.
|
// Using range-based for.
|
||||||
#include <format>
|
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <array>
|
#include <array>
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
std::array items{1, 2, 3, 4, 5}; // type inferred as array<int, 5>
|
std::array items{1, 2, 3, 4, 5}; // type inferred as array<int, 5>
|
||||||
|
|
||||||
|
std::array items2{1, 2, 3, 4, 5};
|
||||||
// display items before modification
|
// display items before modification
|
||||||
std::cout << "items before modification: ";
|
std::cout << "items before modification: ";
|
||||||
for (const int& item : items) { // item is a reference to a const int
|
for (const int& item : items) { // item is a reference to a const int
|
||||||
std::cout << std::format("{} ", item);
|
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";
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
// multiply the elements of items by 2
|
// multiply the elements of items by 2
|
||||||
for (int& item : items) { // item is a reference to an int
|
for (int& item : items) { // item is a reference to an int
|
||||||
item *= 2;
|
item *= 2;
|
||||||
}
|
}
|
||||||
|
|
||||||
// display items after modification
|
for (int item : items2) { // item is a reference to an int
|
||||||
std::cout << "\nitems after modification: ";
|
item *= 2;
|
||||||
for (const int& item : items) {
|
|
||||||
std::cout << std::format("{} ", item);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// display items after modification
|
||||||
|
std::cout << "\nitems after modification: \n ";
|
||||||
|
for (const int& item : items) {
|
||||||
|
std::cout << item << "<-- items\n";
|
||||||
|
}
|
||||||
|
|
||||||
|
// display items after modification
|
||||||
|
std::cout << "\nitems2 after modification:\n ";
|
||||||
|
for (const int& item : items2) {
|
||||||
|
std::cout << item << "<-- items2\n";
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
// sum elements of items using range-based for with initialization
|
// sum elements of items using range-based for with initialization
|
||||||
std::cout << "\n\ncalculating a running total of items' values:\n";
|
std::cout << "\n\ncalculating a running total of items' values:\n";
|
||||||
for (int runningTotal{0}; const int& item : items) {
|
for (int runningTotal{0}; const int& item : items) {
|
||||||
runningTotal += item;
|
runningTotal += item;
|
||||||
std::cout << std::format("item: {}; running total: {}\n",
|
std::cout << " running total "<< item << runningTotal<<"\n";
|
||||||
item, runningTotal);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,6 +1,5 @@
|
||||||
// fig06_04.cpp
|
// fig06_04.cpp
|
||||||
// Set array values to the even integers from 2 to 10.
|
// Set array values to the even integers from 2 to 10.
|
||||||
#include <fmt/format.h>
|
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <array>
|
#include <array>
|
||||||
|
|
||||||
|
@ -16,7 +15,7 @@ int main() {
|
||||||
|
|
||||||
// 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 << fmt::format("{} ", value);
|
std::cout << value << "\n";
|
||||||
}
|
}
|
||||||
|
|
||||||
std::cout << '\n';
|
std::cout << '\n';
|
||||||
|
|
|
@ -13,7 +13,7 @@ int main() {
|
||||||
total += item;
|
total += item;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::cout << fmt::format("Total of array elements: {}\n", total);
|
std::cout << "Total of array elements: " << total);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -13,11 +13,10 @@ int main() {
|
||||||
for (int i{0}; const int& frequency : frequencies) {
|
for (int i{0}; const int& frequency : frequencies) {
|
||||||
// output bar labels ("00-09:", ..., "90-99:", "100:")
|
// output bar labels ("00-09:", ..., "90-99:", "100:")
|
||||||
if (i < 10) {
|
if (i < 10) {
|
||||||
std::cout << std::format("{:03d}-{:03d}: ",
|
std::cout << i * 100 << " " << (i * 100) + 9 << "\n";
|
||||||
i * 100, (i * 100) + 9);
|
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
std::cout << std::format("{:>7d}: ", 100);
|
std::cout << 100 << "\n";
|
||||||
}
|
}
|
||||||
|
|
||||||
++i;
|
++i;
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
// fig06_07.cpp
|
// fig06_07.cpp
|
||||||
// Die-rolling program using an array instead of switch.
|
// Die-rolling program using an array instead of switch.
|
||||||
#include <format> // C++20: This will be #include <format>
|
//#include <format> // C++20: This will be #include <format>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <array>
|
#include <array>
|
||||||
#include <random>
|
#include <random>
|
||||||
|
@ -9,7 +9,7 @@ int main() {
|
||||||
// set up random-number generation
|
// set up random-number generation
|
||||||
std::random_device rd; // used to seed the default_random_engine
|
std::random_device rd; // used to seed the default_random_engine
|
||||||
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{1, 6};
|
std::uniform_int_distribution randomDie{0, 6};
|
||||||
|
|
||||||
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
|
||||||
|
@ -19,11 +19,11 @@ int main() {
|
||||||
++frequency.at(randomDie(engine));
|
++frequency.at(randomDie(engine));
|
||||||
}
|
}
|
||||||
|
|
||||||
std::cout << std::format("{}{:>13}\n", "Face", "Frequency");
|
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 < frequency.size(); ++face) {
|
||||||
std::cout << std::format("{:>4}{:>13}\n", face, frequency.at(face));
|
std::cout << face << " " << frequency.at(face) << "\n";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
// fig06_08.cpp
|
// fig06_08.cpp
|
||||||
// Poll analysis program.
|
// Poll analysis program.
|
||||||
#include <format> // C++20: This will be #include <format>
|
//#include <format> // C++20: This will be #include <format>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <array>
|
#include <array>
|
||||||
|
|
||||||
|
@ -19,12 +19,11 @@ int main() {
|
||||||
++frequency[response];//.at(response);
|
++frequency[response];//.at(response);
|
||||||
}
|
}
|
||||||
|
|
||||||
std::cout << std::format("{}{:>12}\n", "Rating", "Frequency");
|
std::cout << "Rating Frequency \n";
|
||||||
|
|
||||||
// output each array element's value
|
// output each array element's value
|
||||||
for (size_t rating{1}; rating < frequency.size(); ++rating) {
|
for (size_t rating{1}; rating < frequency.size(); ++rating) {
|
||||||
std::cout << std::format("{:>6}{:>12}\n",
|
std::cout << rating << " " << frequency.at(rating) << "\n";
|
||||||
rating, frequency.at(rating));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,14 +1,14 @@
|
||||||
// fig06_11.cpp
|
// fig06_11.cpp
|
||||||
// Compute the sum of the elements of an array using accumulate.
|
// Compute the sum of the elements of an array using accumulate.
|
||||||
#include <array>
|
#include <array>
|
||||||
#include <fmt/format.h>
|
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <numeric>
|
#include <numeric>
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
constexpr std::array integers{10, 20, 30, 40};
|
constexpr std::array integers{10, 20, 30, 40};
|
||||||
std::cout << fmt::format("Total of array elements: {}\n",
|
std::cout << "Total of array elements "<< std::accumulate(std::begin(integers), std::end(integers), 0) <<"\n";
|
||||||
std::accumulate(std::begin(integers), std::end(integers), 0));
|
std::cout << "Total of array elements "<< std::accumulate(integers.cbegin(), integers.cend(), 0) <<"\n";
|
||||||
|
std::cout << "1 to 2 of array elements "<< std::accumulate(&integers[1], &integers[3], 0) <<"\n";
|
||||||
}
|
}
|
||||||
|
|
||||||
/**************************************************************************
|
/**************************************************************************
|
||||||
|
|
|
@ -1,24 +1,32 @@
|
||||||
// fig06_12.cpp
|
// fig06_12.cpp
|
||||||
// Compute the product of an array's elements using accumulate.
|
// Compute the product of an array's elements using accumulate.
|
||||||
#include <array>
|
#include <array>
|
||||||
#include <format>
|
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <numeric>
|
#include <numeric>
|
||||||
|
|
||||||
int multiply(int x, int y) {
|
template <typename T>
|
||||||
|
T multiply(T x, T y) {
|
||||||
return x * y;
|
return x * y;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
int multiply_int(int x, int y) {
|
||||||
|
return x * y;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
constexpr std::array integers{1, 2, 3, 4, 5};
|
constexpr std::array integers{1, 2, 3, 4, 5};
|
||||||
|
constexpr std::array floats {1.0, 2.0, 3.0, 4.0, 5.0};
|
||||||
auto multiply_lambda = [](const auto& x, const auto& y) {return x * y; };
|
auto multiply_lambda = [](const auto& x, const auto& y) {return x * y; };
|
||||||
|
|
||||||
std::cout << std::format("Product of integers: {}\n", std::accumulate(
|
std::cout << "Product of integers: " << std::accumulate( std::begin(integers), std::end(integers), 1, multiply<int>) << "\n";
|
||||||
std::begin(integers), std::end(integers), 1, multiply));
|
|
||||||
|
|
||||||
std::cout << std::format("Product of integers with a lambda: {}\n",
|
std::cout << "Product of integers with a lambda: " << std::accumulate(std::begin(integers), std::end(integers), 1, multiply_lambda) << "\n";
|
||||||
std::accumulate(std::begin(integers), std::end(integers), 1,
|
|
||||||
multiply_lambda));
|
std::cout << "Product of integers with a lambda: " << std::accumulate(std::begin(floats), std::end(floats), 1, multiply_int) << "\n";
|
||||||
|
|
||||||
|
std::cout << "Product of integers with a lambda: " << std::accumulate(std::begin(floats), std::end(floats), 1, multiply_lambda) << "\n";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue