An example of using
bindings C++17 (-std=c++17).
I will attach the code that can be compiled in the C++ shell.
It's an exercise I translated from 1988 BASIC to modern C++ 2020.
Since there is not an option at
http://cpp.sh/ to choose c++17, I will upload a second version that does not use "bindings." That is, I will simply replace
for (const auto & [f, eq] : Functions) { with
for (const auto & t : Functions) {
and then t.first for function f, t.second for string eq.
Of course, if you have access to C++ compiler, like g++:
g++ -g -Wall do6-5_all_table.cpp -std=c++17 -o doit_6-5_all
To get very cool printable copy of the code, install enscript; then:
enscript -1rG --portrait --line-numbers -p do6-5_all_table.ps --highlight=cpp -c do6-5_all_table.cpp
A few future young and old adults may love me for doing this (after I'm dead, of course!) ...
1. Write and run a program to approximate the slope of
the line tangent to the graph of the function f(x) = x^3 - 4*x^2
at x = -4, -3.5, ..., 4.
Use the formula S(x) = ( f(x + h) - f(x) ) / h
with h = 0.0001
2. Modify and run the program in Exercise 1 for each function f
to print a table of values in three columns. The columns should
include x, the calculated slope at x, and an indication of whether
the slope is positive, negative, or zero. Use the table to help
determine intervals where f increases, where f decreases, and
where f'(x) = 0.
(a) f(x) = (1/4)*x^3 - x
(b) f(x) = x^4/2 - 3*x^2
(c) f(x) = x^5 - 2*x^4 + x^3 + x^2 - 2*x + 1
(d) f(x) = -x^5 + 10*x
3. Modify and run the program for f(x) = sqrt(x^2 + 1) */
Just copy and paste the following (inside quote) to the C++ shell:
#include <iostream>
#include <cmath>
#include <iomanip>
#include <string>
#include <vector>
#include <utility> // for std::pair
double Dx(double(*f)(double x), double x0, double dx);
double f1(double x);
double f2a(double x);
double f2b(double x);
double f2c(double x);
double f2d(double x);
double f3(double x);
// PAIR:
// Store pointers to functions: double(*)(double)
// Store equations which define functions: std::string
using Function = std::pair<double(*)(double), std::string>;
int main()
{
std::vector<Function> Functions; // a vector of such pairs
// Load up the vector with pointers to functions : FIRST
// Load the vector with function definitions as strings : SECOND
std::string eq = "f(x) = x^3 - 4*x^2";
Function f = std::make_pair(f1, eq);
Functions.push_back(f);
eq = "f(x) = (1/4)*x^3 - x";
f = std::make_pair(f2a, eq);
Functions.push_back(f);
eq = "f(x) = x^4/2 - 3*x^2";
f = std::make_pair(f2b, eq);
Functions.push_back(f);
eq = "f(x) = x^5 - 2*x^4 + x^3 + x^2 - 2*x + 1";
f = std::make_pair(f2c, eq);
Functions.push_back(f);
eq = "f(x) = -x^5 + 10*x";
f = std::make_pair(f2d, eq);
Functions.push_back(f);
eq = "f(x) = sqrt(x^2 + 1)";
f = std::make_pair(f3, eq);
Functions.push_back(f);
for (auto i : Functions)
std::cout << "\nFUNCTION: " << i.second << '\n';
std::cout << "\n\n";
double h = 0.0001;
char slope;
double tol = 0.0001;
for (const auto & t : Functions) {
std::cout << "\nFunction: " << t.second
<< "\n---------------------------------------\n"
<< "x\tS\t\tslope\n---------------------------------------\n";
for (double j = -4; j <= 4; j += 0.5) {
double S = Dx(t.first, j, h);
std::cout.precision(10);
if (std::fabs(S) < tol) slope = '0';
else {
if (S > 0) slope = '+';
if (S < 0) slope = '-';
}
std::cout << j << "\t" << S << "\t " << slope << '\n';
}
}
return 0;
}
double Dx(double(*f)(double x), double x0, double dx) {
return ( f(x0 + dx) - f(x0) ) / dx;
}
double f1(double x) {
return std::pow(x, 3) - 4*x*x;
}
double f2a(double x) {
return std::pow(x, 3)/4.0 - x;
}
double f2b(double x) {
return std::pow(x, 4)/2 - 3*x*x;
}
double f2c(double x) {
return std::pow(x, 5) - 2*std::pow(x, 4) + std::pow(x, 3) + x*x - 2*x + 1;
}
double f2d(double x) {
return -std::pow(x, 5) + 10*x;
}
double f3(double x) {
return std::sqrt(x*x + 1);
}
/*
NOTES:
double(*f)(double x)
The above tells the compiler that the parameter f is a pointer to a function.
The pointed-to function takes a double input and returns a double.
bindings C++17 (-std=c++17): for (const auto & [f, eq] : Functions)
AND std::pow from <cmath>
g++ -g -Wall do6-5_all_table.cpp -std=c++17 -o doit_6-5_all
enscript -1rG --portrait --line-numbers -p do6-5_all_table.ps
--highlight=cpp -c do6-5_all_table.cpp
if using do6-5_not++17.cpp (the code insode the above quote) and not pasting into
http://cpp.sh/ :
g++ -g -Wall do6-5_not++17.cpp -o doit_6-5_all
*/