THINK FIRST·CODE LATER

← All labs

Two weeks of temperatures

Problem

Write TempStats. Read n, then n daily temperatures into a double[]. Print:

Average: 18.4
Days above average (3): 3 4 5
Longest warming streak: 5 days (day 1 to day 5)
Biggest change: 6.5 degrees (day 5 to day 6)
  • Days are numbered from 1 for the user, but the array starts at index 0.
  • A warming streak is a run of days where each day is warmer than the day before (a single day is a streak of 1). Report the first longest one.
  • The biggest change compares each day with the previous one (use Math.abs).

Data for this lab

Your program reads its data from standard input. Download the file, then give it to your program as shown below. Start with data set 1: its expected output is given.

l9-4-data-1.txt

7 15.5 17 21 22.5 23 16.5 13.3
What your program must print for l9-4-data-1.txt
Average: 18.4
Days above average (3): 3 4 5
Longest warming streak: 5 days (day 1 to day 5)
Biggest change: 6.5 degrees (day 5 to day 6)

Run your program first, then open this and compare line by line: your output must contain these lines exactly.

How to give the file to my program

Terminal (macOS, Linux, or Windows Command Prompt), after compiling:

java Main < l9-4-data-1.txt

Windows PowerShell does not understand <:

Get-Content l9-4-data-1.txt | java Main

IntelliJ IDEA: Run → Edit Configurations → Redirect input from. Or simply paste the content of the file into the console when the program waits for input.

Write it here or in your IDE, then paste it. Compile and test it yourself before comparing. Your code stays in your browser — it is never sent to or stored on the server.