Thread: Temperature analyzer C program help

  1. #1
    Registered User
    Join Date
    Jan 2017
    Posts
    11

    Temperature analyzer C program help

    Hello cboard,

    I'm experiencing difficulties with the syntax of my c program where its intent it to ask user for high and low temperatures for 4 days, analyze those values to see if they meet requirements(temperatures must not exceed 40 degrees celsisus/fall below -40 celsius and high must be greater than low),provide the average temperature for the 4 days, and then display what the highest/lowest temperature were and on which day they respectively occured on.

    My current problem is that my program won't discard the incorrect values after the nested while loop is executed and therefore outputs a false average temperature

    Code:
    #include<stdio.h>const int NUMS = 4;
    
    
    int main (void)
    {
      int i,totalHigh=0,totalLow=0; 
      int high[4];
      int low[4];
      float combinedTemperature,meanTemperature;
      printf("---=== IPC Temperature Calculator ===---\n");
      
      for(i = 1; i <= NUMS; i++)
      {
        printf("Enter the high value for day %d: ",i);
        scanf("%d", &high[i]);
        totalHigh=(totalHigh+high[i]);
    
    
        printf("                                  \n");
    
    
        printf("Enter the low value for day %d: ",i);
        scanf("%d", &low[i]);
        totalLow=(totalLow+low[i]);
    
    
        printf("                                  \n");
        
        while(high[i]<low[i]||high[i]>40||high[i]<-40||low[i]<-40)
        {
          printf("Incorrect values, temperatures must be in the range -40 to 40, high must be greater than low.\n");
    
    
          printf("Enter the high value for day %d: ",i);
          scanf("%d", &high[i]);
          totalHigh=(totalHigh+high[i]);
    
    
          printf("                                  \n");
    
    
          printf("Enter the low value for day %d: ",i);
          scanf("%d", &low[i]);
          totalLow=(totalLow+low[i]);
    
    
          printf("                                  \n");
        }
        combinedTemperature=(totalHigh+totalLow);
        meanTemperature=(combinedTemperature/8);
      }
      printf("The average (mean) temperature was: %.2f",meanTemperature);
      return 0;
    }
    Output that would give me a false average temperature since the incorrect temperatures (45,11) are taken into account:

    ---=== IPC Temperature Calculator ===---
    Enter the high value for day 1: 45


    Enter the low value for day 1: 11


    Incorrect values, temperatures must be in the range -40 to 40, high must be greater than low.
    Enter the high value for day 1: 6


    Enter the low value for day 1: 4


    Enter the high value for day 2: 6


    Enter the low value for day 2: 4


    Enter the high value for day 3: 6


    Enter the low value for day 3: 4


    Enter the high value for day 4: 6


    Enter the low value for day 4: 4


    The average (mean) temperature was: 12.00


    How do I change my source code to only record correct values? How would I implement a way for my program to display what the highest/lowest temperature were and on which day they respectively occured on?

    Thanks in advance for your help

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > for(i = 1; i <= NUMS; i++)
    One of the main problems you have is that arrays are 0-based subscripts.

    So an array[4] has valid subscripts 0,1,2,3

    By doing <= NUMS, you're running off the ends of both arrays.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Jan 2017
    Posts
    11
    Quote Originally Posted by Salem View Post
    > for(i = 1; i <= NUMS; i++)
    One of the main problems you have is that arrays are 0-based subscripts.

    So an array[4] has valid subscripts 0,1,2,3

    By doing <= NUMS, you're running off the ends of both arrays.
    So you would suggest "for(i=1;i < NUMS; i++)" right? I only initialized the loop control variable as '1' with that condition because I couldn't figure out how my program would prompt for day 1, day 2, day 3 and day 4 sequentially otherwise.

    If I use "for(i=1; i < NUMS; i++)", it won't ask for day 4

  4. #4
    Registered User
    Join Date
    Sep 2014
    Posts
    364
    Quote Originally Posted by BlackUmbreon View Post
    So you would suggest "for(i=1;i < NUMS; i++)" right?
    No.

    Quote Originally Posted by BlackUmbreon View Post
    If I use "for(i=1; i < NUMS; i++)", it won't ask for day 4
    The right way is:
    Code:
    …
        for(i = 0; i < NUMS; i++)
        {
            printf("Enter the high value for day %d: ", i + 1);
            scanf("%d", &high[i]);
            totalHigh = totalHigh + high[i];
    …
    Other have classes, we are class

  5. #5
    Registered User
    Join Date
    Jan 2017
    Posts
    11
    Quote Originally Posted by WoodSTokk View Post
    No.



    The right way is:
    Code:
    …
        for(i = 0; i < NUMS; i++)
        {
            printf("Enter the high value for day %d: ", i + 1);
            scanf("%d", &high[i]);
            totalHigh = totalHigh + high[i];
    …
    Thanks, didn't even cross my mind that I could increment i within the printf function. Would you know how I could change my code to only record/read valid temperatures?

  6. #6
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Quote Originally Posted by BlackUmbreon View Post
    Thanks, didn't even cross my mind that I could increment i within the printf function. Would you know how I could change my code to only record/read valid temperatures?
    Code:
    prompt for input
    read input
    
    while input is invalid
        print error message
        prompt for input
        read input
    
    process input

  7. #7
    Registered User
    Join Date
    Jan 2017
    Posts
    11
    Quote Originally Posted by Matticus View Post
    Code:
    prompt for input
    read input
    
    while input is invalid
        print error message
        prompt for input
        read input
    
    process input
    Thanks

    Updated code:
    Code:
    #include<stdio.h>const int NUMS = 4;
    
    
    int main (void)
    {
      int i,totalHigh=0,totalLow=0; 
      int high[4];
      int low[4];
      float combinedTemperature,meanTemperature;
      printf("---=== IPC Temperature Calculator ===---\n");
      
      for(i = 0; i < NUMS; i++)
      {
        printf("Enter the high value for day %d: ",i+1);
        scanf("%d", &high[i]);
        
        printf("                                  \n");
    
    
        printf("Enter the low value for day %d: ",i+1);
        scanf("%d", &low[i]);
    
    
        while(high[i]>low[i]&&high[i]<40&&high[i]>-40&&low[i]>-40)
          {
            totalHigh = totalHigh + high[i];
            totalLow = totalLow+low[i];
            break;
          }
        
        printf("                                  \n");
        
        while(high[i]<low[i]||high[i]>40||high[i]<-40||low[i]<-40)
        {
          printf("Incorrect values, temperatures must be in the range -40 to 40, high must be greater than low.\n");
    
    
          printf("Enter the high value for day %d: ",i+1);
          scanf("%d", &high[i]);
          
          printf("                                  \n");
    
    
          printf("Enter the low value for day %d: ",i+1);
          scanf("%d", &low[i]);
    
    
          while(high[i]>low[i]&&high[i]<40&&high[i]>-40&&low[i]>-40)
          {
            totalHigh = totalHigh + high[i];
            totalLow = totalLow+low[i];
            break;
          }
          printf("                                  \n");
        }
        combinedTemperature=(totalHigh+totalLow);
        meanTemperature=(combinedTemperature/8);
      }
      printf("The average (mean) temperature was: %.2f",meanTemperature);
      return 0;
    }

  8. #8
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Code:
    while(high[i]>low[i]&&high[i]<40&&high[i]>-40&&low[i]>-40)
    {
        totalHigh = totalHigh + high[i];
        totalLow = totalLow+low[i];
        break;
    }
    There is no purpose for this "while", as you're explicitly breaking out of the loop the very first time through. You do this again later in the code.

    You also may want to calculate the mean temperature outside of the main loop, after all the values have been entered.

    Instead of using the magic number 8 when calculating the mean, you should be using NUMS (times two).

    If you spend some time planning out the logic on paper before writing code, your program could be written better.

  9. #9
    Registered User
    Join Date
    Jan 2017
    Posts
    11
    Quote Originally Posted by Matticus View Post
    Code:
    while(high[i]>low[i]&&high[i]<40&&high[i]>-40&&low[i]>-40)
    {
        totalHigh = totalHigh + high[i];
        totalLow = totalLow+low[i];
        break;
    }
    There is no purpose for this "while", as you're explicitly breaking out of the loop the very first time through. You do this again later in the code.

    You also may want to calculate the mean temperature outside of the main loop, after all the values have been entered.

    Instead of using the magic number 8 when calculating the mean, you should be using NUMS (times two).

    If you spend some time planning out the logic on paper before writing code, your program could be written better.
    Yeah, I found the logic/semantics is by far the concept that's giving me the most trouble about programming soo far. I read that magic number link you provided and will practice writing flowcharts before source code in the future.

    Does calculating the mean temperature outside of the main "for loop" make the code easier to read or is it for another reason? Regarding the while loop, are you suggesting I remove both break statements?

  10. #10
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Quote Originally Posted by BlackUmbreon View Post
    Yeah, I found the logic/semantics is by far the concept that's giving me the most trouble about programming soo far. I read that magic number link you provided and will practice writing flowcharts before source code in the future.
    Writing code is only a piece of programming. Problem solving and logic development are necessary skills, so it is good that you will practice these. In fact, once you have a solution sufficiently broken down, converting it to code is generally pretty straight-forward.

    Quote Originally Posted by BlackUmbreon View Post
    Does calculating the mean temperature outside of the main "for loop" make the code easier to read or is it for another reason?
    You want to read all values before you calculate the mean. This means the calculation should occur after the for loop has completed.

    Quote Originally Posted by BlackUmbreon View Post
    Regarding the while loop, are you suggesting I remove both break statements?
    If you remove the break, those while loops will become infinite loops, since the control variables are not updated within them. To maintain functionality, turn them into ifs instead.

  11. #11
    Registered User
    Join Date
    Jan 2017
    Posts
    11
    Quote Originally Posted by Matticus View Post
    Writing code is only a piece of programming. Problem solving and logic development are necessary skills, so it is good that you will practice these. In fact, once you have a solution sufficiently broken down, converting it to code is generally pretty straight-forward.



    You want to read all values before you calculate the mean. This means the calculation should occur after the for loop has completed.



    If you remove the break, those while loops will become infinite loops, since the control variables are not updated within them. To maintain functionality, turn them into ifs instead.
    Thanks, that makes sense. So basically never use break statements inside nested loops and instead always use if statements if break statements are needed.

    Oh, and would you know how I would be able to display the highest/lowest temperatures and on which day they each occured on? Would I need to access and compare index values?

  12. #12
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Quote Originally Posted by BlackUmbreon View Post
    So basically never use break statements inside nested loops and instead always use if statements if break statements are needed.
    Not quite. My advice was tailored to your specific situation, and not intended to be a univeral rule. Break statements should be used when needed to break out of a loop.

    Specifically, your example of:

    Code:
    while( condition )
    {
        do something, but condition is never updated
        break
    }
    ... is equivalent to, and better expressed as:

    Code:
    if( condition )
    {
        do something
    }
    Exiting a loop can be done by monitoring its condition:

    Code:
    while( condition )
    {
        do something; condition should be updated at some point
    }
    ... or using a break statement when necessary; for example:

    Code:
    while( condition )
    {
        do something; condition should be updated at some point
        if something goes wrong
            break
    }
    Quote Originally Posted by BlackUmbreon View Post
    Oh, and would you know how I would be able to display the highest/lowest temperatures and on which day they each occured on? Would I need to access and compare index values?
    You would print out each element of each array in order. The for loop is perfectly suited for this task. The index variable (used to access each element of an array) can also be used to print the day. In fact, you utilize each of these concepts in your code already.

  13. #13
    Registered User
    Join Date
    Jan 2017
    Posts
    11
    Quote Originally Posted by Matticus View Post
    You would print out each element of each array in order. The for loop is perfectly suited for this task. The index variable (used to access each element of an array) can also be used to print the day. In fact, you utilize each of these concepts in your code already.
    Sorry for the way late message, something came up yesterday :/. Anyway, you were referring to parallel arrays for my question about displaying the high/low temperatures right? I think I worded the question incorrectly if that's the case. I meant to say how would I display only the highest/lowest temperatures of the 4 days (i.e The highest temperature was "12 " on day "4 ", The lowest temperature was "-5 " on day "3 ").

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Temperature conversion program bug.
    By prafiate in forum C Programming
    Replies: 6
    Last Post: 08-13-2012, 09:40 AM
  2. Here is my Sentence Analyzer Program!
    By jeremy duncan in forum C Programming
    Replies: 7
    Last Post: 01-02-2012, 12:04 PM
  3. Need help with temperature conversion program
    By Aequitas in forum C Programming
    Replies: 4
    Last Post: 03-06-2011, 08:28 PM
  4. Program Runs off Screen / Lexical Analyzer
    By pantherman34 in forum C Programming
    Replies: 6
    Last Post: 05-05-2010, 06:10 PM
  5. Need Help on text analyzer program
    By drkmarine in forum C++ Programming
    Replies: 8
    Last Post: 03-16-2005, 06:34 PM

Tags for this Thread