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)

Recommended Answers

All 15 Replies

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);
}

of 3 numbers and its not an array

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);

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

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.

to calculate AND display or just display?

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.

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

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

he got it :D

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.