Thread: Need Assistance?

  1. #1
    Registered User
    Join Date
    Apr 2008
    Posts
    5

    Need Assistance?

    My assignment is this....write a program to process a collection of daily high temperatures. Your program should display and count the number of hot days (85 or higher), the nummber of pleasant days (85-65), and the number of cold days (65 or lower). Modify your program to calculate the average temperature.

    I've written a sentinel loop that allows me to enter the values I have for temperature and calculates the average. I'm pretty sure seperate functions are in order for Hot Days, Pleasant Days, and Cold Days...not positive....although I'm not sure about how I list all the Hot Days, Pleasant days, and Cold Days, and how I would get it to give me a count for those catagories. Any help that you could offer would be greatly appreciated I've been making myself crazy over this problem.

  2. #2
    Nub SWE
    Join Date
    Mar 2008
    Location
    Dallas, TX
    Posts
    133
    This would allow the user to input fifty of each temperature.
    Code:
    int hot_days[50];
    int pleasant_days[50];
    int cold_days[50];
    Read in the user input and check to see what kind of day it is. Then store it in the appropriate array. Keep track of which position you're on in each array with three seperate counters.
    Code:
    int hot_counter = 0;
    int pleasant_counter = 0;
    int cold_counter = 0;
    Every time you read in a temperature and add it to the correct array, increment the correct counter.

  3. #3
    Registered User
    Join Date
    Apr 2008
    Posts
    5
    Alright this is what I have so far....but what I still am not sure of is how to count and print the number of hot days, Pleasant Days, and Cold Days. And to display the catagory of each temperature should I use "if, then" statements?

    Code:
    /*Program to process a collection of daily high temperatures
    Written by Janelle Shew
    Adapted from various course handouts and book
    February 2008
    Language written in C (gcc target)*/
    
    #include <stdio.h>
    
    #define sentinel 00
    
    int main(void)
    {
     int	 hot = 0,
    	 pleasant = 0,
    	 cold = 0,
    	 sum = 0,
    	 count,
    	 temp;
     double  average;
     char	 done;
    
    sum=0;
    count=0;
    printf("Enter temperature (00 to quit): ", sentinel);
    scanf("%d", &temp);
    while (temp != sentinel) 
    {  sum=sum+temp;
       count++;
       printf("Enter temperature (00 to quit): ", sentinel);
       scanf("%d", &temp);
    }
    printf("There are %d temperatures\n", count);
    average=(double)sum/(double) count;
    printf("\nAverage temperature is %8.2f\n", average);
    printf("Enter any character to terminate program");
    getchar();
     
    return (0);
    }

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    So after you read a temperature in (i.e., towards the top of your while loop), decide whether it's hot, pleasant, or cold. Then increment the appropriate counter. Note: to do something only if something else is true, you will need to use "if".

  5. #5
    Registered User
    Join Date
    Apr 2008
    Posts
    5
    alright I got some more code written, however I end up with more of a count for Hot Days, Pleasant Days, and/or Cold Days than are actually inputed. Also I still don't know how to list them....this is what I got....

    Code:
    /*Program to process a collection of daily high temperatures
    Written by Janelle Shew
    Adapted from various course handouts and book
    February 2008
    Language written in C (gcc target)*/
    
    #include <stdio.h>
    
    #define sentinel 00
    
    int main(void)
    {
     int	 hot = 0,
    	 pleasant = 0,
    	 cold = 0,
    	 sum = 0,
    	 count,
    	 temp;
     double  average;
    
    sum=0;
    count=0;
    printf("Enter temperature (00 to quit): ", sentinel);
    scanf("%d", &temp);
    while (temp != sentinel) 
    {  if (temp <= 59)
        ++cold;
      if (temp <= 84)
        ++pleasant;
      else 
        ++hot;
       sum=sum+temp;
       count++;
       printf("Enter temperature (00 to quit): ", sentinel);
       scanf("%d", &temp);
    }
    printf("There are %d Hot Days\n", hot);
    printf("There are %d Pleasant Days\n", pleasant);
    printf("There are %d Cold Days\n", cold);
    printf("There are %d temperatures\n", count);
    average=(double)sum/(double) count;
    printf("\nAverage temperature is %8.2f\n", average);
     
    return (0);
    }

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    So step through your code, in the case where temp is (say) 55 and see what happens. That should tell you where your extra numbers comes from.

    You have a list of the counts at the end of the code.

  7. #7
    Nub SWE
    Join Date
    Mar 2008
    Location
    Dallas, TX
    Posts
    133
    Code:
    #include <stdio.h>
    
    #define sentinel 00
    
    int main(void)
    {
    	int hot = 0;
    	int pleasant = 0;
    	int cold = 0;
    	int sum = 0;
    	int count = 0;
    	int temp;
    	double average;
    
    	printf("Enter temperature (00 to quit):");
    	fflush(stdout);
    	scanf("&#37;d", &temp);
    
    	while (temp != sentinel) 
    	{
    		if (temp <= 59)
    		{
    			cold++;
    		}
    		else if (temp >= 60 && temp <= 84)
    		{
    			pleasant++;
    		}
    		else
    		{
    			hot++;
    		}
    		
    		sum = sum + temp;
    		count++;
    		
    		printf("Enter temperature (00 to quit):");
    		fflush(stdout);
    		scanf("%d", &temp);
    	}
    	
    	printf("There are %d Hot Days\n", hot);
    	printf("There are %d Pleasant Days\n", pleasant);
    	printf("There are %d Cold Days\n", cold);
    	printf("There are %d temperatures\n", count);
    	
    	average = (double)sum / (double)count;
    	printf("\nAverage temperature is %8.2f\n", average);
    
    	return (0);
    }

  8. #8
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    temp >= 60 &&

    could be left out
    and better check the return value of scanf to avoid infinite loop
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  9. #9
    Nub SWE
    Join Date
    Mar 2008
    Location
    Dallas, TX
    Posts
    133
    Quote Originally Posted by vart View Post
    temp >= 60 &&

    could be left out
    and better check the return value of scanf to avoid infinite loop
    The reason his numbers were updating incorrectly was because it was considering cold days as ALSO pleasant days, due to the way he had his conditionals set up.

    I find it's easier to read and understand if you're explicit about your conditionals, like I was there.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Hello,i need assistance in completing the code
    By toader in forum C++ Programming
    Replies: 1
    Last Post: 06-22-2009, 03:32 AM
  2. Variable Timer assistance
    By rich2852 in forum C Programming
    Replies: 2
    Last Post: 01-21-2009, 05:43 AM
  3. Any assistance would be greatly appreciated
    By iiwhitexb0iii in forum C Programming
    Replies: 18
    Last Post: 02-26-2006, 12:06 PM
  4. Need some more assistance
    By Thantos in forum Windows Programming
    Replies: 6
    Last Post: 08-14-2003, 12:13 PM
  5. Need assistance for major text base Arena RPG project
    By Ruflano in forum C++ Programming
    Replies: 0
    Last Post: 04-04-2002, 11:11 AM