Monday, July 27, 2009

In C++ Convert each decimal number to the nearest integer??

This is for part of a programming exercise at school. I know I'm missing something and there's an easy way to do this. Here's what the whole program should do:





A) Prompt the user to input 5 decimal numbers.


B) Prints the 5 decimal numbers.


C)Convert each decimal number to the nearest integer.


D)Add the 5 integers


E)Print the sum and average of the 5 integers.





Casting won't round the double to the nearest whole number, it just truncates everything past the decimal point.





I tried using:





cout %26lt;%26lt; setprecision(0) %26lt;%26lt; "Sum: " %26lt;%26lt; a+b+c+d+e %26lt;%26lt; endl;





to convert the numbers and then get the sum but it's not working correctly. For example, I used 1.1 five times and the sum came out to 6 instead of 5, which tells me that the 1.1's weren't converted into integers and then added but rather the answer 5.5 was converted into an integer.

In C++ Convert each decimal number to the nearest integer??
Your logical operators are taking affect.. putting the "+" in presidence before the function base it's self... so it's adding together the floating point values of a-e, then setting percision.





Heres a work around:





int x = 0;


x = (int)(a + 0.5);


x += (int)(b + 0.5);


x += (int)(c + 0.5);


x += (int)(d + 0.5);


x += (int)(e + 0.5);





cout %26lt;%26lt; "Sum: " %26lt;%26lt; x %26lt;%26lt; endl;





'modified to "round to nearest integer"... not just cast to integer.
Reply:use ceil() function or floor() funtion....


floor does the folowing work


suppose u entered 1.9


then floor will convert the nmbr into 1





ceil does the folowing work


suppose u entered 1.9 n Ceil wil convert the nmbr into 2





try to learn the working of the abv mentioned functions...





as it is ur school work so i wont post solved prgrm here...but here is the algorithm...so try to solve ur problem using this algorithm


Start of Algo


------





void main


{





int a=5, b;


float c;


for (b=0; b%26lt;=a; b++)


start of for loop


int xyz=0; // counter


prompt user to enter any nmbr


cin%26gt;%26gt;c;


convert the variable c by using Ceil or floor function


display C


initialize a variable of type float .suppose float d=0.0


now d=d+c;


end of for loop


display D (it is the sum of the nmbrs entered by the user)


divide D by xyz to find the average


display XYZ (it is the average )


getch();


} //end of main





End of Algo...


------





if u find it help ful then do tell me... m waitng for ur reply


u may contact me if u face any prblm regarding C++
Reply://Old casting trick, code not tested


#include %26lt;iostream%26gt;


int main(){


double a, b, c, d, e;


printf("\nInput a: ");


cin %26gt;%26gt; a;


printf("\nInput b: ");


cin %26gt;%26gt; b;


printf("\nInput c: ");


cin %26gt;%26gt; c;


printf("\nInput d: ");


cin %26gt;%26gt; d;


printf("\nInput e: ");


cin %26gt;%26gt; e;


int temp = (int) (a + 0.5);


cout %26lt;%26lt; "a:" %26lt;%26lt; temp %26lt;%26lt; endl;


temp = (int) (b + 0.5);


cout %26lt;%26lt; "b:" %26lt;%26lt; temp %26lt;%26lt; endl;


temp = (int) (c + 0.5);


cout %26lt;%26lt; "c:" %26lt;%26lt; temp %26lt;%26lt; endl;


temp = (int) (d + 0.5);


cout %26lt;%26lt; "d:" %26lt;%26lt; temp %26lt;%26lt; endl;


temp = (int) (e + 0.5);


cout %26lt;%26lt; "e:" %26lt;%26lt; temp %26lt;%26lt; endl;


}

petal

No comments:

Post a Comment