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)

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

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 6 Years Ago by firstPerson: n/a

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?

well i did this

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

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 6 Years Ago by Akill10: n/a

my objectives were to make a function to display average to 2 decimal places.
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 6 Years Ago by nickx522: n/a

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

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

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.

This article has been dead for over six months. Start a new discussion instead.