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

In python I have my code written and I want just 4 functions to be fix...
In python I have my code written and I want just 4 functions to be fix in my code according to rule. My code is running but it has some problem regarding to rules. (I have did all the other things so you do not have to worry about other functions) 1) all the players has to play until he/she reaches to at least 500 points in first round. When user reach 500 points for the first time, user may...
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...
Hi, I have this code so far and need to modify it so that the output...
Hi, I have this code so far and need to modify it so that the output does not print something like 2x^0 but instead will just print 2. Currently it prints 2x^0. I also am having a problem with the output of Polynomial 1 - Polynomial 2. If both coefficient values for the polynomials are equal instead of Polynomial 1 - Polynomial 2 = 0 it outputs nothing. Just Polynomial 1 - Polynomial 2 = For example if I input...
Hi I have a java code for my assignment and I have problem with one of...
Hi I have a java code for my assignment and I have problem with one of my methods(slice).the error is Exception in thread "main" java.lang.StackOverflowError Slice method spec: Method Name: slice Return Type: Tuple (with proper generics) Method Parameters: Start (inclusive) and stop (exclusive) indexes. Both of these parameters are "Integer" types (not "int" types). Like "get" above, indexes may be positive or negative. Indexes may be null. Description: Positive indexes work in the normal way Negative indexes are described...
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 + " "...
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 :...
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT