From 00c5fece016f9471e92f5d46de7149271cec8154 Mon Sep 17 00:00:00 2001 From: mustafaaydn Date: Mon, 5 Feb 2024 01:13:45 +0300 Subject: add solutions --- challenge-254/witawayar/cpp/ch-1.cpp | 37 ++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 challenge-254/witawayar/cpp/ch-1.cpp (limited to 'challenge-254/witawayar/cpp/ch-1.cpp') 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 // abs, round, cbrt +#include +#include // form the error message +#include + +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> 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"; + +} -- cgit