i need help writing display of average function. i find the average in first function "int Main()". it needs to be rounded to 2 decimal points.

int display_avg( //dont know what parameters should be)

nickx522 -1 Light Poster

i need help writing display of average function. i find the average in first function "int Main()". it needs to be rounded to 2 decimal points.

int display_avg( //dont know what parameters should be)

Akill10 96 Posting Pro

so you have calculated the average? Now all you need is to print it, so just pass in an int/double

mrnutty 761 Senior Poster

One way is to do the following:

```
void display(float average){
cout.precision(2);
cout << average << endl;
}
```

Or you can truncate the number to 2 decimal place like so :

```
void display(float average){
cout << truncate(average,2) << endl;
}
//use integer division
float truncate(float num,int decimalPlaces){
int factor = std::pow(10,decimalPlaces);
int tmp = num * factor;
return tmp/float(factor);
}
```

Edited by mrnutty because: * n/a *

Akill10 96 Posting Pro

of 3 numbers

You said you found the average in the main() method

Have you figured out how to calculate the average yet or not?

nickx522 -1 Light Poster

well i did this

```
if ( Num > 0 && Num2 > 0)
Average = Total/Count;
else
Average = 0;
```

Akill10 96 Posting Pro

I don't understand what you want.

To display a number you just print it out....

and for 2 dp use printf("%.2f", Average);

Edited by Akill10 because: * n/a *

mrnutty 761 Senior Poster

average of 3 numbers = (number1 + number2 + number3)/3.0f

nickx522 -1 Light Poster

ok first i have to input 3 numbers then find median(second function) then find averages of median numbers. im going to be inputting more then 1 set of 3 number if that makes sense.

Edited by nickx522 because: * n/a *

Akill10 96 Posting Pro

to calculate AND display or just display?

nickx522 -1 Light Poster

how can u display average without calculating it? calculate and display of course

Akill10 96 Posting Pro

can you just post all your code? It's pretty late and I have some brain lag when reading english

nickx522 -1 Light Poster

One way is to do the following:

`void display(float average){ cout.precision(2); cout << average << endl; }`

Or you can truncate the number to 2 decimal place like so :

`void display(float average){ cout << truncate(average,2) << endl; } //use integer division float truncate(float num,int decimalPlaces){ int factor = std::pow(10,decimalPlaces); int tmp = num * factor; return tmp/float(factor); }`

for the first thing you did, i need to return the average to main() so i dont think it should be void.

mrnutty 761 Senior Poster

Call it like so : `display( (num1+num2+num3)/3.0f) );`

nickx522 -1 Light Poster

lol thats alright i think i got it now...thanks to everyone :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.