/* This program will input a list of numbers from the user to store in an array and then use methods to perform various * computations on the array's values such as finding the minimum, the maximum, the difference between max and min, the sum, the * average, the standard deviation and the index of the first occurrence of the minimum and the last occurrence of * the maximum. This program is primarily set up to demonstrate both arrays and methods where some methods will be * called from multiple places in the code. */ import java.util.Scanner; public class ArrayMadness { public static void main(String[] args) { Scanner in = new Scanner(System.in); int[] values = new int[100]; int number = getValues(in, values); // this method will input the array and return the size of the array int max = findMax(values, number); // obtain the maximum value int min = findMin(values, number); // obtain the minimum value System.out.println(); System.out.println("The maximum value is " + max); System.out.println("The minimum value is " + min); // note we could just compute max - min but I wanted to show modularity by having findDiff call the findMax and findMin methods System.out.println("The difference between max and min is " + findDiff(values, number)); System.out.println("The sum of the values is " + findSum(values, number)); System.out.println("The average value is " + findAvg(values, number)); System.out.println("The standard deviation is " + findSTD(values, number)); System.out.println("The first instance of " + min + " is " + findFirst(values, number, min)); System.out.println("The last instance of " + max + " is " + findLast(values, number, max)); } // This method receives a Scanner and an empty array that can hold up to 100 elements and inputs elements from the user // The method uses a sentinnel loop iterating while the input >= 0 or until the array is filled // The array is automatically returned via the parameter list because it is an object, the method returns the number of // elements input so that main can know how many elements are in the array to control later for loops public static int getValues(Scanner in, int[] array) { int number = 0; int temp; System.out.print("Enter positive values, negative to quit, no more than 100 total values: "); temp = in.nextInt(); while(temp>=0&&number<100) { array[number]=temp; number++; System.out.print("Enter the next positive value, negative to quit, no more than 100 total values: "); temp = in.nextInt(); } return number; } // if there are values in the array, find and return the maximum otherwise return an "error code" value of -1 public static int findMax(int[] values, int number) { if(number>0) { int max=values[0]; // assume values[0] is the largest for(int i=1;imax) max=values[i]; // value > max, remember it as the new max return max; } else return -1; } // if there are values in the array, find and return the minimum otherwise return an "error code" value of -1 public static int findMin(int[] values, int number) { if(number>0) { int min=values[0]; // assume values[0] is the minimum for(int i=1;i0) { for(int i=0;i0) return (double)sum/number; else return -1.0; } // if the array has elements, call findAvg to obtain the average and use it to compute and return the standard deviation // otherwise return -1 // STD: sum of each (values[i] - average)^2, take result, divide by N and take the sqrt public static double findSTD(int[] values, int number) { double avg = findAvg(values, number); double sum = 0.0; if(avg!=-1.0) { for(int i=0;i=0;i--) if(values[i]==target) return i; return -1; } }