Thread: Average temp:

  1. #1
    Registered User
    Join Date
    Jul 2013
    Location
    Germany
    Posts
    499

    Average temp:

    I wanted to take the averages of the whole week and output a print statement. I've tired few ways this like assigning "temp" a pointer, or in each if statement assigning temp to a new variable like a, b , c etc. Then adding them all up at the end and dividing by 7. My issue is I am unclear how to store the number typed for each if statement while the program is running, then have it compute the simple equation. The code in question is below:

    Code:
    #include <stdio.h>
    
    int main(int argc, const char * argv[])
    {
    
        int i, j = 0, temp = 0;
        
        for (i=1; i<=7; i++) {
            j++;
            
            
        printf("Enter the temperature for the %d day of the week\n", j);
         scanf("%d",&temp);
                
            
            if (temp >= 90) {
                
                printf("It's a scorcher! Turn on the air conditioning!\n");
               
            }
            else if (temp >= 80) {
                
                printf("Another hot day! Go for a swim!\n");
                
            }
            else if (temp >= 70) {
                
                printf("A good day to tend to your garden\n");
                
            }
            else if (temp >= 60) {
                
                printf("Bring along a sweater today!\n");
                
            }
            else if (temp <= 59) {
                
                printf("Stay inside and study C programing\n");
                
            }
            
        }
        
       
        return 0;
    }


  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    You don't really have to store the individual numbers, just the sum. So, create a sum variable (initialized to 0) and increment it by whatever temperature the user types in each time through the loop. After the loop is done, you simply divide this sum by 7 and display... you don't even have to store this average prior to its output, you can calculate and display the value in the printf statement without need for an additional variable. Your average should probably a floating-point value so you'd probably want to make sure you avoid integer division (divide by 7.0 instead of 7 would be sufficient).

    If you absolutely know you are always going to be accepting 7 values, then it would seem that the j variable isn't needed.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Clean Windows Temp folder and the User Temp folder?
    By patrick22 in forum Windows Programming
    Replies: 11
    Last Post: 01-27-2008, 04:29 PM
  2. CPU temp
    By PING in forum Tech Board
    Replies: 5
    Last Post: 01-28-2006, 06:25 AM
  3. cpu temp
    By dP munky in forum Tech Board
    Replies: 36
    Last Post: 02-07-2003, 06:01 PM
  4. CPU Temp
    By tntcoder in forum Windows Programming
    Replies: 3
    Last Post: 08-26-2001, 01:20 PM