diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2024-01-21 21:19:29 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-01-21 21:19:29 +0000 |
| commit | 6e1728468bff842c9cfe1642883cbcce9fcbb04d (patch) | |
| tree | 9c20afdde8ffbdaafc05a892af73100600d5d4db /challenge-252/witawayar/cpp/ch-2.cpp | |
| parent | d9158621438166c0d4510ffe001bb339107bd6e8 (diff) | |
| parent | c040e950295d9ef9a3a952b8319ba074a51b1513 (diff) | |
| download | perlweeklychallenge-club-6e1728468bff842c9cfe1642883cbcce9fcbb04d.tar.gz perlweeklychallenge-club-6e1728468bff842c9cfe1642883cbcce9fcbb04d.tar.bz2 perlweeklychallenge-club-6e1728468bff842c9cfe1642883cbcce9fcbb04d.zip | |
Merge pull request #9437 from mustafaaydn/challenge_252
Solutions for the 252nd challenge
Diffstat (limited to 'challenge-252/witawayar/cpp/ch-2.cpp')
| -rw-r--r-- | challenge-252/witawayar/cpp/ch-2.cpp | 28 |
1 files changed, 28 insertions, 0 deletions
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 <iostream> // cout +#include <numeric> // accumulate +#include <sstream> // stringstream +#include <vector> + +std::vector<int> fun(int const& n) { + std::vector<int> range(n); + std::iota(range.begin(), range.end(), -(n / 2)); + return range; +} + +int main(void) { + // Tests + std::vector<unsigned> tests{5, 3, 1}; + + for (const unsigned& input : tests) { + std::vector<int> 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"; +} |
