/* Marty Stepp, CSE 142, Autumn 2009 Reads temperatures from the user, computes average and # days above average. The purpose of the program is to demonstrate basic usage of arrays. Example output: How many days' temperatures? 7 Day 1's high temp: 45 Day 2's high temp: 44 Day 3's high temp: 39 Day 4's high temp: 48 Day 5's high temp: 37 Day 6's high temp: 46 Day 7's high temp: 53 Average temp = 44.6 4 days were above average. Two coldest days: 37, 39 */ import java.util.*; // for Scanner public class Weather { public static void main(String[] args) { Scanner console = new Scanner(System.in); System.out.print("How many days' temperatures? "); int days = console.nextInt(); // read each day's temperature and store it into the array int[] weather = new int[days]; int sum = 0; for (int i = 0; i < weather.length; i++) { System.out.print("Day " + (i + 1) + "'s high temp: "); weather[i] = console.nextInt(); sum += weather[i]; } double average = (double) sum / days; System.out.println("Average temp = " + average); // compute how many days were above average int aboveAverage = 0; for (int i = 0; i < weather.length; i++) { if (weather[i] > average) { aboveAverage++; } } System.out.println(aboveAverage + " days were above average."); // figure out the two coldest days by sorting the array // (the smallest two values will be at the front now) Arrays.sort(weather); System.out.println("Two coldest days: " + weather[0] + ", " + weather[1]); } }