Compare commits

..

No commits in common. "a5f7685dcb3fe7efffde76073bbbc253dde3adcb" and "89880f6615d9899a6ecf6b81983bde9f4b93deb2" have entirely different histories.

9 changed files with 30 additions and 50 deletions

Binary file not shown.

View File

@ -1,50 +1,35 @@
// 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 << item << "<-- items\n"; std::cout << std::format("{} ", item);
} }
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;
} }
for (int item : items2) { // item is a reference to an int
item *= 2;
}
// display items after modification // display items after modification
std::cout << "\nitems after modification: \n "; std::cout << "\nitems after modification: ";
for (const int& item : items) { for (const int& item : items) {
std::cout << item << "<-- items\n"; std::cout << std::format("{} ", item);
} }
// 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 << " running total "<< item << runningTotal<<"\n"; std::cout << std::format("item: {}; running total: {}\n",
item, runningTotal);
} }
} }

View File

@ -1,5 +1,6 @@
// 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>
@ -15,7 +16,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 << value << "\n"; std::cout << fmt::format("{} ", value);
} }
std::cout << '\n'; std::cout << '\n';

View File

@ -13,7 +13,7 @@ int main() {
total += item; total += item;
} }
std::cout << "Total of array elements: " << total); std::cout << fmt::format("Total of array elements: {}\n", total);
} }

View File

@ -13,10 +13,11 @@ 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 << i * 100 << " " << (i * 100) + 9 << "\n"; std::cout << std::format("{:03d}-{:03d}: ",
i * 100, (i * 100) + 9);
} }
else { else {
std::cout << 100 << "\n"; std::cout << std::format("{:>7d}: ", 100);
} }
++i; ++i;

View File

@ -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{0, 6}; std::uniform_int_distribution randomDie{1, 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 << "Face " << " Frequency \n"; std::cout << std::format("{}{:>13}\n", "Face", "Frequency");
// 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 << face << " " << frequency.at(face) << "\n"; std::cout << std::format("{:>4}{:>13}\n", face, frequency.at(face));
} }
} }

View File

@ -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,11 +19,12 @@ int main() {
++frequency[response];//.at(response); ++frequency[response];//.at(response);
} }
std::cout << "Rating Frequency \n"; std::cout << std::format("{}{:>12}\n", "Rating", "Frequency");
// 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 << rating << " " << frequency.at(rating) << "\n"; std::cout << std::format("{:>6}{:>12}\n",
rating, frequency.at(rating));
} }
} }

View File

@ -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 << "Total of array elements "<< std::accumulate(std::begin(integers), std::end(integers), 0) <<"\n"; std::cout << fmt::format("Total of array elements: {}\n",
std::cout << "Total of array elements "<< std::accumulate(integers.cbegin(), integers.cend(), 0) <<"\n"; std::accumulate(std::begin(integers), std::end(integers), 0));
std::cout << "1 to 2 of array elements "<< std::accumulate(&integers[1], &integers[3], 0) <<"\n";
} }
/************************************************************************** /**************************************************************************

View File

@ -1,32 +1,24 @@
// 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>
template <typename T> int multiply(int x, int y) {
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 << "Product of integers: " << std::accumulate( std::begin(integers), std::end(integers), 1, multiply<int>) << "\n"; std::cout << std::format("Product of integers: {}\n", std::accumulate(
std::begin(integers), std::end(integers), 1, multiply));
std::cout << "Product of integers with a lambda: " << std::accumulate(std::begin(integers), std::end(integers), 1, multiply_lambda) << "\n"; std::cout << std::format("Product of integers with a lambda: {}\n",
std::accumulate(std::begin(integers), std::end(integers), 1,
std::cout << "Product of integers with a lambda: " << std::accumulate(std::begin(floats), std::end(floats), 1, multiply_int) << "\n"; multiply_lambda));
std::cout << "Product of integers with a lambda: " << std::accumulate(std::begin(floats), std::end(floats), 1, multiply_lambda) << "\n";
} }