943,822 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 465
  • C++ RSS
Mar 3rd, 2009
0

Please Help with Rounding program

Expand Post »
Below is the code which is supposed to round to two decimal places, i.e. 1.2874 is suppose to round to 1.29, but rather gives me 128.0000. What am I doing wrong need help fast!

#include <iomanip>
#include <iostream>
#include <cmath>
using namespace std;

double roundIt(double x, double n)
{
x = floor( x * pow(10.0, n) + 0.5 / pow(10.0, n));
return x;
}


int main()
{
float x;

cout << setprecision(4) << fixed << showpoint;
cout << "Enter number a positive number with a fractional part" << endl;
cout << "more than 2 decimal places, i.e. 1.2574, to be" << endl;
cout << "rounded to two deciaml places." << endl;
cin >> x;

cout << "The rounded answer for: " << x << "is: " << roundIt(x, 2)<< endl;


cout << "\n\n\n\n";
cout << "Jordan McGehee " << endl;
cout << "Due 03-03-08 " << endl;



system ("pause");


return 0;
}//end main
Similar Threads
Reputation Points: 17
Solved Threads: 0
Junior Poster in Training
songweaver is offline Offline
80 posts
since Mar 2009
Mar 3rd, 2009
0

Re: Please Help with Rounding program

here's your problem:
C++ Syntax (Toggle Plain Text)
  1. x = floor( x * pow(10.0, n) + 0.5 / pow(10.0, n));
  2. return x;
you forgot to divide x with 10^n after you finished rounding
and im not quite sure what is that 0.5 / pow (10.0, n) doing

and dont forget that putting tags such as reply quickly, fast, ASAP and so on wont help you solve the problem
Last edited by Alibeg; Mar 3rd, 2009 at 5:11 pm.
Reputation Points: 11
Solved Threads: 11
Junior Poster in Training
Alibeg is offline Offline
81 posts
since Aug 2008
Mar 3rd, 2009
0

Re: Please Help with Rounding program

I know that has to work, it was given to me exactly by my teacher, there must be something else wrong.
Reputation Points: 17
Solved Threads: 0
Junior Poster in Training
songweaver is offline Offline
80 posts
since Mar 2009
Mar 3rd, 2009
0

Re: Please Help with Rounding program

did you check if you wrote it the same way as your teacher said?
maybe you are missing some part of it.
(maybe teacher is wrong? XD joke)

do you have to use exactly the same algorithm as your teacher said, if not then try something like this:
C++ Syntax (Toggle Plain Text)
  1. int a = x*pow(10, n+1);
  2. if ((a % 10) >= 5) a += 10- (a%10);
  3. a = floor(a / pow(10, n+1));
  4. return a;
Last edited by Alibeg; Mar 3rd, 2009 at 5:29 pm.
Reputation Points: 11
Solved Threads: 11
Junior Poster in Training
Alibeg is offline Offline
81 posts
since Aug 2008
Mar 3rd, 2009
0

Re: Please Help with Rounding program

I don't think there are any if statements in it.
Reputation Points: 17
Solved Threads: 0
Junior Poster in Training
songweaver is offline Offline
80 posts
since Mar 2009
Mar 3rd, 2009
0

Re: Please Help with Rounding program

You missed it by one closing parenthesis.

Your code:
C++ Syntax (Toggle Plain Text)
  1. x = floor( x * pow(10.0, n) + 0.5 / pow(10.0, n));
multiplies the number by a power of ten, then adds (.5 divided by that power of 10). That whole quantity is then passed to the floor( ) function. Thus you end up with a quantity that is just the original value multiplied by the power of ten, and truncated.

Remember your order of operator precedence.

The closing parenthesis needs to be after the addition of .5, so that you then divide by the power of ten, not by a very small number. Then apply the floor function. Like so:
C++ Syntax (Toggle Plain Text)
  1. x = floor( x * pow(10.0, n) + 0.5) / pow(10.0, n);
Reputation Points: 1268
Solved Threads: 228
Posting Virtuoso
vmanes is offline Offline
1,895 posts
since Aug 2007
Mar 3rd, 2009
0

Re: Please Help with Rounding program

vmanes! You are greatness! Thanks!
Reputation Points: 17
Solved Threads: 0
Junior Poster in Training
songweaver is offline Offline
80 posts
since Mar 2009
Mar 3rd, 2009
0

Re: Please Help with Rounding program

I don't know about greatness, but I can be pretty good.

Next time you come visit us, please post your code inside the tags
[code]
your code goes here
[/code]

That will make it easier for people to read. Enjoy!
Reputation Points: 1268
Solved Threads: 228
Posting Virtuoso
vmanes is offline Offline
1,895 posts
since Aug 2007
Mar 4th, 2009
0

Re: Please Help with Rounding program

Reputation Points: 12
Solved Threads: 8
Junior Poster in Training
kbshibukumar is offline Offline
65 posts
since Jan 2009

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: hi I have done my program like below ,but not run & edit program
Next Thread in C++ Forum Timeline: Problem with Double Linked Lists





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC