Implement the method sumArray
that calculates and returns the sum of all integers in an input array.
Learner Level
Loop Array
Implement a method that takes an array of integers as input and returns the sum of all its elements. The method should iterate through the array and calculate the cumulative total of the values.
To calculate the sum of all integers in an array, follow these steps:
-
Initialize a Total Variable
Start with a variable (e.g.,total
) set to0
to hold the sum. -
Iterate Through the Array
Loop over each element in the input array. -
Add Each Element to the Total
Add the current number to thetotal
. -
Return the Total
After the loop, return the final value oftotal
.
Pseudocode:
Function sumArray(nums, size):
total ← 0
For each element in nums, up to size:
Add element to total
Return total