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

***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:...
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...
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...
This is the code I have. My problem is my output includes ", 0" at the...
This is the code I have. My problem is my output includes ", 0" at the end and I want to exclude that. // File: main.cpp /*---------- BEGIN - DO NOT EDIT CODE ----------*/ #include <iostream> #include <fstream> #include <sstream> #include <iomanip> using namespace std; using index_t = int; using num_count_t = int; using isConnected_t = bool; using sum_t = int; const int MAX_SIZE = 100; // Global variable to be used to count the recursive calls. int recursiveCount =...
C++ Problem. I am providing the code. Just Please provide the new function and highlight it....
C++ Problem. I am providing the code. Just Please provide the new function and highlight it. implement the functions replaceAt, seqSearch, and remove. Test your new function too in main. Also, Test Old functions in main. Show the output. Also, modify the functions accordingly which have "See Programming Exercise 22". main.cpp : #include <iostream> using namespace std; #include "arrayListTypetempl.h" int main(){    arrayListType<int> intList;    arrayListType<char> charList;       intList.insertEnd(5);    intList.insertEnd(3);    intList.insertEnd(4);    intList.insertEnd(55);       charList.insertEnd('a');   ...
Need this in C#. Below is my code for Problem 3 of Assignment 2. Just have...
Need this in C#. Below is my code for Problem 3 of Assignment 2. Just have to add the below requirement of calculating the expected winning probability of VCU. Revisit the program you developed for Problem 3 of Assignment 2. Now your program must calculate the expected winning probability of VCU through simulation. Run the simulation 10,000 times (i.e., play the games 10,000 times) and count the number of wins by VCU. And then, calculate the winning probability by using...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT