Compare commits

...

2 Commits

Author SHA1 Message Date
davoudn 2e54dfadfe a.out was removed 2024-04-23 09:33:49 +03:30
davoudn 9a31a61d2c ch06 and ch07 sample codes were modified 2024-04-23 09:32:28 +03:30
11 changed files with 98 additions and 21 deletions

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

View File

@ -1,6 +1,5 @@
// fig07_02.cpp
// Pass-by-value used to cube a variable's value.
#include <fmt/format.h>
#include <iostream>
int cubeByValue(int n); // prototype
@ -8,9 +7,9 @@ int cubeByValue(int n); // prototype
int main() {
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
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

View File

@ -8,10 +8,10 @@ void cubeByReference(int* nPtr); // prototype
int main() {
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

View File

@ -1,7 +1,6 @@
// fig07_10.cpp
// Sizeof operator when used on a built-in array's name
// returns the number of bytes in the built-in array.
#include <fmt/format.h>
#include <iostream>
size_t getSize(double* ptr); // prototype
@ -9,11 +8,13 @@ size_t getSize(double* ptr); // prototype
int main() {
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

View File

@ -1,13 +1,12 @@
// fig07_13.cpp
// Reading in command-line arguments.
#include <fmt/format.h>
#include <iostream>
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) {
std::cout << fmt::format("{}\n", argv[i]);
for (int i{1}; i < argc; ++i) {
std::cout << argv[i] << "\n" ;
}
}