Write a Program that uses a two-dimensional array to store the highest and lowes
ID: 3680539 • Letter: W
Question
Write a Program that uses a two-dimensional array to store the highest and lowest temperatures for each month of the year. The program should output the average high, average low, and highest and lowest temperatures of the year. Your program must consist of the following methods: Method getData: This method reads and stores the data in the two-dimensional array. Method averageHigh: This method calculates and returns the average high temperature of the year. Method averageLow: This method calculates and returns the average low temperature of the year. Method indexHighTemp: This method returns the index of the highest temperature in the array. Method indexLowTemp: This method returns the index of the highest temperature in the array (These array. methods must all have appropriate parameters.)Explanation / Answer
Hi i have written the code for your reference,
public class ArrayAssignmentClient {
public static void main(String[] args) {
System.out.println("The average high of the year is: " + averageHigh(getData()));
System.out.println("The average low of the year is: " + averageLow(getData()));
System.out.println("The highest temperature is: " + indexHighTemp(getData()));
System.out.println("The lowest temperature is: " + indexLowTemp(getData()));
}
public static int[][] getData() {
// i - row j - collum
int[][] data = {{40, 44, 53, 64, 74, 83, 87, 85, 78, 67, 56, 45}, {26, 28, 34, 44, 54, 64, 69, 68, 60, 48, 39, 30}};
return data;
}
public static int averageHigh(int[][] data) { // average high = 64.667, rounds down as an int
int total = 0;
int average = 0;
for (int i = 0; i < data[0].length; i++)
{
total += data[0][i];
average = total / data[0].length;
}
return average;
}
public static int averageLow(int[][] data) { //average low = 46
int total = 0;
int average = 0;
for (int i = 0; i < data[1].length; i++)
{
total += data[1][i];
average = total / data[1].length;
}
return average;
}
public static int indexHighTemp(int[][] data) {
int tempVal;
int maxIndex = 0;
for (int i = 0; i < data[0].length; i++)
{
tempVal = data[0][i];
if (tempVal > maxIndex)
{
maxIndex = data[0][i];
} }
return maxIndex;
} public static int indexLowTemp(int[][] data) {
int tempVal;
int lowIndex = Integer.MAX_VALUE;
for (int i = 0; i < data[1].length; i++)
{
tempVal = data[1][i];
if (lowIndex > tempVal)
{
lowIndex = data[1][i];
} }
return lowIndex;
} }
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.