2

This is the question.

write a program that prompts the user to input five decimal numbers. the program should then add the five decimal numbers, convert the sum to the nearest integer,m and print the result.

This is what I've gotten so far:

// p111n9.cpp : Defines the entry point for the console application.
//

#include <iostream>
using namespace std;

double a, b , c , d , e, f;

int main(int argc, char* argv[])
{
 cout << "enter 5 decimals: " << endl;
 cin >> a >> b >> c >> d >> e;
 f = a + b + c + d + e;
 return 0;
}

Now I just need to convert the sum(f) to the nearest integer, m and print the result. How do I do this?

N 1.1
  • 12,418
  • 6
  • 43
  • 61
SkyBoxer
  • 103
  • 1
  • 2
  • 5
  • 5
    Sounds like homework to me. Remove the tag if I'm wrong. – Potatoswatter Apr 08 '10 at 21:34
  • 2
    What's the question? You've given a problem description but not how you are stuck... – Billy ONeal Apr 08 '10 at 21:35
  • 1
    Ok. I modified it. The question was hiding. This should stay open now because he has shown what he was attempted.. he just needs to know how to typecast and such – Earlz Apr 08 '10 at 21:37
  • With the addition of an actual question, this has essentially become a duplicate of: http://stackoverflow.com/questions/485525/round-for-float-in-c – Tyler McHenry Apr 08 '10 at 21:41
  • @Tyler: Those answers are a little bizarre… most implementations provide a `round` function, I don't think we should be directing anyone (especially a newbie) to actually implement `round`. – Potatoswatter Apr 08 '10 at 22:02
  • What is "convert the sum (of a float) to the nearest integer" if not a round function? – Tyler McHenry Apr 09 '10 at 00:07

3 Answers3

1

"declare m" means say

int m;

if you say

m = (int)f; // it means the int value of f is assigned to m.

The casting is actually not even necessary here:

m=f; //works just as well

now you can print m

cout<<m;
Stef
  • 446
  • 1
  • 4
  • 19
  • 1
    This truncates (aka, rounds down). round(f) from #include is better, but your solution does provide an insight into how one might go about implementing a rounding function. – Niki Yoshiuchi Apr 08 '10 at 22:25
0

You need to

  1. Declare m. A declaration looks like this: double a, b , c , d , e, f;
  2. Call a rounding function or change the default rounding that occurs when you use =.
    • Assigning a double value to an int variable rounds down by default. You can change the default with fesetround( FE_TONEAREST ); from #include <fenv.h>.
    • Alternately you can call the function round from #include <cmath>.
    • Neither of these functions are currently strictly standard C++; they are standard C and are scheduled to become standard C++.
  3. Add a statement to print m, using cout and <<.
Potatoswatter
  • 134,909
  • 25
  • 265
  • 421
  • thank you for the reply but i am totally newbie at this computer science... i cant understand :( – SkyBoxer Apr 08 '10 at 21:40
  • 1
    @user: are you taking a class or teaching yourself? You should refer to class notes instead of Stack Overflow if you have them. – Potatoswatter Apr 08 '10 at 21:55
0

This is the answer:

// p111n9.cpp : Defines the entry point for the console application.
//

#include <iostream>
using namespace std;

double a, b , c , d , e, f;

int main(int argc, char* argv[])
{
 cout << "enter 5 decimals: " << endl;
 cin >> a >> b >> c >> d >> e;
 f = a + b + c + d + e;
 cout << static_cast<int> (f + .5)<< endl;
 return 0;
}

By type casting from double to integer the decimal is being truncated AFTER the .5 is added. This means if you have a number like 13.4 and you add .5 to it and then truncate the decimal it will round down. If you have 13.6 and add .5 and then truncate the decimal it will be 14. That is how you round up and down with type casting.

jonf
  • 1