IN JAVA: The weather array is a two dimensional array that has 2 rows for each m
ID: 3856778 • Letter: I
Question
IN JAVA:
The weather array is a two dimensional array that has 2 rows for each month. The first row for a month has all the low temperatures by day as columns. The second row has all high temperatures of the day as columns. In the sample data below January 2ndhad a low of 24 and a high of 53
int[][] weather = { {23,24,24,29,19,20…. }, // low temps for Jan
{43,53,39,42,32,44…. }, // high temps for Jan
… // many more lines of data go here
};
Write code to be inserted into the Main method to output, via System.out.printlin, the following three values:
- The day that had the coldest temperature expressed as month and day….4 – 3 (for March 3) . Do not convert month number to its month name (March), just output month number.
- The month with the highest average high temperature.
- The month with the largest difference between the average high and the average low.
Explanation / Answer
Given below is the code for the question. I have just initialized the weather array with dummy data. But the real array will have 24 rows for the 12 months with each row having 30 or 31 elements corresponding to the number of days in that month. Each month has 2 rows - one for low temperatures and the other for high temperatures.
Please rate the answer if it helped. Thank you.
public class Weather {
//method to return the index of the lowest i.e. minimum value in the specified row of data
private static int getRowMinIdx(int data[][], int row)
{
int minIdx = 0; //assume the 1st element in the row is min
//now iterate over the rest of the elements to find out the index of minimum
for(int i = 1; i < data[row].length; i++)
{
if(data[row][i] < data[row][minIdx])
minIdx = i;
}
return minIdx;
}
//method to calculate the average of values in the specifed row in data
private static double getRowAvg(int data[][], int row)
{
double avg = 0;
for(int i = 0 ;i < data[row].length; i ++)
{
avg += data[row][i];
}
//now divide by number of values to get avg
avg /= data[row].length;
return avg;
}
public static void main(String[] args) {
//just a dummy data, to replace by real data of low and high temperaturees for
//different months
int weather[][]={{2,3},
{3,4},
{5,6},
{7,9}};
displayColdestDay(weather);
displayHighestAvgTemp(weather);
displayLargestAvgDiff(weather);
}
private static void displayColdestDay(int weather[][])
{
//get the index of lowest value value for jan and store it as coldest day of month jan
int coldestMonth = 0, coldestDay = getRowMinIdx(weather, 0);
int idx;
//now iterate over rest of months and compare and save the coldest day
for(int month = 1; month < 12; month++)
{
idx = getRowMinIdx(weather, 2 * month ); //the low tempeatures for months are in rows 0, 2, 4, 6 etc for months 0, 1, 2,3 ....
if(weather[2 * month][idx] < weather[2 * coldestMonth ][coldestDay])
{
coldestMonth = month;
coldestDay = idx;
}
}
//add 1 to both month and day since they are 0 based numbers i.e 0 for jan, 1 for feb
System.out.println("The coldest day was on Month: " + (coldestMonth+1) + " Day: " + (coldestDay + 1));
}
private static void displayHighestAvgTemp(int weather[][])
{
double highestAvgMonthIdx = 0, highestAvg = getRowAvg(weather, 1); //get avg of high temperatures of jan
//high tempearatures of months 0 (jan), 1 (feb), 2, 3 etc are in rows 1, 3, 5, 7 etc
//so the formula to get corresponding high temperature row will be 2*month + 1
//iterate months feb to dec
for(int month = 1; month < 12; month++)
{
double avg = getRowAvg(weather, 2 * month + 1);
if(avg > highestAvg)
{
highestAvg = avg;
highestAvgMonthIdx = month;
}
}
System.out.println("The month " + (highestAvgMonthIdx + 1) + " has the highest average high temperature of " + highestAvg);
}
private static void displayLargestAvgDiff(int weather[][])
{
double highestDiffMonthIdx = 0;
double highestDiff = getRowAvg(weather, 1) - getRowAvg(weather, 0); //get difference of avg high and low for jans
//low tempearatures of months 0 (jan), 1 (feb), 2, 3 etc are in rows 0, 2, 4, 6 etc
//so the formula to get corresponding low temperature row will be 2*month
//high tempearatures of months 0 (jan), 1 (feb), 2, 3 etc are in rows 1, 3, 5, 7 etc
//so the formula to get corresponding high temperature row will be 2*month + 1
//iterate months feb to dec
for(int month = 1; month < 12; month++)
{
double diff = getRowAvg(weather, 2 * month + 1) - getRowAvg(weather, 2 * month);
if(diff > highestDiff)
{
highestDiff = diff;
highestDiffMonthIdx = month;
}
}
System.out.println("The month " + (highestDiffMonthIdx + 1) + " has the largest difference in " +
"average high and average low temperature and the difference is " + highestDiff);
}
}