Compare commits
No commits in common. "a5f7685dcb3fe7efffde76073bbbc253dde3adcb" and "89880f6615d9899a6ecf6b81983bde9f4b93deb2" have entirely different histories.
a5f7685dcb
...
89880f6615
Binary file not shown.
|
@ -1,50 +1,35 @@
|
|||
// fig06_03.cpp
|
||||
// Using range-based for.
|
||||
#include <format>
|
||||
#include <iostream>
|
||||
#include <array>
|
||||
|
||||
int main() {
|
||||
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
|
||||
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 << 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
|
||||
for (int& item : items) { // item is a reference to an int
|
||||
item *= 2;
|
||||
}
|
||||
|
||||
for (int item : items2) { // item is a reference to an int
|
||||
item *= 2;
|
||||
}
|
||||
|
||||
// display items after modification
|
||||
std::cout << "\nitems after modification: \n ";
|
||||
std::cout << "\nitems after modification: ";
|
||||
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
|
||||
std::cout << "\n\ncalculating a running total of items' values:\n";
|
||||
for (int runningTotal{0}; const int& item : items) {
|
||||
runningTotal += item;
|
||||
std::cout << " running total "<< item << runningTotal<<"\n";
|
||||
std::cout << std::format("item: {}; running total: {}\n",
|
||||
item, runningTotal);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
// fig06_04.cpp
|
||||
// Set array values to the even integers from 2 to 10.
|
||||
#include <fmt/format.h>
|
||||
#include <iostream>
|
||||
#include <array>
|
||||
|
||||
|
@ -15,7 +16,7 @@ int main() {
|
|||
|
||||
// output contents of array values in tabular format
|
||||
for (const int& value : values) {
|
||||
std::cout << value << "\n";
|
||||
std::cout << fmt::format("{} ", value);
|
||||
}
|
||||
|
||||
std::cout << '\n';
|
||||
|
|
|
@ -13,7 +13,7 @@ int main() {
|
|||
total += item;
|
||||
}
|
||||
|
||||
std::cout << "Total of array elements: " << total);
|
||||
std::cout << fmt::format("Total of array elements: {}\n", total);
|
||||
}
|
||||
|
||||
|
||||
|
|
|
@ -13,10 +13,11 @@ int main() {
|
|||
for (int i{0}; const int& frequency : frequencies) {
|
||||
// output bar labels ("00-09:", ..., "90-99:", "100:")
|
||||
if (i < 10) {
|
||||
std::cout << i * 100 << " " << (i * 100) + 9 << "\n";
|
||||
std::cout << std::format("{:03d}-{:03d}: ",
|
||||
i * 100, (i * 100) + 9);
|
||||
}
|
||||
else {
|
||||
std::cout << 100 << "\n";
|
||||
std::cout << std::format("{:>7d}: ", 100);
|
||||
}
|
||||
|
||||
++i;
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
// fig06_07.cpp
|
||||
// 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 <array>
|
||||
#include <random>
|
||||
|
@ -9,7 +9,7 @@ int main() {
|
|||
// set up random-number generation
|
||||
std::random_device rd; // used to seed the default_random_engine
|
||||
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
|
||||
std::array<int, arraySize> frequency{}; // initialize to 0s
|
||||
|
@ -19,11 +19,11 @@ int main() {
|
|||
++frequency.at(randomDie(engine));
|
||||
}
|
||||
|
||||
std::cout << "Face " << " Frequency \n";
|
||||
std::cout << std::format("{}{:>13}\n", "Face", "Frequency");
|
||||
|
||||
// output each array element's value
|
||||
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));
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
// fig06_08.cpp
|
||||
// Poll analysis program.
|
||||
//#include <format> // C++20: This will be #include <format>
|
||||
#include <format> // C++20: This will be #include <format>
|
||||
#include <iostream>
|
||||
#include <array>
|
||||
|
||||
|
@ -19,11 +19,12 @@ int main() {
|
|||
++frequency[response];//.at(response);
|
||||
}
|
||||
|
||||
std::cout << "Rating Frequency \n";
|
||||
std::cout << std::format("{}{:>12}\n", "Rating", "Frequency");
|
||||
|
||||
// output each array element's value
|
||||
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));
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -1,14 +1,14 @@
|
|||
// fig06_11.cpp
|
||||
// Compute the sum of the elements of an array using accumulate.
|
||||
#include <array>
|
||||
#include <fmt/format.h>
|
||||
#include <iostream>
|
||||
#include <numeric>
|
||||
|
||||
int main() {
|
||||
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 << "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";
|
||||
std::cout << fmt::format("Total of array elements: {}\n",
|
||||
std::accumulate(std::begin(integers), std::end(integers), 0));
|
||||
}
|
||||
|
||||
/**************************************************************************
|
||||
|
|
|
@ -1,32 +1,24 @@
|
|||
// fig06_12.cpp
|
||||
// Compute the product of an array's elements using accumulate.
|
||||
#include <array>
|
||||
#include <format>
|
||||
#include <iostream>
|
||||
#include <numeric>
|
||||
|
||||
template <typename T>
|
||||
T multiply(T x, T y) {
|
||||
int multiply(int x, int y) {
|
||||
return x * y;
|
||||
}
|
||||
|
||||
|
||||
int multiply_int(int x, int y) {
|
||||
return x * y;
|
||||
}
|
||||
|
||||
|
||||
int main() {
|
||||
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; };
|
||||
|
||||
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 << "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";
|
||||
std::cout << std::format("Product of integers with a lambda: {}\n",
|
||||
std::accumulate(std::begin(integers), std::end(integers), 1,
|
||||
multiply_lambda));
|
||||
}
|
||||
|
||||
|
||||
|
|
Loading…
Reference in New Issue