Thread: Need Help With C Program Plz

  1. #1
    Registered User
    Join Date
    Feb 2011
    Posts
    39

    Angry Need Help With C Program Plz

    Hi the program works and all i just need help finding the average. It uses the last # i enter and not the rest. For ex *i enter * = 10 ,10, 10 ,10,-99
    and it just uses -99 not the 10's

    Problem:
    Write a program to process a collection of daily high temperatures. Your program should count and print the number of hot days (high temperature 85 or higher), the number of pleasant days (high temperature 60-84), and the number of cold days (high temperatures less than 60). It should also display the category of each temperature.
    Last edited by 123456tacos; 02-24-2011 at 03:06 AM.

  2. #2
    Registered User
    Join Date
    Feb 2011
    Posts
    39
    Sorry heres the program :
    Code:
     
    #include <stdio.h>
    
    
    int main (void)
    {
        int temp;
        double average;
        int hot_days, pleasant_days, cold_days;
        char x;
        double total;
        double avg;
    
    
        hot_days      = 0;
        pleasant_days = 0;
        cold_days     = 0;
    
        printf("Enter the temperatures and Enter -99 when finished>");
    
        do
        {
            scanf("%d", &temp);
    
            if (temp == -99)
                break;
    
            if(temp >= 85)
                hot_days++;
    printf("Enter the temperatures and Enter -99 when finished>");
            if(60 < temp && temp <= 84)
                pleasant_days++;
    
            if(temp <= 60)
                cold_days++;
            } while (temp != -99);
            
            total += temp +99;
            avg = total/(hot_days + pleasant_days + cold_days) ;
    
        printf("%d Hot days, %d Pleasant Days and %d Cold Days\n",
            hot_days, pleasant_days, cold_days);
       
        printf("The average is> %.2f\n",avg);  
    system ("pause");  
    return (0);
    }

  3. #3
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,472
    Code:
     if(60 < temp && temp <= 84)
                pleasant_days++;
    Whats going on there?
    Also its a bit messed up, you need to tidy those conditions up, and your loop is controlled by the -99 value, but you also have a statement to break as soon as -99 is input?
    You should structure your program so that the real temperature values you want to work with are only processed if the input is not -99, maybe use an if statement immediately inside of your while loop, instead of the break, i dunno its just a clarity thing

    Code:
    while(input != -99)
    {
    	//get value
    
    	if(value != -99)
    	{
    		//if() to check cold days
    		//if() to check hot days
    		//etc
    	}
    }
    Last edited by rogster001; 02-24-2011 at 03:36 AM.
    Thought for the day:
    "Are you sure your sanity chip is fully screwed in sir?" (Kryten)
    FLTK: "The most fun you can have with your clothes on."

    Stroustrup:
    "If I had thought of it and had some marketing sense every computer and just about any gadget would have had a little 'C++ Inside' sticker on it'"

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Code:
     
    #include <stdio.h>
    
    
    int main (void)
    {
        int temp;
        double average;
        int hot_days, pleasant_days, cold_days;
        char x;
        double total;
        double avg;
    
    
        hot_days      = 0;
        pleasant_days = 0;
        cold_days     = 0;
        total = 0.0;
        do
        {
            printf("\nEnter the temperatures and Enter -99 when finished>");
            scanf("%d", &temp);
    
            if (temp == -99)
                break;
    
            if(temp >= 85)
                hot_days++;
    
            if(60 < temp && temp <= 84)
                pleasant_days++;
    
            if(temp <= 60)
                cold_days++;
            total += temp;
    
         } while (temp != -99);
            
            avg = total/(hot_days + pleasant_days + cold_days) ;
    
        printf("%d Hot days, %d Pleasant Days and %d Cold Days\n",
            hot_days, pleasant_days, cold_days);
       
        printf("The average is> %.2f\n",avg);  
    system ("pause");  
    return (0);
    }
    That should be very close. Haven't tested it, but made a few tweaks in it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Debugging strings program help plz
    By allen9190 in forum C Programming
    Replies: 1
    Last Post: 11-08-2009, 08:17 PM
  2. Need help with my program...
    By Noah in forum C Programming
    Replies: 2
    Last Post: 03-11-2006, 07:49 PM
  3. Plz help wit my program
    By psychaospath in forum C++ Programming
    Replies: 4
    Last Post: 03-10-2006, 05:13 PM
  4. Plz help me to debug my program
    By Bage in forum Linux Programming
    Replies: 1
    Last Post: 04-02-2004, 01:54 PM
  5. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM

Tags for this Thread