Home > Net >  The output for average is not correct
The output for average is not correct

Time:04-25

  1. The average which is the last output should display 60 but I got it wrong. What is the error here? Here is the temperature input 72, 46, 90, 20, 70 85, 60, 40, -1000.

  2. The total hot days should display 2 but on the output I got 3.

     #include <stdio.h>
    
     int categorize_days(int temp);
    
     int categorize_days(int temp){
     if (temp >= 85){
     return 1;
     }
     else if(temp >=60){
     return 2;
     }
     else{
         return 3;
     }
     }
    
     int main(){
    
     int i, temp, h, p,c, temp_ave=0, type_days;
     double ave; 
    
     printf("Lets check the whether !\n");
     printf("Keep entering the integer temperature, enter -1000 to quit\n\n");
    
     printf("Temperature: ");
     scanf("%d", &temp);
    
     while(temp!= -1000){
    
     for(i = 0; i<8; i   ){
     temp_ave =  temp;
     }
     type_days = categorize_days(temp);
    
     if( type_days == 1){
         printf ("Day: h\n\n");
         h  ;
     }
     else if(type_days == 2){
         printf ("Day: p\n\n");
         p  ;
     }
     else{
         printf ("Day: c\n\n");
         c  ;
     }
    
     printf("Temperature: ");
     scanf("%d", &temp);
     }
     printf("End\n\n");
    
    
     ave = temp_ave/8;
     printf("Total Hot days: %d \n", h);
     printf("Total Pleasant days: %d \n", p);
     printf("Total Cold days: %d \n", c);
     printf("Average temperature for 8 days is %f", ave);
    }
    

CodePudding user response:

The first big mistake is here:

for(i = 0; i<8; i   ){
 temp_ave =  temp;     // You are not adding temp to temp_ave, to add you should
 }                     // write temp_ave  = temp;
// Anyway this won't help you, cause you are trying to add the same number 8 times.
// So therefore it will give you wrong average, when you are calculating it.

I think this is what you wanted to do.

#include <stdio.h>

int categorize_days(int temp) {
    return (temp >= 85 ? 1 : (temp >= 60 ? 2 : 3));
}

int main() {
     int temp = 0, h = 0, p = 0, c = 0, cnt = -1;
     double ave = 0;

     printf("Lets check the whether !\n");
     printf("Keep entering the integer temperature, enter -1000 to quit\n\n");

    while (temp != -1000) {
        ave  = temp;
        cnt   ;
        printf("Temperature: ");
        scanf("%d", &temp);
        int type_days = categorize_days(temp);

        if (type_days == 1) {
            printf ("Day: h\n\n");
            h  ;
        }
        else if(type_days == 2) {
            printf ("Day: p\n\n");
            p  ;
        }
        else {
            printf ("Day: c\n\n");
            c  ;
        }
    }

    printf ("End\n\n");
    printf ("Total Hot days: %d \n", h);
    printf ("Total Pleasant days: %d \n", p);
    printf ("Total Cold days: %d \n", c);
    printf ("Average temperature for 8 days is %f", ave / cnt);
}

CodePudding user response:

  1. Don't use magic numbers in the code, following and updating becomes tedious. Use #define macros as necessary.
#define HOT_CUTOFF  85  // Fahrenheit
#define COLD_CUTOFF 60  // Fahrenheit
#define INPUT_GUARD -1000 
  1. Make use of enum to list out day-types:
enum {
    eHotDay  = 1,
    eColdDay = 2,
    eCozyDay = 3
} eDayType;
  1. Then your day_type() would change to :
int day_type (const int temp) {
    if (temp >= HOT_CUTOFF)
        return eHotDay;
    else if (temp <= COLD_CUTOFF) // should be less than or equal to
        return eColdDay;
    else
        return eCozyDay;
}
  1. Always initialise variables before usage. h, p & c are being used without initialisation. Also, temp_ave is misleading name for keeping total.
int i, temp, h, p,c, temp_ave=0, type_days;
  1. Why limit only to 8 inputs when you've -1000 as guard?

Simplified:

#include <stdio.h>

#define HOT_CUTOFF  85  // Fahrenheit
#define COLD_CUTOFF 60  // Fahrenheit

enum {
    eHotDay  = 1,
    eColdDay = 2,
    eCozyDay = 3
} eDayType;

//int categorize_days (int temp);   // redundant as you're defining the function before usage

//int categorize_days (int temp) {
int day_type (const int temp) {
    if (temp >= HOT_CUTOFF)
        return eHotDay;
    else if (temp <= COLD_CUTOFF) // should be less than or equal to
        return eColdDay;
    else
        return eCozyDay;
}

int main() {

    printf ("Keep entering the integer temperature, enter -1000 to quit\n\n");
    int total = 0;
    int count = 0;
    int hot = 0, cold = 0, cozy = 0;
    while (1) {
        int temp;
        if (1 != scanf ("%d", &temp)) {
            printf ("ERROR: Invalid input\n");
            return 1;
        }
        if (INPUT_GUARD == temp) break;

        total  = temp;
          count;
        switch (day_type(temp)) {
            case eHotDay :   hot; break;
            case eColdDay :   cold; break;
            case eCozyDay :   cozy; break;
        }
    }
    double avgTemp = 0.0;
    if (count)
        avgTemp = (double)total / count;

    printf ("End\n\n");
    printf ("Total Hot days: %d \n", hot);
    printf ("Total Pleasant days: %d \n", cozy);
    printf ("Total Cold days: %d \n", cold);
    printf ("Average temperature for 8 days is %.2lf\n", avgTemp);

    return 0;
}

CodePudding user response:

The average which is the last output should display 60 but I got it wrong.

This loop is a mess. Same as temp_ave = temp;, same as temp_ave = temp;.

for (i = 0; i<8; i  ) {
  temp_ave =  temp; // ????????
}

Instead:

temp_ave  = temp; // Note  =

temp_ave/8 in an int division. Instead, perform the division per the type of ave avoiding casts.

// ave = temp_ave/8;
ave = temp_ave;
ave /= 8;  // Division done per wider of type of `ave` and `int` constant `8`.

The total hot days should display 2 but on the output I got 3.

Enable all warnings.

Initialize h, p, c. @Weather Vane

  •  Tags:  
  • c
  • Related