-
Code long add_arr(const unsigned long len, const int arr[len]) { long result = 0; for ( unsigned long i = 0; i < len; result += arr[i++] ); return result; }
Test Cases long add_arr(const unsigned long len, const int[len]); 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); }
Output:
-
Code function addArr(arr){- long
- add_arr(const unsigned long len, const int arr[len])
- {
- long result = 0;
if(arr.length === 0) return null- for
- (
- unsigned long i = 0;
- i < len;
- result += arr[i++]
- );
let final = 0arr.forEach(num => {final += num})return final- return result;
- }
Test Cases - #include<criterion/criterion.h>
const chai = require("chai");const assert = chai.assert;- long add_arr(const unsigned long len, const int[len]);
describe("Solution", function() {it("should test for something", function() {assert.strictEqual(addArr([1, 2, 3, 4, 5]), 15);assert.strictEqual(addArr([1, 100]), 101)assert.strictEqual(addArr([]), null)});});- 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);
- }
- 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 }}