Question
5.4.3 : Print the sum and average of an array's elements. Modify the program to print the average as well as the sum. Hint: You
5.4.3 : Print the sum and average of an array's elements.
Modify the program to print the average as well as the sum. Hint: You don't actually have to change the loop, but rather change what you print.
import java.util.Scanner;
public class ArraySum { public static void main(String[] args) { Scanner scnr = new Scanner(System.in); final int NUM_ELEMENTS = 8; // Number of elements int[] userVals = new int[NUM_ELEMENTS]; // User numbers int i = 0; // Loop index int sumVal = 0; // For computing sum
// Prompt user to populate array System.out.println("Enter " + NUM_ELEMENTS + " integer values..."); for (i = 0; i
// Determine sum sumVal = 0; for (i = 0; i
return; } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started