In: Computer Science
So I have written a code for it but i just have a problem with the output. For the month with the highest temperature and lowest temperature, my code starts at 0 instead of 1. For example if I input that month 1 had a high of 20 and low of -10, and every other month had much warmer weather than that, it should say "The month with the lowest temperature is 1" but instead it says "The month with the lowest temperature is 0" So i just want to know what i have to change to do that. The code below is the one i have written
import java.util.Scanner;
public class Lab2 {
public static int[][] getData() {
Scanner sc = new Scanner(System.in);
int num;
System.out.println("Enter The Number of Months: ");
num = sc.nextInt();
int[][] temps = new int[2][num];
for(int i=0; i < num; i++) {
System.out.println("Enter The High Temperature For Month " + (i+1) + " : ");
temps[0][i] = sc.nextInt();
System.out.println("Enter The Low Temperature For Month " + (i+1) + " : ");
temps[1][i] = sc.nextInt();
}
return temps;
}
public static double averageHigh(int[][] temps) {
double sum = 0;
for(int i=0; i < temps[0].length; i++) {
sum += temps[0][i];
}
return sum / (double)(temps[0].length);
}
public static double averageLow(int[][] temps) {
double sum = 0;
for(int i=0; i < temps[1].length; i++) {
sum += temps[1][i];
}
return sum / (double)(temps[1].length);
}
public static int indexHighTemp(int[][] temps) {
int index = 0;
int high = temps[0][0];
for(int i=0; i < temps[0].length; i++) {
if (high < temps[0][i]) {
high = temps[0][i];
index = i;
}
}
return index;
}
public static int indexLowTemp(int[][] temps) {
int index = 0;
int low = temps[1][0];
for(int i=0; i < temps[1].length; i++) {
if (low > temps[1][i]) {
low = temps[1][i];
index = i;
}
}
return index;
}
public static void main(String[] args) {
int temps[][] = getData();
System.out.println("\nAverage High Temperature : "+averageHigh(temps));
System.out.println("\nAverage Low Temperature : "+averageLow(temps));
System.out.println("\nThe The Month With The Highest Temperature : "+indexHighT$
System.out.println("\nThe The Month With The Lowest Temperature : "+indexLowTem$
}
}
As, the array stores values with 0-index based, i.e., values are stored in the array from index 0 to index (n-1). So, we have to return (index+1) from function indexHighTemp() and indexLowTemp().
Java code screenshot:
Java code:
import java.util.Scanner;
public class Lab2
{
public static int[][] getData()
{
Scanner sc = new Scanner(System.in);
int num;
System.out.print("Enter The Number of Months: ");
num = sc.nextInt();
int[][] temps = new int[2][num];
for(int i=0; i < num; i++)
{
System.out.print("Enter The High Temperature For Month " + (i+1) + " : ");
temps[0][i] = sc.nextInt();
System.out.print("Enter The Low Temperature For Month " + (i+1) + " : ");
temps[1][i] = sc.nextInt();
}
return temps;
}
public static double averageHigh(int[][] temps)
{
double sum = 0;
for(int i=0; i < temps[0].length; i++)
{
sum += temps[0][i];
}
return sum / (double)(temps[0].length);
}
public static double averageLow(int[][] temps)
{
double sum = 0;
for(int i=0; i < temps[1].length; i++)
{
sum += temps[1][i];
}
return sum / (double)(temps[1].length);
}
public static int indexHighTemp(int[][] temps)
{
int index = 0;
int high = temps[0][0];
for(int i=0; i < temps[0].length; i++)
{
if (high < temps[0][i])
{
high = temps[0][i];
index = i;
}
}
return index + 1;
}
public static int indexLowTemp(int[][] temps)
{
int index = 0;
int low = temps[1][0];
for(int i=0; i < temps[1].length; i++)
{
if (low > temps[1][i])
{
low = temps[1][i];
index = i;
}
}
return index + 1;
}
public static void main(String[] args)
{
int temps[][] = getData();
System.out.println("\nAverage High Temperature : " + averageHigh(temps));
System.out.println("\nAverage Low Temperature : " + averageLow(temps));
System.out.println("\nThe Month With The Highest Temperature : " + indexHighTemp(temps));
System.out.println("\nThe Month With The Lowest Temperature : " + indexLowTemp(temps));
}
}
Output: