Mohamed MosȜd - 1 year ago 64

C Question

In the following code, the function

`avg(int i,int j,int k,int *pint,double *pfloat)`

`i`

`j`

`k`

`average = 22.45`

Here is the code:

`#include <stdio.h>`

#include <stdlib.h>

#include <ctype.h>

#include <math.h>

void avg(int i,int j,int k,int *pint,double *pfloat)

{

double average=(i+j+k)/3;

*pfloat=average-(floor(average));

*pint=floor(average);

}

int main()

{

int Integer=0;

double rem=0;

avg(2,4,5,&Integer,&rem);

printf("%d\n%lf",Integer,rem);

return 0;

}

the output is 3 and 0 why ??

Answer Source

```
double average=(i+j+k)/3;
```

This is an integer division. To get a useful floating point division, you can add a "." to the constant like so:

```
double average=(i+j+k)/3.;
```

making it a floating point constant (which in turn makes the division be done as floating point division).