Sum of all Array Elements Program

Sum of all Array Elements Program Problem Statment:

Given an Array of size n, Find the sum of all the array elements present in the array.

For example consider an array: arr[]={1,2,3,4,5} in that case array sum must be 1+2+3+4+5 which totals 15.

Use the 6-step strategy to solve any problem

  1. Understand the problem
  2. Design test data/test cases (i.e. input and expected output)
  3. Derive the solution (write pseudocode for the solution)
  4. Test the solution (do a dry run of the pseudo-code for the test data and confirm it works)
  5. Write the program/code (using Java here)
  6. Test the code (debug any syntax errors, runtime errors, and logical errors).

Sum of all Array Elements Program | Using 6 Steps Strategies

Step 1: Understand the problem

You want to create a program that calculates the sum of an array of n numbers.

Step 2: Design test data/test cases

Before we write the program, let’s define some test cases:

Test Case 1:

Input: [1, 2, 3, 4, 5]

Expected Output: 15

Test Case 2:

Input: [10, 20, 30, 40, 50]

Expected Output: 150

Test Case 3:

Input: [0, 0, 0, 0, 0]

Expected Output: 0

Step 3: Derive the solution (write pseudocode for the solution)

Here’s a simple pseudocode for calculating the sum of an array:

1. Initialize a variable `sum` to 0.
2. Loop through each element `num` in the array.
   - Add `num` to `sum`.
3. After the loop, `sum` will contain the total sum of the array.
4. Return `sum`.

Step 4: Test the solution (do a dry run of the pseudo-code for the test data and confirm it works)

Let’s do a dry run of the pseudocode for Test Case 1:

Input: [1, 2, 3, 4, 5]

sum = 0

Loop through each element `num` in the array:
   num = 1
   sum = 0 + 1 = 1
   num = 2
   sum = 1 + 2 = 3
   num = 3
   sum = 3 + 3 = 6
   num = 4
   sum = 6 + 4 = 10
   num = 5
   sum = 10 + 5 = 15

Return sum (which is 15)

It works as expected.

Step 5: Write the program/code (using Java here)

Sum of all Array Elements Program

C
#include <stdio.h>

int calculateSum(int arr[], int n) {
    int sum = 0;
    for (int i = 0; i < n; i++) {
        sum += arr[i];
    }
    return sum;
}

int main() {
    int arr1[] = {1, 2, 3, 4, 5};
    int arr2[] = {10, 20, 30, 40, 50};
    int arr3[] = {0, 0, 0, 0, 0};
    int n = 5;

    int sum1 = calculateSum(arr1, n);
    int sum2 = calculateSum(arr2, n);
    int sum3 = calculateSum(arr3, n);

    printf("Sum of arr1: %d\n", sum1); // Expected output: 15
    printf("Sum of arr2: %d\n", sum2); // Expected output: 150
    printf("Sum of arr3: %d\n", sum3); // Expected output: 0

    return 0;
}
C

C++
#include <iostream>

int calculateSum(int arr[], int n) {
    int sum = 0;
    for (int i = 0; i < n; i++) {
        sum += arr[i];
    }
    return sum;
}

int main() {
    int arr1[] = {1, 2, 3, 4, 5};
    int arr2[] = {10, 20, 30, 40, 50};
    int arr3[] = {0, 0, 0, 0, 0};
    int n = 5;

    int sum1 = calculateSum(arr1, n);
    int sum2 = calculateSum(arr2, n);
    int sum3 = calculateSum(arr3, n);

    std::cout << "Sum of arr1: " << sum1 << std::endl; // Expected output: 15
    std::cout << "Sum of arr2: " << sum2 << std::endl; // Expected output: 150
    std::cout << "Sum of arr3: " << sum3 << std::endl; // Expected output: 0

    return 0;
}
C++

Java
public class ArraySumCalculator {
    public static int calculateSum(int[] arr) {
        int sum = 0;
        for (int num : arr) {
            sum += num;
        }
        return sum;
    }

    public static void main(String[] args) {
        int[] arr1 = {1, 2, 3, 4, 5};
        int[] arr2 = {10, 20, 30, 40, 50};
        int[] arr3 = {0, 0, 0, 0, 0};

        int sum1 = calculateSum(arr1);
        int sum2 = calculateSum(arr2);
        int sum3 = calculateSum(arr3);

        System.out.println("Sum of arr1: " + sum1); // Expected output: 15
        System.out.println("Sum of arr2: " + sum2); // Expected output: 150
        System.out.println("Sum of arr3: " + sum3); // Expected output: 0
    }
}
Java

JavaScript
function calculateSum(arr) {
    let sum = 0;
    for (let num of arr) {
        sum += num;
    }
    return sum;
}

let arr1 = [1, 2, 3, 4, 5];
let arr2 = [10, 20, 30, 40, 50];
let arr3 = [0, 0, 0, 0, 0];

let sum1 = calculateSum(arr1);
let sum2 = calculateSum(arr2);
let sum3 = calculateSum(arr3);

console.log("Sum of arr1: " + sum1); // Expected output: 15
console.log("Sum of arr2: " + sum2); // Expected output: 150
console.log("Sum of arr3: " + sum3); // Expected output: 0
JavaScript

Python
def calculateSum(arr):
    sum = 0
    for num in arr:
        sum += num
    return sum

arr1 = [1, 2, 3, 4, 5]
arr2 = [10, 20, 30, 40, 50]
arr3 = [0, 0, 0, 0, 0]

sum1 = calculateSum(arr1)
sum2 = calculateSum(arr2)
sum3 = calculateSum(arr3)

print("Sum of arr1:", sum1) # Expected output: 15
print("Sum of arr2:", sum2) # Expected output: 150
print("Sum of arr3:", sum3) # Expected output: 0
Python

Step 6: Test the code

Compile and run the code to verify that it works as expected. If there are any syntax errors, runtime errors, or logical errors, debug and correct them.

Once you’ve completed these steps, you should have a Java program that calculates the sum of an array of n numbers.

Execute Code Here

Sum of all Array Elements Program | Conclusion

In this article, we have explored a systematic approach to solving problems, specifically focusing on how to calculate the sum of an array of numbers using Java. By following the 6-step strategy outlined below, you can tackle a wide range of programming challenges effectively and efficiently.

By following this structured approach, you not only improve your problem-solving skills but also increase the likelihood of producing correct and efficient code. Remember that problem-solving is a skill that can be honed with practice, and the 6-step strategy provides a valuable framework for achieving success in programming and software development.

Leave a Comment