82.7k views
5 votes
Write a program that uses a 2-D 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 with their appropriate parameters:

1 Answer

4 votes

Answer:

*/

import java.util.*;

public class Main{

public static void main(String[] args){

int infoArray[][]=getData();

int avgArray[][]=average(infoArray);

int tempArray[][]=temps(infoArray);

System.out.println("Data set - Highs: 87,99,100,102,101,97,97,93,99,94,98,92.");

System.out.println("Lows: 11,12,19,24,23,25,32,33,29,21,10,1.");

System.out.println("Average High: "+avgArray[0][0]);

System.out.println("Average Low: "+avgArray[1][0]);

System.out.println("Highest Temperature: "+tempArray[0][0]);

System.out.println("Lowest Temperature: "+tempArray[1][0]);

}

public static int[][] getData(){

int tempDatahl[][] = {{87,99,100,102,101,97,97,93,99,94,98,92},{11,12,19,24,23,25,32,33,29,21,10,1}};

return tempDatahl;

}

public static int[][] average(int x[][]){

int avgH = 0;

int avgL = 0;

//loop to find avg high

for(int i=0;i<x[0].length;i++){

avgH+=x[0][i] ;

}

//loop to find avg low

for(int h=0;h<x[1].length;h++){

avgL+=x[1][h] ;

}

avgH=avgH/x[0].length;

avgL=avgL/x[1].length;

int avgData[][] = {{avgH},{avgL}};

return avgData;

}

public static int[][] temps(int x[][]){

//high temp

int ht = 0;

int lt = 100;

for(int t=0;t<x[0].length;t++){

if(x[0][t]>ht){

ht = x[0][t];

}

}

//low temp

for(int y=0;y<x[1].length;y++){

if(x[1][y]<lt){

lt = x[1][y];

}

}

int arrTemps[][]={{ht},{lt}};

return arrTemps;

}

}

Step-by-step explanation:

User Jst
by
8.8k points

No related questions found