-
Code template <std::size_t L> long add_arr(std::array<int, L> arr) { long result = 0; for (int i : arr) result += i; return result; } template long add_arr(std::array<int, 9> arr);
Test Cases template <std::size_t L> long add_arr(std::array<int, L> arr); Test(add_arr, all) { std::array<int, 9> arr = {1, 2, 3, 4, 5, 6, 7, 8, 9}; cr_assert_eq(add_arr<9>(arr), 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9); std::array<int, 9> arr2 = {1, -2, 3, 4, 5, 6, 7, 8, 9}; cr_assert_eq(add_arr<9>(arr2), 1 + (-2) + 3 + 4 + 5 + 6 + 7 + 8 + 9); }
Output:
-
Code - #include <array>
- template <std::size_t L>
- long
add_arr(const unsigned long len, const int arr[len])- add_arr(std::array<int, L> arr)
- {
- long result = 0;
for(unsigned long i = 0;i < len;result += arr[i++]);- for (int i : arr) result += i;
- return result;
}- }
- template long add_arr(std::array<int, 9> arr);
Test Cases #include<criterion/criterion.h>- #include <array>
- #include <criterion/criterion.h>
long add_arr(const unsigned long len, const int[len]);- template <std::size_t L>
- long add_arr(std::array<int, L> arr);
Test (add_arr, all) {int arr[] = {1,2,3,4,5,6,7,8,9};cr_assert_eq(add_arr(9, arr), 1+2+3+4+5+6+7+8+9);int arr2[] = {1,-2,3,4,5,6,7,8,9};cr_assert_eq(add_arr(9, arr2), 1+(-2)+3+4+5+6+7+8+9);- Test(add_arr, all)
- {
- std::array<int, 9> arr = {1, 2, 3, 4, 5, 6, 7, 8, 9};
- cr_assert_eq(add_arr<9>(arr), 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9);
- std::array<int, 9> arr2 = {1, -2, 3, 4, 5, 6, 7, 8, 9};
- cr_assert_eq(add_arr<9>(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 }}