Computer problems,Computer help
*AX SOFT>>>Programming & Design

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 << setprecision(0) << "Sum: " << a+b+c+d+e << 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.

The problem with using floor and ceiling is that we haven't learned about that in class yet. It doesn't seem to make sense to have to use something that we haven't gone over yet in class... Sigh, right now, Java seems so much better and easier.

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 << "Sum: " << x << endl;

'modified to "round to nearest integer"... not just cast to integer.

//Old casting trick, code not tested
#include <iostream>
int main(){
double a, b, c, d, e;
printf("\nInput a: ");
cin >> a;
printf("\nInput b: ");
cin >> b;
printf("\nInput c: ");
cin >> c;
printf("\nInput d: ");
cin >> d;
printf("\nInput e: ");
cin >> e;
int temp = (int) (a + 0.5);
cout << "a:" << temp << endl;
temp = (int) (b + 0.5);
cout << "b:" << temp << endl;
temp = (int) (c + 0.5);
cout << "c:" << temp << endl;
temp = (int) (d + 0.5);
cout << "d:" << temp << endl;
temp = (int) (e + 0.5);
cout << "e:" << temp << endl;
}

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<=a; b++)
start of for loop
int xyz=0; // counter
prompt user to enter any nmbr
cin>>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++

Tags
  General - Computers & Internet   Software   Security   Programming & Design   Facebook   Flickr   Google   MSN   MySpace
Related information
  • What form input type does this?

    checkboxes? radio buttons, you can only select 1 in a group pull down menus can only select 1 listboxes in VB you can select more than 1 what language would this be in? I initially thought...

  • How make your own website??

    WHAT YOU NEED; 1.Domain name & Hosting (to display site) needed. Buy it... ...

  • Can somebody tell me what the error is in this PHP line?

    looks like your missing a "," after the ssn: (line 13) if(isset($_REQUEST['agree'], $_REQUEST['firstname'], $_REQUEST['lastname'], $_REQUEST['address...

  • Photoshop. Need help on technique. Example inside.?

    Open any image you need with photoshop, zoom it at size you can edit it, use earser tool to remove the background, finally, duplicate the layer to another new one and change blending options to add...

  • How to make brighter, detailed photos in photoshop for website?

    curves, levels, and better images to begin with... at least more res maybe? the noise you're getting is because the images are too dark/ overexposed.

    ...
  • What is the linux command line for displaying a website?

    The original text-based browser was Lynx. If that's what you mean. I don't know how Gentoo installs software, but downloading (or checking for if it IS installed) is the first step. Then,...

  • I need to enter a string with a decimal to change to a double?

    When you say that Double.parseDouble does not work what exactly do you mean? Print the String before you do the parse. Add a character before and after like System.out.println("#" + ...

  • MS Word - themes & background applied to 1 page only ?

    Hi there, By default Word puts the watermark in the header and also by default the same header on each page of the document. In order to change that, after you have added your watermark to t...

  •  

    Categories--Copyright/IP Policy--Contact Webmaster