0

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)

3
Contributors
15
Replies
16
Views
6 Years
Discussion Span
Last Post by Akill10
Featured Replies
  • One way is to do the following: [code] void display(float average){ cout.precision(2); cout << average << endl; } [/code] Or you can truncate the number to 2 decimal place like so : [code] void display(float average){ cout << truncate(average,2) << endl; } //use integer division float truncate(float num,int decimalPlaces){ int … Read More

0

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

1

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 firstPerson: n/a

0

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?

0

well i did this

if ( Num > 0 && Num2 > 0)
      Average = Total/Count;
   else
      Average = 0;
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 by Akill10: n/a

0

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 by nickx522: n/a

0

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

0

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

0

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 topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.