Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 45 additions & 0 deletions docs/hackerrank/warmup/simple_array_sum.md
Original file line number Diff line number Diff line change
@@ -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 $.
Original file line number Diff line number Diff line change
@@ -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
15 changes: 15 additions & 0 deletions src/lib/exercises/src/hackerrank/warmup/simple_array_sum.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#include <exercises/hackerrank/warmup/simple_array_sum.h>

/**
* @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;
}
31 changes: 31 additions & 0 deletions src/tests/unit/lib/hackerrank/warmup/simple_array_sum.test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#include <catch2/catch_test_macros.hpp>

#include <exercises/hackerrank/warmup/simple_array_sum.h>
#include <filesystem>
#include <fstream>
#include <nlohmann/json.hpp>
#include <vector>

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<int>(testcase["input"].size());
std::vector<int> input_vector = testcase["input"];
const int *input_array = input_vector.data();

int result = HACKERRANK_WARMUP_simpleArraySum(size, input_array);
CHECK(result == testcase["expected"]);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[
{
"input": [1, 2, 3, 4, 10, 11],
"expected": 31
}
]