-
Code int64_t add_arr(const std::vector<int32_t> &arr) { return std::reduce(arr.cbegin(), arr.cend(), 0); }
Test Cases int64_t add_arr(const std::vector<int32_t> &arr); Test(add_arr, all) { std::vector<int> arr = {1, 2, 3, 4, 5, 6, 7, 8, 9}; cr_assert_eq(add_arr(arr), 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9); std::vector<int> arr2 = {1, -2, 3, 4, 5, 6, 7, 8, 9}; cr_assert_eq(add_arr(arr2), 1 + (-2) + 3 + 4 + 5 + 6 + 7 + 8 + 9); }
Output:
-
Code #include<bits/stdc++.h>using namespace std;- #include<numeric>
- #include<stdint.h>
- #include<vector>
long add_arr(vector<int> arr){long sum = 0;for(int i = 0; i < arr.size(); i++) {sum += arr[i];}return sum;- int64_t add_arr(const std::vector<int32_t> &arr) {
- return std::reduce(arr.cbegin(), arr.cend(), 0);
- }
Test Cases - #include <vector>
- #include <criterion/criterion.h>
long add_arr(std::vector<int> arr);- int64_t add_arr(const std::vector<int32_t> &arr);
- Test(add_arr, all)
- {
- std::vector<int> arr = {1, 2, 3, 4, 5, 6, 7, 8, 9};
- cr_assert_eq(add_arr(arr), 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9);
- std::vector<int> arr2 = {1, -2, 3, 4, 5, 6, 7, 8, 9};
- cr_assert_eq(add_arr(arr2), 1 + (-2) + 3 + 4 + 5 + 6 + 7 + 8 + 9);
- }
- All
- {{group.name}} ({{group.count}})
This comment has been reported as {{ abuseKindText }}.
Show
This comment has been hidden. You can view it now .
This comment can not be viewed.
- |
- Reply
- Edit
- View Solution
- Expand 1 Reply Expand {{ comments?.length }} replies
- Collapse
- Remove
- Remove comment & replies
- Report
{{ fetchSolutionsError }}