Question

In: Computer Science

So I have written a code for it but i just have a problem with the...

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$

}

}

Solutions

Expert Solution

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:


Related Solutions

hey I have this program written in c++ and I have a problem with if statement...
hey I have this program written in c++ and I have a problem with if statement {} brackets and I was wondering if someone can fix it. //Name: Group1_QueueImplementation.cpp //made by ibrahem alrefai //programming project one part 4 //group members: Mohammad Bayat, Seungmin Baek,Ibrahem Alrefai #include <iostream> #include<stdlib.h> using namespace std; struct node { string data; struct node* next; }; struct node* front = NULL; struct node* rear = NULL; struct node* temp; void Insert() {     string val;    ...
***You can use a calculator but I just need the work written out so I can...
***You can use a calculator but I just need the work written out so I can see what you did and how you did it*** I need the answer for number 4 which is based on number 3. Please answer number 4 based on number 3. Number 4 can be found at the bottom. Thank you! 3. A business takes out a loan for $250,000 at 4.8% interest compounded monthly. If the business can afford to make monthly payments of...
Language: Java I have written this code but not all methods are running. First method is...
Language: Java I have written this code but not all methods are running. First method is running fine but when I enter the file path, it is not reading it. Directions The input file must be read into an input array and data validated from the array. Input file format (500 records maximum per file): comma delimited text, should contain 6 fields: any row containing exactly 6 fields is considered to be invalid Purpose of this code is to :...
Here is what I have so far. I have created a code where a user can...
Here is what I have so far. I have created a code where a user can enter in their information and when they click submit all of the information is shown. How can I add a required field for the phone number without using an alert? <!Doctype html> <html> <head> <meta charset="UTF-8"> <title>Login and Registeration Form Design</title> <link rel="stylesheet" type="text/css" href="signin.css"> <script> function myFunction(){ document.getElementById('demo').innerHTML = document.getElementById('fname').value + " " + document.getElementById('lname').value + " " + document.getElementById('street').value + " "...
I just wrote Python code to solve this problem: Write a generator that will return a...
I just wrote Python code to solve this problem: Write a generator that will return a sequence of month names. Thus gen = next_month('October') creates a generator that generates the strings 'November', 'December', 'January' and so on. If the caller supplies an illegal month name, your function should raise a ValueError exception with text explaining the problem. Here is my code: month_names = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'] def next_month(name: str) -> str:...
I already have the code of this program, I just want to know how to fix...
I already have the code of this program, I just want to know how to fix the code to Implement the 5th function (System.nanotime() and System.currentTimeMillis() methods) What Code does: Introduction Searching is a fundamental operation of computer applications and can be performed using either the inefficient linear search algorithm with a time complexity of O (n) or by using the more efficient binary search algorithm with a time complexity of O (log n). Task Requirements In this lab, you...
Java homework problem: I need the code to be able to have a message if I...
Java homework problem: I need the code to be able to have a message if I type in a letter instead of a number. For example, " Please input only numbers". Then, I should be able to go back and type a number. import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JTextField; public class LoginGui {    static JFrame frame = new JFrame("JFrame Example");    public static void main(String s[]) {        JPanel panel...
In Python I have a code: here's my problem, and below it is my code. Below...
In Python I have a code: here's my problem, and below it is my code. Below that is the error I received. Please assist. Complete the swapCaps() function to change all lowercase letters in string to uppercase letters and all uppercase letters to lowercase letters. Anything else remains the same. Examples: swapCaps( 'Hope you are all enjoying October' ) returns 'hOPE YOU ARE ALL ENJOYING oCTOBER' swapCaps( 'i hope my caps lock does not get stuck on' ) returns 'I...
hello! So I have this CIS assignment lab but when I try to make the code...
hello! So I have this CIS assignment lab but when I try to make the code I don't really know where to start from. My professor is very hard and he likes to see the outcomes as they are shown in the problem. Please help me! Write a program that can be used as a math helper for an elementary student. The program should display two random integer numbers that are to be added, such as:     247 + 129...
I have written code in C programming that checks where the command line arguments are floats...
I have written code in C programming that checks where the command line arguments are floats or not. For example, if I type "./math 1 1 0 0 2.5 3" in the terminal, my program realizes they are all floats but if I type "./math 1 1 0 0 2.5 g", it recognizes that not all arguments are floats and gives an error message. I want to take my code further such that after typing in "./math 1 1 0...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT