简体   繁体   中英

Average of 4 variables function printing “4.00000” (divided by 4) rather than the actual average

I'm wondering if I'm using the correct variable types or not. I'm supposed to use only floats and ints as far as I'm aware. I know its printing the "divide by 4" part because in the function I switched it to divide by 5 and it printed "5.0000". If I make "result" an int, it obviously strips the answer of the decimal points, but it still gives me the correct average and not "4". Why is it storing "4.0000" in the result variable instead of the answer?

void main(void)
{
WDT_A->CTL = WDT_A_CTL_PW | WDT_A_CTL_HOLD;
// stop watchdog timer\

srand(time(NULL));
int val1 = (rand()%100);
int val2 = (rand()%100);
int val3 = (rand()%100);
int val4 = (rand()%100);
printf("%d\n",val1);
printf("%d\n",val2);
printf("%d\n",val3);
printf("%d\n",val4);

printf("%f\n",average4(val1, val2, val3, val4));
}


float average4(int val1, int val2, int val3, int val4){
float result;
result = (val1 + val2 + val3 + val4)/4;
return result;
}

Two issues:

  1. (val1 + val2 + val3 + val4)/4; is computed in integer arithmetic , as the terms are integral types. The remainder is discarded.

    One fix is to write 1.f * (val1 + val2 + val3 + val4) / 4; or the slightly obfuscated 0.25f * (val1 + val2 + val3 + val4);

  2. You're missing a prototype for average4 . Formally the behaviour on doing that is undefined (since and including C99), you might be observing a conversion to int at the calling site.

the following proposed code:

  1. doesn't quite compile because the three fields: WDT_A WDT_A_CTL_HOLD and WDT_A_CTL_PW are not declared.

And now the proposed code, with commentary

#include <stdio.h>     // printf()
#include <stdlib.h>    // rand(), srand()
#include <time.h>      // time()

// prototypes
float average4(int val1, int val2, int val3, int val4);


int main(void)                       // corrected signature
{
    WDT_A->CTL = WDT_A_CTL_PW | WDT_A_CTL_HOLD;
    // stop watchdog timer           // removed trailing line continuation 'slash'

    srand( (unsigned int)time(NULL)); // cast to expected type
    int val1 = (rand()%100);
    int val2 = (rand()%100);
    int val3 = (rand()%100);
    int val4 = (rand()%100);
    printf("%d\n",val1);
    printf("%d\n",val2);
    printf("%d\n",val3);
    printf("%d\n",val4);

    printf("%f\n",average4(val1, val2, val3, val4));
}


float average4(int val1, int val2, int val3, int val4)
{
    float result;
    // cast the 'integer' sum to 'float',
    // properly declared the '4' as a 'float' literal
    // so division is 'float' rather than `'integer' division
    result = (float)(val1 + val2 + val3 + val4)/4.0f;
    return result;
}

for testing, I commented out the line that tries to mess with the watchdog timer.

I ran the above code using gcc to compile/link then opened a terminal on my ubuntu linux system and here is a couple typical executions.

32
13
74
29
37.000000

and

77
53
90
49
67.250000

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM