Please Help with Rounding program

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Mar 2009
Posts: 56
Reputation: songweaver is an unknown quantity at this point 
Solved Threads: 0
songweaver songweaver is offline Offline
Junior Poster in Training

Please Help with Rounding program

 
0
  #1
Mar 3rd, 2009
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
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 71
Reputation: Alibeg is an unknown quantity at this point 
Solved Threads: 10
Alibeg Alibeg is offline Offline
Junior Poster in Training

Re: Please Help with Rounding program

 
0
  #2
Mar 3rd, 2009
here's your problem:
  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.
Reply With Quote Quick reply to this message  
Join Date: Mar 2009
Posts: 56
Reputation: songweaver is an unknown quantity at this point 
Solved Threads: 0
songweaver songweaver is offline Offline
Junior Poster in Training

Re: Please Help with Rounding program

 
0
  #3
Mar 3rd, 2009
I know that has to work, it was given to me exactly by my teacher, there must be something else wrong.
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 71
Reputation: Alibeg is an unknown quantity at this point 
Solved Threads: 10
Alibeg Alibeg is offline Offline
Junior Poster in Training

Re: Please Help with Rounding program

 
0
  #4
Mar 3rd, 2009
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:
  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.
Reply With Quote Quick reply to this message  
Join Date: Mar 2009
Posts: 56
Reputation: songweaver is an unknown quantity at this point 
Solved Threads: 0
songweaver songweaver is offline Offline
Junior Poster in Training

Re: Please Help with Rounding program

 
0
  #5
Mar 3rd, 2009
I don't think there are any if statements in it.
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 1,679
Reputation: vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold 
Solved Threads: 193
vmanes's Avatar
vmanes vmanes is offline Offline
Posting Virtuoso

Re: Please Help with Rounding program

 
0
  #6
Mar 3rd, 2009
You missed it by one closing parenthesis.

Your code:
  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:
  1. x = floor( x * pow(10.0, n) + 0.5) / pow(10.0, n);
Everyone's gotta believe in something. I believe I'll have another drink.
~~~~~~~~~~~~~~~~~~
Looking for an exciting graduate degree? Robotics and Intelligent Autonomous Systems (RIAS) at SDSM&T See the program brochure here.
Reply With Quote Quick reply to this message  
Join Date: Mar 2009
Posts: 56
Reputation: songweaver is an unknown quantity at this point 
Solved Threads: 0
songweaver songweaver is offline Offline
Junior Poster in Training

Re: Please Help with Rounding program

 
0
  #7
Mar 3rd, 2009
vmanes! You are greatness! Thanks!
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 1,679
Reputation: vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold 
Solved Threads: 193
vmanes's Avatar
vmanes vmanes is offline Offline
Posting Virtuoso

Re: Please Help with Rounding program

 
0
  #8
Mar 3rd, 2009
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!
Everyone's gotta believe in something. I believe I'll have another drink.
~~~~~~~~~~~~~~~~~~
Looking for an exciting graduate degree? Robotics and Intelligent Autonomous Systems (RIAS) at SDSM&T See the program brochure here.
Reply With Quote Quick reply to this message  
Join Date: Jan 2009
Posts: 46
Reputation: kbshibukumar is an unknown quantity at this point 
Solved Threads: 7
kbshibukumar kbshibukumar is offline Offline
Light Poster

Re: Please Help with Rounding program

 
0
  #9
Mar 4th, 2009
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC