Sum of array
Medium
2
5.4% Acceptance
The objective is to create a Solidity function named sumArray
which will take two arguments and return the sum of all elements in an array.
Function Signature
function sumArray(int[] memory arr, uint length) public returns (int) { // ... }
Arguments
The sumArray
function receives the following arguments:
arr
- A dynamic array of integers.length
- The length of the array.
Return Value
The sumArray
function will return the sum of all elements in the array.
Examples
Here is how the function should behave:
- If input array is
[4, 2, 3, 4]
,sumArray
returns13
, because4+2+3+4 = 13
. - If input array is
[1, 2, 3]
,sumArray
returns6
, because1+2+3 = 6
.
NOTE: The function will be declared as
public