# Thread: whats going on here? (float question)

1. ## whats going on here? (float question)

I have this simple program...that takes 4 integers and prints out the average

Code:
```#include <stdio.h>

int AVERAGE(int num1, int num2, int num3, int num4);
float OUTPUT(float output);

int main(void){

int a,b,c,d,x, avr, out;

printf("Please enter four integers : ");
scanf("%d%d%d%d",&a,&b,&c,&d );

avr=AVERAGE(a,b,c,d);

OUTPUT(avr);

exit(0);
}

int AVERAGE(int num1, int num2, int num3, int num4){
float result;
result = (num1 + num2+ num3 + num4)/4;
return(result);
}

float OUTPUT(float output){
printf("Average of the 4 integers is %5.2f \n", output);
}```

But for some reason i'm losing decimals. If for example i input 1, 2,3 and 4

out put should be 2.5 but i get 2

2. Your AVERAGE function is returning an int, you probably want it to return a float.

3. nope..that did not help

4. This worked for me:
Code:
```#include <stdio.h>

float Average(int Num1, int Num2, int Num3, int Num4)
{
float Result = Num1+Num2+Num3+Num4;
Result = Result/4;
return Result;
}

int main(void)
{
printf("%f\n", Average(1,2,3,4));
return 0;
}```
- Sean

5. Code:
```#include <stdio.h>

float AVERAGE(int num1, int num2, int num3, int num4);
float OUTPUT(float output);

int main(void){

int a,b,c,d,x, avr, out;

printf("Please enter four integers : ");
scanf("%d%d%d%d",&a,&b,&c,&d );

avr=AVERAGE(a,b,c,d);

OUTPUT(avr);

exit(0);
}

float AVERAGE(int num1, int num2, int num3, int num4){
float result;
result = (num1 + num2+ num3 + num4);
result = result /4;
return(result);
}

float OUTPUT(float output){
printf("Average of the 4 integers is %5.2f \n", output);
}```

i did a few changes and nothing is helping

6. Solution is simple. You were doing integer division. Do floating point division (done when you have at least one floating point operand). Also, return a float.
Code:
```float AVERAGE(int num1, int num2, int num3, int num4){
float result;
result = (num1 + num2+ num3 + num4)/4.0;
return(result);
}```
Now with my coding style :
Code:
```float average(int num1, int num2, int num3, int num4)
{
return (num1 + num2+ num3 + num4) / 4.0;
}```

7. Make 'avr' a float.

8. Originally posted by Dave_Sinkula
Make 'avr' a float.
thanks..that worked.

Popular pages Recent additions