Compare commits
2 Commits
89880f6615
...
a5f7685dcb
Author | SHA1 | Date |
---|---|---|
davoudn | a5f7685dcb | |
davoudn | 1196fe88f9 |
Binary file not shown.
|
@ -1,35 +1,50 @@
|
|||
// 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 << 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
|
||||
for (int& item : items) { // item is a reference to an int
|
||||
item *= 2;
|
||||
}
|
||||
|
||||
// display items after modification
|
||||
std::cout << "\nitems after modification: ";
|
||||
for (const int& item : items) {
|
||||
std::cout << std::format("{} ", item);
|
||||
for (int item : items2) { // item is a reference to an int
|
||||
item *= 2;
|
||||
}
|
||||
|
||||
// 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
|
||||
std::cout << "\n\ncalculating a running total of items' values:\n";
|
||||
for (int runningTotal{0}; const int& item : items) {
|
||||
runningTotal += item;
|
||||
std::cout << std::format("item: {}; running total: {}\n",
|
||||
item, runningTotal);
|
||||
std::cout << " running total "<< item << runningTotal<<"\n";
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -1,6 +1,5 @@
|
|||
// fig06_04.cpp
|
||||
// Set array values to the even integers from 2 to 10.
|
||||
#include <fmt/format.h>
|
||||
#include <iostream>
|
||||
#include <array>
|
||||
|
||||
|
@ -16,7 +15,7 @@ int main() {
|
|||
|
||||
// output contents of array values in tabular format
|
||||
for (const int& value : values) {
|
||||
std::cout << fmt::format("{} ", value);
|
||||
std::cout << value << "\n";
|
||||
}
|
||||
|
||||
std::cout << '\n';
|
||||
|
|
|
@ -13,7 +13,7 @@ int main() {
|
|||
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) {
|
||||
// output bar labels ("00-09:", ..., "90-99:", "100:")
|
||||
if (i < 10) {
|
||||
std::cout << std::format("{:03d}-{:03d}: ",
|
||||
i * 100, (i * 100) + 9);
|
||||
std::cout << i * 100 << " " << (i * 100) + 9 << "\n";
|
||||
}
|
||||
else {
|
||||
std::cout << std::format("{:>7d}: ", 100);
|
||||
std::cout << 100 << "\n";
|
||||
}
|
||||
|
||||
++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{1, 6};
|
||||
std::uniform_int_distribution randomDie{0, 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 << std::format("{}{:>13}\n", "Face", "Frequency");
|
||||
std::cout << "Face " << " Frequency \n";
|
||||
|
||||
// output each array element's value
|
||||
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
|
||||
// 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,12 +19,11 @@ int main() {
|
|||
++frequency[response];//.at(response);
|
||||
}
|
||||
|
||||
std::cout << std::format("{}{:>12}\n", "Rating", "Frequency");
|
||||
std::cout << "Rating Frequency \n";
|
||||
|
||||
// output each array element's value
|
||||
for (size_t rating{1}; rating < frequency.size(); ++rating) {
|
||||
std::cout << std::format("{:>6}{:>12}\n",
|
||||
rating, frequency.at(rating));
|
||||
std::cout << rating << " " << frequency.at(rating) << "\n";
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -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 << fmt::format("Total of array elements: {}\n",
|
||||
std::accumulate(std::begin(integers), std::end(integers), 0));
|
||||
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";
|
||||
}
|
||||
|
||||
/**************************************************************************
|
||||
|
|
|
@ -1,24 +1,32 @@
|
|||
// fig06_12.cpp
|
||||
// Compute the product of an array's elements using accumulate.
|
||||
#include <array>
|
||||
#include <format>
|
||||
#include <iostream>
|
||||
#include <numeric>
|
||||
|
||||
int multiply(int x, int y) {
|
||||
template <typename T>
|
||||
T multiply(T x, T 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 << std::format("Product of integers: {}\n", std::accumulate(
|
||||
std::begin(integers), std::end(integers), 1, multiply));
|
||||
std::cout << "Product of integers: " << std::accumulate( std::begin(integers), std::end(integers), 1, multiply<int>) << "\n";
|
||||
|
||||
std::cout << std::format("Product of integers with a 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(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";
|
||||
}
|
||||
|
||||
|
||||
|
|
Loading…
Reference in New Issue