Skip to content
Snippets Groups Projects
Commit bd67c4d1 authored by Sandipan Mohanty's avatar Sandipan Mohanty
Browse files

Add the first two chapters of the Book

The first two chapters of the companion book are now reasonably
updated, and can be added to the course material.
parent e57dd5c8
No related branches found
No related tags found
No related merge requests found
Showing
with 394 additions and 0 deletions
File added
#include <iostream>
void say_hello()
{
std::cout << "Hello!\n";
}
auto add(int input1, int input2) -> int
{
return input1 + input2;
}
auto sub(int input1, int input2) -> int
{
return input1 - input2;
}
auto main() -> int
{
auto v1 = add(34552, 88112);
auto v2 = add(v1, sub(335, 88112));
say_hello();
std::cout << "v1 has value : " << v1 << "\n";
std::cout << "v2 has value : " << v2 << "\n";
}
#include <iostream>
auto main() -> int
{
std::cout << "Hello, world!\n";
}
#include <print>
#include <numeric>
auto main() -> int
{
auto x = 2147483645, y = 2147483641;
std::print("Mid-point of {} and {} is {}\n", x, y, std::midpoint(x, y));
std::print("Naive mid-point of {} and {} is {}\n", x, y, (x + y)/2);
}
#include <iostream>
#include <numeric>
auto main() <- int
{
auto x = 2147483645, y = 2147483641;
std::cout << "Mid-point of " << x << " and "<< y << " is " << std::midpoint(x, y) << "\n";
std::cout << "Naive mid-point of " << x << " and " << y << " is " << (x + y)/2 << "\n";
}
#include <iostream>
#include <numeric>
auto main() <- int
{
auto x = 1000000005, y = 2000403030;
std::cout << "The greatest common divisor of ";
<< x << " and " << y << " is "
<< std::gcd(x, y) << "\n";
}
// examples/hello3b.cc // broken code for you to fix!
#include <print>
#include <numeric>
auto main() -> int
{
auto x = 1000000005, y = 2000403030;
std::print("The greatest common divisor of {} and {} is {}\n",
x, y, std::gcd(x, y));
}
// examples/hello3c.cc
#include <print>
#include <numeric>
auto main() -> int
{
auto x = 1000000005, y = 2000403030;
std::print("The greatest common divisor of {2} and {1} is {0}\n",
std::gcd(x, y), y, x);
}
// examples/hello3d.cc
#include <print>
#include <numeric>
auto main() -> int
{
auto x = 1000000005, y = 2000403030;
std::print("The greatest common divisor of {2:0>12} and {1:0>12} is {0: <}\n",
std::gcd(x, y), y, x);
std::print("The greatest common divisor of {2:0>12} and {1:0>12} is {0: <}\n",
std::gcd(132, 90), 90, 132);
std::print("The greatest common divisor of {2:0>12} and {1:0>12} is {0: <}\n",
std::gcd(51768, 77924), 77924, 51768);
}
#include <iostream>
#include 'numeric'
pkw main()
{
auto x = 15, y = 24;
std::cout << "The least common multiple of "
<< x << " and " << y << " is "
<< std::lcm(x, y) << "\n";
}
#include <print>
auto main() -> int
{
std::println("Hello world!");
}
// examples/math_functions.cc
#include <cmath>
#include <iostream>
auto main() -> int
{
double inp = 3.141592653 / 6.0;
//
std::cout << "Enter angle: ";
//
std::cin >> inp;
std::cout << "sqrt(" << inp << ") = " << std::sqrt(inp) << "\n";
std::cout << "cbrt(" << inp << ") = " << std::cbrt(inp) << "\n";
std::cout << "cos(" << inp << ") = " << std::cos(inp) << "\n";
std::cout << "sin(" << inp << ") = " << std::sin(inp) << "\n";
std::cout << "cosh(" << inp << ") = " << std::cosh(inp) << "\n";
std::cout << "sinh(" << inp << ") = " << std::sinh(inp) << "\n";
std::cout << "exp(" << inp << ") = " << std::exp(inp) << "\n";
std::cout << "erf(" << inp << ") = " << std::erf(inp) << "\n";
std::cout << "riemann_zeta(" << inp << ") = " << std::riemann_zeta(inp) << "\n";
}
// examples/math_functions_cmdln.cc
#include <cmath>
#include <iostream>
#include <string>
auto main(int argc, char* argv[]) -> int
{
double inp = 3.141592653 / 6.0;
if (argc > 1)
inp = std::stod(argv[1]);
std::cout << "sqrt(" << inp << ") = " << std::sqrt(inp) << "\n";
std::cout << "cbrt(" << inp << ") = " << std::cbrt(inp) << "\n";
std::cout << "cos(" << inp << ") = " << std::cos(inp) << "\n";
std::cout << "sin(" << inp << ") = " << std::sin(inp) << "\n";
std::cout << "cosh(" << inp << ") = " << std::cosh(inp) << "\n";
std::cout << "sinh(" << inp << ") = " << std::sinh(inp) << "\n";
std::cout << "exp(" << inp << ") = " << std::exp(inp) << "\n";
std::cout << "erf(" << inp << ") = " << std::erf(inp) << "\n";
std::cout << "riemann_zeta(" << inp << ") = " << std::riemann_zeta(inp) << "\n";
}
#include <iostream>
#include <cmath>
sin(3.0);
int main()
{
double x = 2.1;
x = x * x * x;
std::cout << x << "\n";
}
#include <print>
auto main() -> int
{
std::print();
}
#include <iostream>
#include <vector>
auto main() -> int
{
const auto G = 6.674e-11;
std::vector x{ 0., 1., 2., 3., 4., 5. };
std::vector y{ 0., 1., 2., 3., 4., 5. };
std::vector z{ 0., 1., 2., 3., 4., 5. };
std::vector m{ 1.0e5, 1.0e5, 2.0e5, 1.0e5, 1.0e5, 1.0e5 };
int size = x.size();
auto total = 0.;
// loop over index i from 0 till size.
// loop over index j from i+1 till size
// figure out where to put the end of the loops
auto dx = x[i] - x[j];
auto dy = y[i] - y[j];
auto dz = z[i] - z[j];
auto r2 = dx * dx + dy * dy + dz * dz;
total = total + G * m[i] * m[j] / r2;
}
std::cout << "Total gravitational potential energy = " << total << "\n";
}
// examples/array_or_pointer.cc
#include <iostream>
auto f(double arr[4]) -> void
{
std::cout << "size of function argument in bytes = " << sizeof(decltype(arr)) << "\n";
std::cout << *arr << "\n";
}
auto main() -> int
{
double v[4]{1, 2, 3, 4};
std::cout << "Elements of raw array...\n";
for (auto a : v) std::cout << a << "\n";
std::cout << "size of raw array in bytes = " << sizeof(decltype(v)) << "\n";
f(v);
}
// examples/asan0.cc
auto main() -> int
{
auto* X = new double[400]; // space for 400 doubles reserved on the heap
// X holds the address of where those 400 doubles are on the heap.
// For instance, X could be the numeric address 337932100.
// work with X, e.g.,
if (X != nullptr) {
for (int i = 0; i < 400; ++i) {
X[i] = 0;
}
}
// Done with calculations using X
delete[] X; // Previously allocated space for 400 doubles is now released
// X is still 337932100, but the previously allocated 400 doubles at
// that address on the heap is already gone!
if (X != nullptr) {
for (int i = 0; i < 400; ++i) {
X[i] = 1;
}
return X[33] > X[23];
}
}
#include <bit>
#include <bitset>
#include <concepts>
#include <cstddef>
#include <iomanip>
#include <iostream>
template <class T>
concept Number = std::floating_point<T> || std::integral<T>;
using std::byte;
template <Number T>
auto bits(const T& var)
{
auto loc { var };
unsigned long bytes { 0UL };
byte* in = reinterpret_cast<byte*>(&loc);
byte* out = reinterpret_cast<byte*>(&bytes);
std::copy(in, in + sizeof(T), out);
return std::bitset<8 * sizeof(T)>(bytes);
}
void showbits(double var)
{
std::cout << "bits for double value " << var << "\n";
auto b = bits(var).to_string();
std::cout << "raw = " << b << "\n";
std::cout << "sign exponent mantissa\n"
<< std::setw(6) << std::left << b[0]
<< std::setw(16) << std::left << b.substr(1UL, 11)
<< " " << b.substr(12) << "\n\n";
}
void showbits(float var)
{
std::cout << "bits for float value " << var << "\n";
auto b = bits(var).to_string();
std::cout << "raw = " << b << "\n";
std::cout << "sign exponent mantissa\n"
<< std::setw(6) << std::left << b[0]
<< std::setw(16) << std::left << b.substr(1UL, 8)
<< " " << b.substr(9) << "\n\n";
}
void showbits(auto var)
{
std::cout << "bits for " << var << " with non-floating point type : " << bits(var) << "\n";
}
int main()
{
showbits(1);
showbits(1U - 2U);
showbits(-1);
showbits(1.0);
showbits(1.25);
showbits(1.25F);
showbits(1.5);
showbits(1.75);
showbits(2.0);
showbits(2.25);
showbits(2.5);
showbits(2.75);
showbits(3.0);
showbits(4.0);
showbits(8.0);
showbits(0.0);
showbits(-0.0);
std::cout << "-0.0 == 0.0 is " << std::boolalpha << (-0.0 == 0.0) << "\n\n";
showbits(1.0e-100);
std::cout << "1.0e-100 == 0.0 is " << std::boolalpha << (1.0e-100 == 0.0) << "\n\n";
showbits(8UL);
}
#include <bitset>
#include <cstddef>
#include <iomanip>
#include <iostream>
using byte = unsigned char;
template <class T>
auto bits(T var)
{
unsigned long bytes { 0UL };
byte* in = reinterpret_cast<byte*>(&var);
byte* out = reinterpret_cast<byte*>(&bytes);
std::copy(in, in + sizeof(var), out);
return std::bitset<8 * sizeof(decltype(var))>(bytes);
}
void showbits(double var)
{
std::cout << "bits for double value " << var << "\n";
auto b = bits(var).to_string();
std::cout << "raw = " << b << "\n";
std::cout << "sign exponent mantissa\n"
<< std::setw(6) << std::left << b[0]
<< std::setw(16) << std::left << b.substr(1UL, 11)
<< " " << b.substr(12) << "\n\n";
}
void showbits(float var)
{
std::cout << "bits for float value " << var << "\n";
auto b = bits(var).to_string();
std::cout << "raw = " << b << "\n";
std::cout << "sign exponent mantissa\n"
<< std::setw(6) << std::left << b[0]
<< std::setw(16) << std::left << b.substr(1UL, 8)
<< " " << b.substr(9) << "\n\n";
}
template <class T>
void showbits(T var)
{
std::cout << "bits for " << var << " with non-floating point type : " << bits(var) << "\n";
}
int main()
{
showbits(1);
showbits(1U - 2U);
showbits(-1);
showbits(1.0);
showbits(1.25);
showbits(1.25F);
showbits(1.5);
showbits(1.75);
showbits(2.0);
showbits(4.0);
showbits(8.0);
showbits(0.0);
showbits(-0.0);
std::cout << "-0.0 == 0.0 is " << std::boolalpha << (-0.0 == 0.0) << "\n\n";
showbits(1.0e-100);
std::cout << "1.0e-100 == 0.0 is " << std::boolalpha << (1.0e-100 == 0.0) << "\n\n";
showbits(8UL);
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment