C programming question

Discussion in 'OT Technology' started by couchlock, Feb 17, 2003.

  1. couchlock

    couchlock OT Supporter

    Joined:
    Apr 8, 2002
    Messages:
    40,833
    Likes Received:
    309
    Location:
    The Highway
    K, I wrote this program that supposed to take in a bunch of tempatures, seperate them into high/low good day, what not. Calculate avg temp and also give the HIGHEST temp and LOWEST temp.

    Its all working except the high and low, I cant figure it out, Any clues? It keeps returning 59 for some reason???

    #include <stdio.h>
    int main (void)
    {
    int i,j,h,c,p;
    int min, max, sum;
    float avg;
    h=0;
    sum=0;
    c=0;
    p=0;

    for (i=0; i<26; ++i)
    {scanf("%d", &j);
    max = j;
    if (j >= max)
    max = j;
    else
    max = max;
    sum = sum + j;
    avg = (sum / 26);
    if (j >= 85)
    ++h;
    else {if (j < 60)
    ++c;
    else {if (j <= 84 || j >= 60)
    ++p;}}
    }
    printf("HIGH TEMPS: %d \n", h);
    printf("LOW TEMPS: %d \n", c);
    printf("PLEASANT DAYS: %d \n", p);
    printf("AVERAGE TEMPERATURE: %f \n", avg);
    printf("HIGH TEMPERATURE: %d \n", max);
    printf("LOW TEMPERATURE: %d \n", min);
    return 0;
    }
     
  2. Penguin Man

    Penguin Man Protect Your Digital Liberties

    Joined:
    Apr 27, 2002
    Messages:
    21,696
    Likes Received:
    0
    Location:
    Edmonton, AB
    1) Maximum isn't working because you've told it to set the maximum to the number entered on every loop (line 14). Just delete that line.
    2) Minimum isn't working because you never check to see if the number is the minimum. On every loop you need to check whether:
    a) the entered number is lower than the current minimum
    b) it's on the first loop (because minimum is made zero at the top).
    If either condition is true, then the number entered (j) is the new minimum.

    Hope that helps :).
     

Share This Page