In: Computer Science
IN JAVA
Write a program that uses a two-dimensional array to store the highest and lowest temperatures for each month of the year. Prompt the user for 12 months of highest and lowest. Write two methods : one to calculate and return the average high and one to calculate and return the average low of the year. Your program should output all the values in the array and then output the average high and the average low.
im trying to figure out how to get a 2D array to store a number for the highest and the lowest of each month. I can get it to do either the highest or lowest but not both.
Thanks
import java.util.Scanner;
class ArrayAssignmentClient {
public static void main(String[]args){
int i,j;
double [][] arr = new double[12][2];
Scanner scan = new Scanner(System.in);
System.out.print("Enter lowest and highest temparatures of 12
months : ");
for(i=0; i<12; i++)
{
for(j=0; j<2; j++)
{
arr[i][j] = scan.nextDouble();
}
}
System.out.print("The lowest and highest temparatures of 12months
are :\n");
for(i=0; i<12; i++)
{
for(j=0; j<2; j++)
{
System.out.print(arr[i][j]+ " ");
}
System.out.println();
}
System.out.println(averageHigh(arr));
System.out.println(averageLow(arr));
}
public static double averageHigh(double[][]arr){
double total = 0;
double average = 0;
for (int i = 0; i <12; i++) {
total += arr[i][1];
average = total /12;
}
return average;
}
public static double averageLow(double[][]arr){
double total = 0;
double average = 0;
for (int i = 0; i <12; i++) {
total += arr[i][0];
average = total / 12;
}
return average;
}
}