Compare commits

..

No commits in common. "2e54dfadfe35390276e36845d66caf362a6da153" and "ad3dfdc83b0c63ef851cc85f2f4b1ad3dac5364f" have entirely different histories.

11 changed files with 21 additions and 98 deletions

Binary file not shown.

View File

@ -15,63 +15,18 @@ 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};
std::array floats {1.0, 2.0, 3.0, 4.0, 5.0};
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 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,6 +1,7 @@
// fig06_13.cpp
// Functional-style programming with C++20 ranges and views.
#include <array>
#include <fmt/format.h>
#include <iostream>
#include <numeric>
#include <ranges>
@ -9,10 +10,10 @@ int main() {
// lambda to display results of range operations
auto showValues{
[](auto& values, const std::string& message) {
std::cout << message << "\n";
std::cout << fmt::format("{}: ", message);
for (const auto& value : values) {
std::cout << value << " ";
std::cout << fmt::format("{} ", value);
}
std::cout << '\n';
@ -39,8 +40,8 @@ int main() {
showValues(values4, "Squares of even integers");
// total the squares of the even integers
std::cout << "Sum squares of even integers 2-10: \n",
std::accumulate(std::begin(values4), std::end(values4), 0);
std::cout << fmt::format("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

@ -1,33 +0,0 @@
// 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;
}

View File

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

View File

@ -1,5 +1,6 @@
// fig07_02.cpp
// Pass-by-value used to cube a variable's value.
#include <fmt/format.h>
#include <iostream>
int cubeByValue(int n); // prototype
@ -7,9 +8,9 @@ int cubeByValue(int n); // prototype
int main() {
int number{5};
std::cout << "Original value of number is " << number << "\n";
std::cout << fmt::format("Original value of number is {}\n", number);
number = cubeByValue(number); // pass number by value to cubeByValue
std::cout << "New value of number is " << number <<"\n";
std::cout << fmt::format("New value of number is {}\n", number);
}
// 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,6 +1,7 @@
// 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
@ -8,13 +9,11 @@ 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 << "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";
std::cout << fmt::format("Number of bytes returned by getSize is {}\n",
getSize(numbers));
}
// return size of ptr

View File

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