Hi all, so I'm stuck on a certain part of a program for my C class. Okay the program is supposed to query a user for a set number of entries for temperatures which is 26. It is supposed to take those temperatures and calculate the avg. at the end. Here's the part I'm lost on; There's 3 ranges for these temps to be specified; hot, cold, and pleasant. Besides returning the avg. number of all the temps. entered it also has to return how many hot, cold, and pleasant days were entered. I'm using a for loop and if statements to to gather the 26 entries and calculate the avg. But I'm lost on how to sum up how many hot, pleasant, and cold days and return the printf statement saying so. Should I use an array to do this? Thanks for any insight.

Recommended Answers

All 14 Replies

>Should I use an array to do this?
Sounds good to me. You need to store the count somewhere, and an array keeps the three counts neatly organized under one object.

Well I understand that part of it. But at the end of the program I need it to return a printf statement telling the user how many hot, warm, and cold days were counted and the avg. temp for all of those days. This is where I'm stuck.

I don't see what the problem is. It's not hard to keep a count of some metric in your loop and then print the count later. Maybe you should post your code so I don't have to guess what you're doing wrong.

I don't see what the problem is. It's not hard to keep a count of some metric in your loop and then print the count later. Maybe you should post your code so I don't have to guess what you're doing wrong.

Ok, I'll post it in a couple of hours, I hope u don't mind checking it. I have to make some adjustments before I post though. I'll try to post it by 5 or 6. Thanks.

Here's my code so far:

/* A program to collect, calculate, and return the daily average of hot, 
   pleasant, and cold days
   Written by: Bryan Jones
   Febuary 22, 2008
*/

#include <stdio.h>


int get_n(void);
void calculate_and_print(int n);
double get_value(void);

int main(void)
{   int n;
    n=get_n();
    calculate_and_print(n);
    return 0;
}

int get_n(void)


{   
    int n;
    printf("What is the total of numbers to be entered?");
    scanf("%d", &n);
    return n;
}

void calculate(int n)

{

    double sum, average;
    int i;
    sum=0.0; 
for (i=1; i<=n; i++) 
    sum=sum+get_value();
    average=sum/(double)(n);
    printf("The average temperature is %lf\n\n",n, average);
    return;
}

But when I try to compile it it gives me an undefined reference error for the functions calculate and print and get value. Aren't those syntactically correct C functions? And if someone can just summarize for me how I need to structure the part for displaying the number of hot, cold, and warm days I really appreciate it. I know I need to use an if statment someone in there and this part might even be easier than what I have so far but for some reason it's stumping me. Thanks.

Pay close attention to the names of the functions that you (a) declare, (b) call, and (c) define. Short of this trifecta, you're asking for linker errors.

void calculate_and_print(int n); is not the same than void calculate(int n)
double get_value(void); is not found

printf("The average temperature is %lf\n\n",n, average); /* remove the n and the l */

void calculate_and_print(int n); is not the same than void calculate(int n)
double get_value(void); is not found

printf("The average temperature is %lf\n\n",n, average); /* remove the n and the l */

So you''re saying "Get_value" is not a valid function? Sorry for my in-proficiency but I just started this programming stuff and this assignment is a couple of levels above the last one, hence the trouble I'm having understanding the syntax.

Q: In post #6, on what line does the definition of get_value begin?
A: It doesn't exist.

Q: In post #6, on what line does the definition of get_value begin?
A: It doesn't exist.

I see, thanks.

Yes the n and | is not an existing function in C

void calculate_and_print(int n); is not the same than void calculate(int n)
double get_value(void); is not found

printf("The average temperature is %lf\n\n",n,average); /* remove the n and the l */

isn't "lf" an identifier for "long float"? This is weird cuz the example code our instructor gave us compiled for him but is made up of what seems to be many errors according to this forum. Well anyway, Here's my final code which by the way is waaaaay different than the example code our teacher gave us to calcultate his average:

#include <stdio.h>
#include <stdlib.h>

  int b;
  int cnt,tl,otl;

int main()
{
  int input[7];
  int b=0,inp=0,ccnt=0,wcnt=0,hcnt=0,sum=0,ave=0;
  printf("Program to collect the daily temperature for 7 days and return the total hot,\nwarm, and cold days and the average temperature for those days combined\n\n");

  for (b=0;b<7;b++)
    {
    printf("Enter a temperature:\n");
    scanf("%d", &input[b]);
    }

  for (b=0;b<7;b++)
    if (input[b] >= 85)
      {
        hcnt = hcnt +1;
        inp = input[b];
        otl = (sum + inp);
        sum = otl;
        ave = sum/7;

      }
    else if ((input[b] >= 65) && (input[b] < 85))
      {
        wcnt = wcnt + 1;
        inp = input[b];      
        otl = (sum + inp);
        sum = otl;
        ave = sum/7;
      }
    else if (input[b] < 70)
      {
       ccnt = ccnt +1;
       inp = wcnt + 1;
       otl = (sum + inp);
       sum = otl;
       ave= sum/7;
      }
        printf("The average temperature is %d\n\n", ave);
        printf("The total of Hot days is %d\n", hcnt);
        printf("The total of warm days is %d\n", wcnt);
       printf("The total of cold days is %d\n", ccnt);
   return 0;
}

First of all, don't use ICODE tags unless you want to show code inline with your message.

Your code is not too bad, but here are some comments for you to think about:

if (input[b] >= 85)
      {
        hcnt = hcnt +1;      // you can also use hcnt++;
        inp = input[b];      // why waste another variable?  
                             //    You can use input[b] anywhere

        otl = (sum + inp);   // what are these two lines doing?
        sum = otl;           // can't you just say sum = sum + input[b]
                             //    or  sum += input[b]?

        ave = sum/7;         // you don't want to compute the average 
                             //    while inside the loop
        
      }
    else if ((input[b] >= 65) && (input[b] < 85))
      {
        wcnt = wcnt + 1;      // same comments
        inp = input[b];      
        otl = (sum + inp);
        sum = otl;
        ave = sum/7;
      }
    else if (input[b] < 70)   // so all temps from 65 and 69 are 
                              //    both warm and cold?
      {
       ccnt = ccnt +1;
       inp = wcnt + 1;        // why is inp number of warm days+1?
       otl = (sum + inp);
       sum = otl;
       ave= sum/7;
      }

////  calculate your average here.

        printf("The average temperature is %d\n\n", ave);
        printf("The total of Hot days is %d\n", hcnt);
        printf("The total of warm days is %d\n", wcnt);
       printf("The total of cold days is %d\n", ccnt);
   return 0;
}

#%%##%^#!!!! Damn, I can't beleive I didn't see some of these errors before. And I already turned the assignment in. O well, I'm sure I'll get atleast a 70 on it as it does compile and run properly. Thanks for that assessment Walt.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.