From c040e950295d9ef9a3a952b8319ba074a51b1513 Mon Sep 17 00:00:00 2001 From: mustafaaydn Date: Mon, 22 Jan 2024 00:12:03 +0300 Subject: add solutions --- challenge-252/witawayar/cpp/ch-2.cpp | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 challenge-252/witawayar/cpp/ch-2.cpp (limited to 'challenge-252/witawayar/cpp/ch-2.cpp') diff --git a/challenge-252/witawayar/cpp/ch-2.cpp b/challenge-252/witawayar/cpp/ch-2.cpp new file mode 100644 index 0000000000..91b103757b --- /dev/null +++ b/challenge-252/witawayar/cpp/ch-2.cpp @@ -0,0 +1,28 @@ +// g++ -Wall -Wextra -Wpedantic -std=c++17 cpp/ch-2.cpp +#include // cout +#include // accumulate +#include // stringstream +#include + +std::vector fun(int const& n) { + std::vector range(n); + std::iota(range.begin(), range.end(), -(n / 2)); + return range; +} + +int main(void) { + // Tests + std::vector tests{5, 3, 1}; + + for (const unsigned& input : tests) { + std::vector got = fun(input); + int sum = std::accumulate(got.cbegin(), got.cend(), 0); + if (sum != 0) { + std::stringstream error_msg; + error_msg << "Expected " << 0 << ", got " << sum; + throw std::runtime_error(error_msg.str()); + } + } + + std::cout << "done-testing, success\n"; +} -- cgit