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,8 @@
#include <iostream>
#include <vector>

#pragma once

namespace hackerrank::warmup {
int simpleArraySum(std::vector<int> ar);
}
29 changes: 29 additions & 0 deletions src/lib/exercises/src/hackerrank/warmup/simple_array_sum.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#include <exercises/hackerrank/warmup/simple_array_sum.hpp>

/**
* @link Problem definition [[docs/hackerrank/warmup/simple_array_sum.md]]
*/

#include <iostream>
#include <cmath>
#include <vector>

using namespace std;

string ltrim(const string &);
string rtrim(const string &);
vector<string> split(const string &);


namespace hackerrank::warmup {

int simpleArraySum(std::vector<int> ar) {
int accum = 0;

for(const int i : ar) {
accum += i;
}
return accum;
}

}
26 changes: 26 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,26 @@
#include <catch2/catch_test_macros.hpp>

#include <exercises/hackerrank/warmup/simple_array_sum.hpp>
#include <iostream>
#include <vector>

#include <filesystem>
#include <fstream>
#include <nlohmann/json.hpp>
using json = nlohmann::json;

TEST_CASE("simpleArraySum", "[warmup]")
{
std::filesystem::path cwd = std::filesystem::current_path();
std::string path = cwd.string() + "/unit/lib/hackerrank/warmup/simple_array_sum.testcase.json";

INFO("simpleArraySum JSON test cases FILE: " << path);

std::ifstream f(path);
json data = json::parse(f);

for (auto testcase : data) {
int result = hackerrank::warmup::simpleArraySum(testcase["input"]);
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
}
]