In: Computer Science
// JavaLanguage
. You need to write a program that asks the user for an array size, and then asks the user to enter that many integers. Your program will then print out the sum , average, largest and smallest of the values in the array.
//SumAverageMinMaxArray.java import java.util.Random; import java.util.Scanner; public class SumAverageMinMaxArray { public static int getLowest(int[] numbers){ int min = Integer.MAX_VALUE; for(int i = 0;i<numbers.length;i++){ if(min > numbers[i]){ min = numbers[i]; } } return min; } public static int getHighest(int[] numbers){ int max = Integer.MIN_VALUE; for(int i = 0;i<numbers.length;i++){ if(max < numbers[i]){ max = numbers[i]; } } return max; } public static int getTotal(int[] iArray){ int sum = 0; for(int i = 0;i<iArray.length;i++){ if((iArray[i] & 2) > 0 || ((iArray[i] & 4) > 0) || ((iArray[i] & 8) > 0)){ sum += iArray[i]; } } return sum; } public static double getAverage(int arr[]){ return 1.0 * getTotal(arr)/arr.length; } public static void main(String args[]){ Scanner scan = new Scanner(System.in); int size; System.out.print("Enter size of array: "); size = scan.nextInt(); int arr[] = new int[size]; System.out.println("Enter "+size+" integers for array"); for(int i = 0;i<size;i++){ arr[i] = scan.nextInt(); } System.out.println(); System.out.println("Sum = " + getTotal(arr)); System.out.println("Average = " + getAverage(arr)); System.out.println("Minimum = " + getLowest(arr)); System.out.println("Maximum = " + getHighest(arr)); } }