Saturday, May 22, 2010

In this C++ program how do you round the answer to the nearest integer value before the answer is displayed?

I am not sure how to make it round to the nerest integer before it displays the anwser.





#include%26lt;iostream%26gt;


#include%26lt;cmath%26gt;


using namespace std;


int main(void)


{


int x, y;


float distance;


cout %26lt;%26lt; "x";


cin%26gt;%26gt;x;


cout %26lt;%26lt; "y";


cin%26gt;%26gt;y;


distance=sqrt(x*x+y*y);


cout%26lt;%26lt;"The distance is "%26lt;%26lt;distance%26lt;%26lt;endl;


system("pause");


return 0;


}

In this C++ program how do you round the answer to the nearest integer value before the answer is displayed?
floor()


ceil()


and abs()


are the functions that can be used for ur purpose
Reply:Use the ceil() function to round up and the floor() function to round down.





For example, ceil(2.3) is 3.0 and the floor(2.3) is 2.0.
Reply:You will need to use the RoundTo statement. Please look as these examples;


distance=300;


cout%26lt;%26lt;"The distance is "%26lt;%26lt;IO::Format::roundTo(distance,0)%26lt;%26lt;endl...


cout%26lt;%26lt;"The distance is "%26lt;%26lt;IO::Format::roundTo(distance,1)%26lt;%26lt;endl...


cout%26lt;%26lt;"The distance is "%26lt;%26lt;IO::Format::roundTo(distance,2)%26lt;%26lt;endl...


cout%26lt;%26lt;"The distance is "%26lt;%26lt;IO::Format::roundTo(distance,3)%26lt;%26lt;endl...








output


The distance is 300


The distance is 300.0


The distance is 300.00


The distance is 300.000
Reply:#include %26lt;math.h%26gt;


double round (double x)


long roundl (long double x)


float roundf (float x)


long lround (double x)


long lroundl (long double x)


long lroundf (float x)


long llround (double x)


long llroundl (long double x)


long llroundf (float x)
Reply:add 0.5 to distance and cast it to int.

tropical flowers

No comments:

Post a Comment