Simple Array Sum Hackerrank Solution C++. Take multiple inputs of the same type and store the input in an array after that with the help of the third variable add or the sum of all available and print the sum of an array. Here is a huge collection of programs in C, C++, and Java. 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, n, denoting the size of the array.
The second line contains n space-separated integers representing the array's elements.
Constraints
0 < n, ar[i] <= 1000
Output Format
Print the sum of the array's elements as a single integer.
Sample Input
6
1 2 3 4 10 11
Sample Output
31
Explanation
We print the sum of the array's elements: 1 + 2 + 3 + 4 + 10 + 11 = 31.
Submit the Solution Here: Click Here
Simple Array Sum Hackerrank Solution C++
#include <bits/stdc++.h>
using namespace std;
string ltrim(const string &);
string rtrim(const string &);
vector<string> split(const string &);
/*
*Complete the 'simpleArraySum' function below.
*
*The function is expected to return an INTEGER.
*The function accepts INTEGER_ARRAY ar as parameter.
*/
int simpleArraySum(vector<int> ar)
{
int sum = 0;
for (int i = 0; i < ar.size(); i++)
{
sum += ar[i];
}
return sum;
}
int main()
{
ofstream fout(getenv("OUTPUT_PATH"));
string ar_count_temp;
getline(cin, ar_count_temp);
int ar_count = stoi(ltrim(rtrim(ar_count_temp)));
string ar_temp_temp;
getline(cin, ar_temp_temp);
vector<string> ar_temp = split(rtrim(ar_temp_temp));
vector<int> ar(ar_count);
for (int i = 0; i < ar_count; i++)
{
int ar_item = stoi(ar_temp[i]);
ar[i] = ar_item;
}
int result = simpleArraySum(ar);
fout << result << "\n";
fout.close();
return 0;
}
string ltrim(const string &str)
{
string s(str);
s.erase( s.begin(),
find_if(s.begin(), s.end(), not1(ptr_fun<int, int> (isspace)))
);
return s;
}
string rtrim(const string &str)
{
string s(str);
s.erase( find_if(s.rbegin(), s.rend(), not1(ptr_fun<int, int> (isspace))).base(),
s.end()
);
return s;
}
vector<string> split(const string &str)
{
vector<string> tokens;
string::size_type start = 0;
string::size_type end = 0;
while ((end = str.find(" ", start)) != string::npos)
{
tokens.push_back(str.substr(start, end - start));
start = end + 1;
}
tokens.push_back(str.substr(start));
return tokens;
}
Simple Array Sum Hackerrank Solution Output
Similar to Simple Array Sum
- StringStream Hackerrank Solution in C++
- Attribute Parser Hackerrank Solution in C++
- Basic Data Types HackerRank Solution in C++
- For Loop Hackerrank Solution in C++
- Functions in C++ Hackerrank Solution
- Pointer Hackerrank Solution in C++
- Arrays Introduction Hackerrank Solution in C++
- Strings Hackerrank Solution in C++
- Plus Minus Hackerrank Solution C++
- Hello World in C Hackerrank Solution
- Divisible Sum Pairs Hackerrank Solution in C++
- Apple And Orange Hackerrank Solution
0 Comments: