Learner Level

Implement the method sumArray that calculates and returns the sum of all integers in an input 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
Returns (int) 15

Example 2

Input
nums (int[]) = [10, -5, 5]
size (int) = 3
Returns (int) 10

Example 3

Input
nums (int[]) = [-1, -2, -3]
size (int) = 3
Returns (int) -6

Example 4

Input
nums (int[]) = [100]
size (int) = 1
Returns (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
Ready to test your solutionRun Code checks your solution against the test cases.Shift + Enter to run

Notifications

Updates are enabled for this browser.

Receive new tutorials, articles and learning resources. You can turn updates off anytime.

Test browser code

Use this code in admin to send a test notification to this browser.