| | |
Please Help with Rounding program
Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved |
•
•
Join Date: Mar 2009
Posts: 56
Reputation:
Solved Threads: 0
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
#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
•
•
Join Date: Aug 2008
Posts: 71
Reputation:
Solved Threads: 10
here's your problem:
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
C++ Syntax (Toggle Plain Text)
x = floor( x * pow(10.0, n) + 0.5 / pow(10.0, n)); return x;
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.
•
•
Join Date: Aug 2008
Posts: 71
Reputation:
Solved Threads: 10
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:
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)
int a = x*pow(10, n+1); if ((a % 10) >= 5) a += 10- (a%10); a = floor(a / pow(10, n+1)); return a;
Last edited by Alibeg; Mar 3rd, 2009 at 5:29 pm.
You missed it by one closing parenthesis.
Your code:
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:
Your code:
C++ Syntax (Toggle Plain Text)
x = floor( x * pow(10.0, n) + 0.5 / pow(10.0, n));
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)
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.
~~~~~~~~~~~~~~~~~~
Looking for an exciting graduate degree? Robotics and Intelligent Autonomous Systems (RIAS) at SDSM&T See the program brochure here.
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!
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.
~~~~~~~~~~~~~~~~~~
Looking for an exciting graduate degree? Robotics and Intelligent Autonomous Systems (RIAS) at SDSM&T See the program brochure here.
![]() |
Similar Threads
- Rounding up problem (C++)
- Candidate Program (C++)
- Newbie needs initial direction - Warehouse inventory program (VB.NET)
- currency rounding difficulties (VB.NET)
- Rounding floating points (C)
- Number Rounding (Java)
- I need help with a program (C++)
- SLOT MACHINE PROGRAM... Help plsss! (C)
Other Threads in the C++ Forum
- Previous Thread: hi I have done my program like below ,but not run & edit program
- Next Thread: Problem with Double Linked Lists
| Thread Tools | Search this Thread |
api array arrays beginner binary bitmap c++ c/c++ calculator char char* class classes coding compile compiler console conversion convert count data database delete desktop developer directshow dll dynamiccharacterarray email encryption error file forms fstream function functions game generator getline google graph homeworkhelper iamthwee ifstream input int integer java lib linkedlist linux list loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference return rpg sorting string strings struct template templates test text tree unix url vector video visualstudio win32 windows winsock word wordfrequency wxwidgets






