diff --git a/docs/hackerrank/warmup/simple_array_sum.md b/docs/hackerrank/warmup/simple_array_sum.md new file mode 100644 index 0000000..7cd4e53 --- /dev/null +++ b/docs/hackerrank/warmup/simple_array_sum.md @@ -0,0 +1,45 @@ +# [Simple Array Sum](https://www.hackerrank.com/challenges/simple-array-sum/problem) + +Difficulty: #easy +Category: #warmup + +Given an array of integers, find the sum of its elements. +For example, if the array $ ar = [1, 2, 3], 1 + 2 + 3 = 6 $, so return $ 6 $. + +## Function Description + +Complete the simpleArraySum function in the editor below. It must return the sum + of the array elements as an integer. +simpleArraySum has the following parameter(s): + +- ar: an array of integers + +## Input Format + +The first line contains an integer, , denoting the size of the array. +The second line contains space-separated integers representing the array's elements. + +## Constraints + +$ 0 < n, ar[i] \leq 1000 $ + +## Output Format + +Print the sum of the array's elements as a single integer. + +## Sample Input + +```text +6 +1 2 3 4 10 11 +``` + +## Sample Output + +```text +31 +``` + +## Explanation + +We print the sum of the array's elements: $ 1 + 2 + 3 + 4 + 10 + 11 = 31 $. diff --git a/src/lib/exercises/include/exercises/hackerrank/warmup/simple_array_sum.h b/src/lib/exercises/include/exercises/hackerrank/warmup/simple_array_sum.h new file mode 100644 index 0000000..9d8217d --- /dev/null +++ b/src/lib/exercises/include/exercises/hackerrank/warmup/simple_array_sum.h @@ -0,0 +1,11 @@ +#pragma once + +#ifdef __cplusplus +extern "C" { +#endif + +int HACKERRANK_WARMUP_simpleArraySum(int ar_count, const int *ar); + +#ifdef __cplusplus +} // extern "C" +#endif diff --git a/src/lib/exercises/src/hackerrank/warmup/simple_array_sum.c b/src/lib/exercises/src/hackerrank/warmup/simple_array_sum.c new file mode 100644 index 0000000..18d1fb5 --- /dev/null +++ b/src/lib/exercises/src/hackerrank/warmup/simple_array_sum.c @@ -0,0 +1,15 @@ +#include + +/** + * @link Problem definition [[docs/hackerrank/warmup/simple_array_sum.md]] + */ + +int HACKERRANK_WARMUP_simpleArraySum(int ar_count, const int *ar) { + int total = 0; + + for (int i = 0; i < ar_count; i++) { + total += ar[i]; + } + + return total; +} diff --git a/src/tests/unit/lib/hackerrank/warmup/simple_array_sum.test.cpp b/src/tests/unit/lib/hackerrank/warmup/simple_array_sum.test.cpp new file mode 100644 index 0000000..3d4d493 --- /dev/null +++ b/src/tests/unit/lib/hackerrank/warmup/simple_array_sum.test.cpp @@ -0,0 +1,31 @@ +#include + +#include +#include +#include +#include +#include + +using json = nlohmann::json; + +TEST_CASE("simpleArraySum JSON Test Cases", + "[hackerrank] [jsontestcase] [warmup]") { + std::filesystem::path cwd = std::filesystem::current_path(); + std::string path = + cwd.string() + + "/unit/lib/hackerrank/warmup/simple_array_sum.testcases.json"; + + INFO("simpleArraySum JSON test cases FILE: " << path); + + std::ifstream f(path); + json data = json::parse(f); + + for (auto testcase : data) { + auto size = static_cast(testcase["input"].size()); + std::vector input_vector = testcase["input"]; + const int *input_array = input_vector.data(); + + int result = HACKERRANK_WARMUP_simpleArraySum(size, input_array); + CHECK(result == testcase["expected"]); + } +} diff --git a/src/tests/unit/lib/hackerrank/warmup/simple_array_sum.testcases.json b/src/tests/unit/lib/hackerrank/warmup/simple_array_sum.testcases.json new file mode 100644 index 0000000..c916b56 --- /dev/null +++ b/src/tests/unit/lib/hackerrank/warmup/simple_array_sum.testcases.json @@ -0,0 +1,6 @@ +[ + { + "input": [1, 2, 3, 4, 10, 11], + "expected": 31 + } +]