Declare an array of type double values of size 52 to store the average weekly te
ID: 3622413 • Letter: D
Question
Declare an array of type double values of size 52 to store the average weekly temperature for one year. Using this data structure which you should assume someone has filled up with valid values, write code to do the following items. Write only code segments, not compete programs.a.) display the temperature for the hottest and coldest weeks of the year.
b.) display the average weekly temperature for the whole year.
c.) display the largest change in average temperature from one week to the next (positive or negative).
Explanation / Answer
// Declare an array of type double values of size 52 to store the average weekly temperature for one year.
double[] temperatures = new double[52];
// a.) display the temperature for the hottest and coldest weeks of the year.
double hottest = Double.MIN_VALUE;
double coldest = Double.MAX_VALUE;
for(double t : temperatures)
{
if(t > hottest)
hottest = t;
else if(t < coldest)
coldest = t;
}
// b.) display the average weekly temperature for the whole year.
double average = 0.0;
for(double t : temperatures)
average += t;
average /= temperatures.length;
// c.) display the largest change in average temperature from one week to the next (positive or negative).
double change = 0.0;
for(int i = 1; i < temperatures.length; i++)
if(Math.abs(temperatures[i] - temperatures[i-1]) > Math.abs(change))
change = temperatures[i] - temperatures[i-1];
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.