ch06 and ch07 sample codes were modified

This commit is contained in:
davoudn 2024-04-23 09:32:28 +03:30
parent a5f7685dcb
commit 9a31a61d2c
11 changed files with 98 additions and 21 deletions

Binary file not shown.

View File

@ -15,18 +15,63 @@ int multiply_int(int x, int y) {
} }
double f(double& x){
return x*x;
}
template <typename T, typename F, long unsigned int N>
T integrate (std::array<T, N> x, F f){
T tmp{T(0)};
for (auto s: x){
tmp+= f(s);
}
return tmp;
}
typedef double (*FUNC)(double&);
template <typename T, long unsigned int N>
T integrate2 (std::array<T, N> x, FUNC f){
T tmp{T(0)};
for (auto s: x){
tmp+= f(s);
}
return tmp;
}
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}; 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; };
auto f_lambda = [](auto& x) {return x*x;};
auto g_lambda = [&integers](auto& x) {
auto tmp{0};
for (int i{0};auto s: x){
tmp += s*integers[i];
}
return tmp;
};
std::cout << "Product of integers: " << std::accumulate( std::begin(integers), std::end(integers), 1, multiply<int>) << "\n"; std::cout << "Product of integers: " << std::accumulate( std::begin(integers), std::end(integers), 1, multiply<int>) << "\n";
std::cout << "Product of integers: " << std::accumulate( integers.cbegin(), integers.cend(), 1, multiply<int>) << "\n";
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(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_int) << "\n";
std::cout << "Product of integers with a lambda: " << std::accumulate(std::begin(floats), std::end(floats), 1, multiply_lambda) << "\n"; std::cout << "Product of integers with a lambda: " << std::accumulate(std::begin(floats), std::end(floats), 1, multiply_lambda) << "\n";
std::cout << "Integration2 template " << integrate2(floats, f) << "\n";
std::cout << "Integration full template " << integrate(floats, f_lambda) << "\n";
std::cout << "Lambda test " << g_lambda(floats) << "\n";
} }

View File

