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.
Example 1
Input:
nums (int[]) = [1, 2, 3, 4, 5]
size (int) = 5
Return:
(int) 15
Example 2
Input:
nums (int[]) = [10, -5, 5]
size (int) = 3
Return:
(int) 10
Example 3
Input:
nums (int[]) = [-1, -2, -3]
size (int) = 3
Return:
(int) -6
Example 4
Input:
nums (int[]) = [100]
size (int) = 1
Return:
(int) 100

To calculate the sum of all integers in an array, follow these steps:

  1. Initialize a Total Variable
    Start with a variable (e.g., total) set to 0 to hold the sum.

  2. Iterate Through the Array
    Loop over each element in the input array.

  3. Add Each Element to the Total
    Add the current number to the total.

  4. Return the Total
    After the loop, return the final value of total.

Pseudocode:

Function sumArray(nums, size):
    total ← 0
    For each element in nums, up to size:
        Add element to total
    Return total
Run your code to see the result.