/************************************************************ C151 Spring 2021 Dana Vrajitoru SumArray.java A program that inputs a list of real numbers, stores it in an array, and outputs the sum of these numbers. *************************************************************/ // package sumar; import java.util.Scanner; public class SumArray { static int size; static float [] numbers; static Scanner scan = new Scanner(System.in); // A function that assign 0.0 to each element of the array. static void initArray(int n) { numbers = new float[n]; size = n; for (int i = 0; i < size; i++) numbers[i] = 0.0f; } // A function that inputs the elements of an array . static void inputArray() { try { System.out.println("Enter the number of elements in the array"); size = scan.nextInt(); initArray(size); for (int i = 0; i < size; i++) { System.out.println("Enter the element number " + i); numbers[i] = scan.nextFloat(); } } catch (Exception e) { System.out.println("An error happened"); } } // A function that computes the sum of the elements of an array. static float computeSum() { float sum = 0; for (int i = 0; i < size; i++) sum = sum + numbers[i]; return sum; } public static void main(String[] args) { // Input the numbers inputArray(); // Compute and output the sum System.out.println("The sum of these numbers is " + computeSum()); } } /******************* Example of program output ************* Enter the number of elements in the array 5 Enter the element number 0 2.15 Enter the element number 1 3.14 Enter the element number 2 60 Enter the element number 3 19.5 Enter the element number 4 2.41 The sum of these numbers is 87.2 *************************************************************/