@ -1,7 +1,6 @@
// fig06_13.cpp // fig06_13.cpp
// Functional-style programming with C++20 ranges and views. // Functional-style programming with C++20 ranges and views.
#include <array> #include <array>
#include <fmt/format.h>
#include <iostream> #include <iostream>
#include <numeric> #include <numeric>
#include <ranges> #include <ranges>
@ -10,10 +9,10 @@ int main() {
// lambda to display results of range operations // lambda to display results of range operations
auto showValues{ auto showValues{
[](auto& values, const std::string& message) { [](auto& values, const std::string& message) {
std::cout << fmt::format("{}: ", message); std::cout << message << "\n";
for (const auto& value : values) { for (const auto& value : values) {
std::cout << fmt::format("{} ", value); std::cout << value << " ";
} }
std::cout << '\n'; std::cout << '\n';
@ -40,8 +39,8 @@ int main() {
showValues(values4, "Squares of even integers"); showValues(values4, "Squares of even integers");
// total the squares of the even integers // total the squares of the even integers
std::cout << fmt::format("Sum squares of even integers 2-10: {}\n", std::cout << "Sum squares of even integers 2-10: \n",
std::accumulate(std::begin(values4), std::end(values4), 0)); std::accumulate(std::begin(values4), std::end(values4), 0);
// process a container's elements // process a container's elements
constexpr std::array numbers{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; constexpr std::array numbers{1, 2, 3, 4, 5, 6, 7, 8, 9, 10};

View File

@ -0,0 +1,33 @@
// resizing vector
#include <iostream>
#include <vector>
#include <array>
int main ()
{
std::vector<int> myvector;
// set some initial content:
for (int i=1;i<10;i++) myvector.push_back(i);
myvector.resize(5);
std::cout << "myvector contains:";
for (int i=0;i<myvector.size();i++)
std::cout << ' ' << myvector[i];
std::cout << '\n';
myvector.resize(8,100);
std::cout << "myvector contains:";
for (int i=0;i<myvector.size();i++)
std::cout << ' ' << myvector[i];
std::cout << '\n';
myvector.resize(12);
std::cout << "myvector contains:";
for (int i=0;i<myvector.size();i++)
std::cout << ' ' << myvector[i];
std::cout << '\n';
return 0;
}

Binary file not shown.

View File

@ -1,6 +1,7 @@
// fig07_01.cpp // fig07_01.cpp
// Pointer operators & and *. // Pointer operators & and *.
#include <iostream> #include <iostream>
#include <array>
using namespace std; using namespace std;
int main() { int main() {

View File

@ -1,6 +1,5 @@
// fig07_02.cpp // fig07_02.cpp
// Pass-by-value used to cube a variable's value. // Pass-by-value used to cube a variable's value.
#include <fmt/format.h>
#include <iostream> #include <iostream>
int cubeByValue(int n); // prototype int cubeByValue(int n); // prototype
@ -8,9 +7,9 @@ int cubeByValue(int n); // prototype
int main() { int main() {
int number{5}; int number{5};
std::cout << fmt::format("Original value of number is {}\n", number); std::cout << "Original value of number is " << number << "\n";
number = cubeByValue(number); // pass number by value to cubeByValue number = cubeByValue(number); // pass number by value to cubeByValue
std::cout << fmt::format("New value of number is {}\n", number); std::cout << "New value of number is " << number <<"\n";
} }
// calculate and return cube of integer argument // calculate and return cube of integer argument

View File

@ -8,10 +8,10 @@ void cubeByReference(int* nPtr); // prototype
int main() { int main() {
int number{5}; int number{5};
std::cout << "Original value of number is " << number << "\n";
cubeByValue(number); // pass number by value to cubeByValue
std::cout << "New value of number is " << number <<"\n";
std::cout << fmt::format("Original value of number is {}\n", number);
cubeByReference(&number); // pass number address to cubeByReference
std::cout << fmt::format("New value of number is {}\n", number);
} }
// calculate cube of *nPtr; modifies variable number in main // calculate cube of *nPtr; modifies variable number in main

View File

@ -1,7 +1,6 @@
// fig07_10.cpp // fig07_10.cpp
// Sizeof operator when used on a built-in array's name // Sizeof operator when used on a built-in array's name
// returns the number of bytes in the built-in array. // returns the number of bytes in the built-in array.
#include <fmt/format.h>
#include <iostream> #include <iostream>
size_t getSize(double* ptr); // prototype size_t getSize(double* ptr); // prototype
@ -9,11 +8,13 @@ size_t getSize(double* ptr); // prototype
int main() { int main() {
double numbers[20]; // 20 doubles; occupies 160 bytes on our system double numbers[20]; // 20 doubles; occupies 160 bytes on our system
std::cout << fmt::format("Number of bytes in numbers is {}\n\n",
sizeof(numbers));
std::cout << fmt::format("Number of bytes returned by getSize is {}\n",
getSize(numbers)); std::cout << "Number of bytes in numbers is " << sizeof(numbers)<<"\n";
std::cout << "Actual nukber of elements : " << sizeof(numbers)/sizeof(double) << "\n";
std::cout << "Number of bytes returned by getSize is " << getSize(numbers) << "\n";
} }
// return size of ptr // return size of ptr

View File

@ -1,13 +1,12 @@
// fig07_13.cpp // fig07_13.cpp
// Reading in command-line arguments. // Reading in command-line arguments.
#include <fmt/format.h>
#include <iostream> #include <iostream>
int main(int argc, char* argv[]) { int main(int argc, char* argv[]) {
std::cout << fmt::format("Number of arguments: {}\n\n", argc); std::cout << "Number of arguments: " << argc << "\n";
for (int i{0}; i < argc; ++i) { for (int i{1}; i < argc; ++i) {
std::cout << fmt::format("{}\n", argv[i]); std::cout << argv[i] << "\n" ;
} }
} }