From 986dc102d78668e5eaa5091d86c74ce50afaf0eb Mon Sep 17 00:00:00 2001 From: Jethro Stapelbroek Date: Tue, 17 Oct 2023 21:07:35 +0200 Subject: [PATCH] Arithmetic Functions --- arithmeticfunctions.cpp | 21 +++++++++++++++++++++ arithmeticfunctions.h | 20 ++++++++++++++++++++ 2 files changed, 41 insertions(+) create mode 100644 arithmeticfunctions.cpp create mode 100644 arithmeticfunctions.h diff --git a/arithmeticfunctions.cpp b/arithmeticfunctions.cpp new file mode 100644 index 0000000..094545e --- /dev/null +++ b/arithmeticfunctions.cpp @@ -0,0 +1,21 @@ +#include "arithmeticfunctions.h" +#include + +// Compute x^3 +int cube(int x){ + return pow(x, 3); +} + +// Compute the maximum of x and y +int max(int x, int y){ + if (x>y) { + return x; + } else { + return y; + } +} + +// Compute x - y +int difference(int x, int y){ + return x - y; +} diff --git a/arithmeticfunctions.h b/arithmeticfunctions.h new file mode 100644 index 0000000..f7491db --- /dev/null +++ b/arithmeticfunctions.h @@ -0,0 +1,20 @@ +#include + +int cube(int x); +int max(int x, int y); +int difference(int x, int y); + +int main() { + int cube2 = cube(2); + if (cube2 != 8) { + std::cout << "ERROR: cube(2) = " << cube2 << ", expected 8" << std::endl; + } + int max85 = max(8, 5); + if (max85 != 8) { + std::cout << "ERROR: max(8, 5) = " << max85 << ", expected 8" << std::endl; + } + int diff102 = difference(10, 2); + if (diff102 != 8) { + std::cout << "ERROR: difference(10, 2) = " << diff102 << ", expected 8" << std::endl; + } +}