/* CSE 142, Autumn 2007, Marty Stepp This program reads several days' high temperatures from the user and computes the average temperature as well as how many days were above that average temperature. 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.57142857142857 4 days above average */ import java.util.*; 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 all the temperatures into an array, // and also compute their cumulative sum int[] temperatures = new int[days]; double sum = 0.0; for (int i = 0; i < temperatures.length; i++) { System.out.print("Day " + (i + 1) + "'s high temp: "); int temp = console.nextInt(); temperatures[i] = temp; sum += temp; } double average = sum / days; // 44.57 System.out.println("Average temp = " + average); // compute how many days were above average // {45, 44, 39, 48, 37, 46, 53} int count = 0; for (int i = 0; i < temperatures.length; i++) { if (temperatures[i] > average) { count++; } } System.out.println(count + " days above average"); } }