diff options
| author | mustafaaydn <isaidwhynot@protonmail.com> | 2024-02-05 01:13:45 +0300 |
|---|---|---|
| committer | mustafaaydn <isaidwhynot@protonmail.com> | 2024-02-05 01:13:45 +0300 |
| commit | 00c5fece016f9471e92f5d46de7149271cec8154 (patch) | |
| tree | 917a84b1897483f18e578e6bf8c25ab32b57eb21 /challenge-254/witawayar/cpp/ch-1.cpp | |
| parent | 71046c66f45423d9064fe32ac773d9caa143d36e (diff) | |
| download | perlweeklychallenge-club-00c5fece016f9471e92f5d46de7149271cec8154.tar.gz perlweeklychallenge-club-00c5fece016f9471e92f5d46de7149271cec8154.tar.bz2 perlweeklychallenge-club-00c5fece016f9471e92f5d46de7149271cec8154.zip | |
add solutions
Diffstat (limited to 'challenge-254/witawayar/cpp/ch-1.cpp')
| -rw-r--r-- | challenge-254/witawayar/cpp/ch-1.cpp | 37 |
1 files changed, 37 insertions, 0 deletions
diff --git a/challenge-254/witawayar/cpp/ch-1.cpp b/challenge-254/witawayar/cpp/ch-1.cpp new file mode 100644 index 0000000000..f08e21c8c6 --- /dev/null +++ b/challenge-254/witawayar/cpp/ch-1.cpp @@ -0,0 +1,37 @@ +// g++ -Wall -Wextra -Wpedantic -std=c++17 cpp/ch-1.cpp +#include <cmath> // abs, round, cbrt +#include <iostream> +#include <sstream> // form the error message +#include <vector> + +static constexpr double TOLERANCE = 1E-6; + +bool fun(int n) { + double cube_root = std::cbrt(n); + return std::abs(cube_root - std::round(cube_root)) < TOLERANCE; +} + +int main() { + // Tests + std::vector<std::pair<int, int>> tests = { + {0, true}, + {1, true}, + {-343, true}, + {27, true}, + {122, false}, + {6, false}, + {777, false} + }; + + for (const auto& [input, expected_output] : tests) { + int got = fun(input); + if (got != expected_output) { + std::stringstream error_msg; + error_msg << "Expected " << expected_output << ", got " << got; + throw std::runtime_error(error_msg.str()); + } + } + + std::cout << "done-testing, success\n"; + +} |
