Results 1 to 7 of 7

Thread: Finding the maximum and minimum temperature

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Sep 2006
    Posts
    19

    Finding the maximum and minimum temperature

    Can anybody help me finish this or at least tell what my next step is. Thanks.



    A common task that must be done in a loop is to find the maximum and minimum of a sequence of values. This program that reads in a sequence of hourly temperature readings over a 24-hour period. You will be adding code to this program to find the maximum and minimum temperatures. Do the following:

    1. Save the file to your directory, open it and see what's there. Note that a for loop is used since we need a count-controlled loop. Your first task is to add code to find the maximum temperature read in. In general to find the maximum of a sequence of values processed in a loop you need to do two
    things:
    a) You need a variable that will keep track of the maximum of the values processed so far. This variable must be initialized before the loop. There are two standard techniques for initialization: one is to initialize the variable to some value smaller than any possible value being processed; another is to initialize the variable to the first value processed. In either case, after the first value is processed the maximum variable should contain the first value. For the temperature program declare a variable maxTemp to hold the maximum temperature. Initialize it to -1000 (a value less than any legitimate temperature).

    b) The maximum variable must be updated each time through the loop. This is done by comparing the maximum to the current value being processed. If the current value is larger, then the current value is the new maximum. So, in the temperature program, add an if statement inside the loop to compare the current temperature read in to maxTemp. If the current temperature is larger set maxTemp to that temperature. NOTE: If the current temperature is NOT larger, DO NOTHING!

    2. Add code to print out the maximum after the loop. Test your program to make sure it is correct. Be sure to test it on at least three scenarios: the first number read in is the maximum, the last number read in is the maximum, and the maximum occurs somewhere in the middle of the list. For testing
    purposes you may want to change the HOURS_PER_DAY variable to something smaller than 24 so you don't have to type in so many numbers!

    3. Often we want to keep track of more than just the maximum. For example, if we are finding the maximum of a sequence of test grades we might want to know the name of the student with the maximum grade. Suppose for the temperatures we want to keep track of the time (hour) the maximum temperature occurred. To do this we need to save the current value of the hour variable when we update the maxTemp variable. This of course requires a new variable to store the time (hour) that the maximum occurs. Declare timeOfMax (type int) to keep track of the time (hour) the maximum temperature occurred. Modify your if statment so that in addition to updating maxTemp you also save the value of hour in the timeOfMax variable. (WARNING: you are now doing TWO things when the if condition is TRUE.)

    4. Add code to print out the time the maximum temperature occurred along with the maximum.
    5. Finally, add code to find the minimum temperature and the time that temperature occurs. The idea is the same as for the maximum. NOTE: Use a separate if when updating the minimum temperature variable (that is, don't add an else clause to the if that is already there).

    Here is my code

    // **********************************************************
    // Temps.java
    //
    // This program reads in a sequence of hourly temperature
    // readings (beginning with midnight) and prints the maximum
    // temperature (along with the hour, on a 24-hour clock, it
    // occurred) and the minimum temperature (along with the hour
    // it occurred).
    // **********************************************************

    import java.util.Scanner;

    public class Temps
    {
    //----------------------------------------------------
    // Reads in a sequence of temperatures and finds the
    // maximum and minimum read in.
    //----------------------------------------------------
    public static void main (String[] args)
    {
    final int HOURS_PER_DAY = 24;

    int temp; // a temperature reading
    int timeOfMax;
    int maxTemp;
    int minTemp;

    Scanner scan = new Scanner(System.in);

    // print program heading
    System.out.println ();
    System.out.println ("Temperature Readings for 24 Hour Period");
    System.out.println ();

    for (int hour = 0; hour < HOURS_PER_DAY; hour++)
    {
    System.out.print ("Enter the temperature reading at " + hour +
    " hours: ");
    temp = scan.nextInt();
    }

    // Print the results

    }
    }
    Last edited by tmlucky14; Mar 8th, 2007 at 03:13 PM.

  2. #2
    Addicted Member TBeck's Avatar
    Join Date
    Apr 2006
    Location
    Ontario, Canada
    Posts
    254

    Re: Finding the maximum and minimum temperature

    the first thing you need to do is initialize the values of maxTemp and minTemp.

    where we have:
    Code:
    int maxTemp;
    int minTemp;
    we can add a bit to make something like
    Code:
    int maxTemp = -1000;
    int minTemp = 1000;
    once these are set we can add a couple if statements inside your for loop after you get the temperature input.The first if statement should see if temp > maxTemp.You will have to reassign the value of maxTemp to temp, but you will also have to store the value of hours into the variable timeOfMax. You need the other if statement to determine if temp < minTemp, if so you must set minTemp's value to temp and set the timeOfMin to hours.

    After that you should just need to add a println statement to the end and you should be good to go.

    feel free to post any questions about my description and please post again if you have made some more progress but still need a bit of help, we are happy to give you help
    Last edited by TBeck; Mar 9th, 2007 at 12:40 PM.

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Sep 2006
    Posts
    19

    Re: Finding the maximum and minimum temperature

    After I put the if statement in what should I tell it to print out under the if statement. Here is my code and I in color is what I am talking about.


    // **********************************************************
    // Temps.java
    //
    // This program reads in a sequence of hourly temperature
    // readings (beginning with midnight) and prints the maximum
    // temperature (along with the hour, on a 24-hour clock, it
    // occurred) and the minimum temperature (along with the hour
    // it occurred).
    // **********************************************************

    import java.util.Scanner;

    public class Temps
    {
    //----------------------------------------------------
    // Reads in a sequence of temperatures and finds the
    // maximum and minimum read in.
    //----------------------------------------------------
    public static void main (String[] args)
    {
    final int HOURS_PER_DAY = 10;

    int temp; //a temperature reading
    int maxTemp = -1000; //maximum temperature
    int miniTemp = 1000; //minimum temperature
    int timeOfMax; //time of maximum temperature
    int timeOfMin; //time of minimum temperature

    Scanner scan = new Scanner(System.in);

    // print program heading
    System.out.println ();
    System.out.println ("Temperature Readings for 24 Hour Period");
    System.out.println ();

    for (int hour = 0; hour < HOURS_PER_DAY; hour++)
    {
    System.out.print ("Enter the temperature reading at " + hour +
    " hours: ");
    temp = scan.nextInt();
    if (temp > maxTemp)
    System.out.println(");

    }

    // Print the results
    }
    }

  4. #4
    Addicted Member TBeck's Avatar
    Join Date
    Apr 2006
    Location
    Ontario, Canada
    Posts
    254

    Re: Finding the maximum and minimum temperature

    you can either print out that the temperature that was just entered is the highest, or you can print out nothing and just store the current temperature in the highest temperature variable. and at the same time store the time of the highest in the appropriate variable

  5. #5

    Thread Starter
    Junior Member
    Join Date
    Sep 2006
    Posts
    19

    Re: Finding the maximum and minimum temperature

    I have the maximum of the temperature, but I not getting the timOfMax.
    Here is the code I am having trouble with. thanks.

    for (int hour = 0; hour < HOURS_PER_DAY; hour++)
    {
    System.out.print ("Enter the temperature reading at " + hour +
    " hours: ");
    temp = scan.nextInt();
    if (temp > maxTemp)
    maxTemp = temp;
    if (timeOfMax > maxTemp)
    maxTemp = timeOfMax;

  6. #6
    Addicted Member TBeck's Avatar
    Join Date
    Apr 2006
    Location
    Ontario, Canada
    Posts
    254

    Re: Finding the maximum and minimum temperature

    insead of if
    Code:
    if(temp > maxTemp)
    maxTemp = temp;
    if (timeOfMax > maxTemp)
    maxTemp = timeOfMax;
    try

    Code:
    if (temp > maxTemp)
    {
    maxTemp = temp;
    timeOfMax = hour;
    }
    any code inside the {}s after the if statement will be executed when the if statment condition is true, so in this case, when the current temperature is greater than the previous max temperature we will:
    1)change the value of maxTemp to reflect the new temperature
    2)assign a value to timeOfMax, so that we can remember at which time this occured. if we do not assign this value now, we will not know when it occured if we try to print it out

  7. #7
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594

    Re: Finding the maximum and minimum temperature

    Use [code][/code] tags!
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width