More C++
OttoDestruct
Join Date: 2002-11-08 Member: 7790Members
in Off-Topic
Erm.. ok this time the question is about rounding. I'm supposed to run the input from float to int back to float, and supposedly that will round it. Its not happening though. Heres my code.
<!--c1--></span><table border='0' align='center' width='95%' cellpadding='3' cellspacing='1'><tr><td><b>CODE</b> </td></tr><tr><td id='CODE'><!--ec1-->
// Program by Aaron Friedley
// October 12, 2003
// Program rounds input then finds ceiling and floor
#include <iostream.h>
#include <math.h>
#include <iomanip.h>
int main (void)
{
// Prototypes
float round (void);
void ceilingfloor (float);
// Declarations
float num1;
// Calls
num1 = round ( );
ceilingfloor (num1);
return 0; // Terminate main
}
// *** round function ***
float round (void)
{
// Declarations
float num1;
int num2;
float num3;
cout << "Program by Aaron Friedley MWF 10:00\n\n"
<< "Input number: ";
cin >> num1;
// Declarations
num2 = num1;
num3 = num2;
cout << setiosflags(ios::fixed) << setiosflags(ios::showpoint)
<< setprecision(2) << "Rounded: " << num3;
return num3; // Terminate round
}
// *** ceilingfloor function ***
void ceilingfloor (float num1)
{
cout << "\nCeiling: " << ceil (num1)
<< "\nFloor: " << floor (num1) << "\n";
return; // Terminate ceilingfloor
}
<!--c2--></td></tr></table><span class='postcolor'><!--ec2-->
Yes I realize coding in multiple functions for this is retarded, but thats how the professor wants this one done.
<!--c1--></span><table border='0' align='center' width='95%' cellpadding='3' cellspacing='1'><tr><td><b>CODE</b> </td></tr><tr><td id='CODE'><!--ec1-->
// Program by Aaron Friedley
// October 12, 2003
// Program rounds input then finds ceiling and floor
#include <iostream.h>
#include <math.h>
#include <iomanip.h>
int main (void)
{
// Prototypes
float round (void);
void ceilingfloor (float);
// Declarations
float num1;
// Calls
num1 = round ( );
ceilingfloor (num1);
return 0; // Terminate main
}
// *** round function ***
float round (void)
{
// Declarations
float num1;
int num2;
float num3;
cout << "Program by Aaron Friedley MWF 10:00\n\n"
<< "Input number: ";
cin >> num1;
// Declarations
num2 = num1;
num3 = num2;
cout << setiosflags(ios::fixed) << setiosflags(ios::showpoint)
<< setprecision(2) << "Rounded: " << num3;
return num3; // Terminate round
}
// *** ceilingfloor function ***
void ceilingfloor (float num1)
{
cout << "\nCeiling: " << ceil (num1)
<< "\nFloor: " << floor (num1) << "\n";
return; // Terminate ceilingfloor
}
<!--c2--></td></tr></table><span class='postcolor'><!--ec2-->
Yes I realize coding in multiple functions for this is retarded, but thats how the professor wants this one done.
Comments
First lesson, it ain't as easy as you think.
Second lesson, Error executing newb_coding_check_list due to segmentation fault 0x042ab9
Press any key to continue...
Just not rounding the number at all?
Just start putting Cout statements near all operations to pinpoint exactly where it is going wrong.
Like one after the operation to store the float into the int, then one after the int is put back into the float to see where it muffs up.
Another problem could be that none of the variables are initialized... IT's just a good habit to declare stuff "int moo_cow=0;"
"num1 = round ( );" could be the problem... I think there needs to be a parameter in that... Wait the entire function is like nothing i've done before... As it stands nothing is being passed into the function to work with.... I think you might need "float round (float num_to_round);" or something of the sort in the prototype, and then "num1= round(num1);" Perhaps.. but it's been awhile, haven't coded in almost two years >_< (stupid school)
If you had 6.5 for example and converted it to an int, instead of getting 7 (rounded up) you'd just get 6 <!--emo&:)--><img src='http://www.unknownworlds.com/forums/html/emoticons/smile.gif' border='0' style='vertical-align:middle' alt='smile.gif'><!--endemo-->
Just not rounding the number at all?
Just start putting Cout statements near all operations to pinpoint exactly where it is going wrong.
Like one after the operation to store the float into the int, then one after the int is put back into the float to see where it muffs up.
Another problem could be that none of the variables are initialized... IT's just a good habit to declare stuff "int moo_cow=0;"
"num1 = round ( );" could be the problem... I think there needs to be a parameter in that... Wait the entire function is like nothing i've done before... As it stands nothing is being passed into the function to work with.... I think you might need "float round (float num_to_round);" or something of the sort in the prototype, and then "num1= round(num1);" Perhaps.. but it's been awhile, haven't coded in almost two years >_< (stupid school) <!--QuoteEnd--></td></tr></table><span class='postcolor'><!--QuoteEEnd-->
Thats the entire point... it goes to that function, takes the input from there, then if you notice I have it set to return it to main and send it into the second function. The problem is that say if I typed in 1.55 it will be displayed as 1.00 and not 1.60
*Edit*
Geminosty I realize that, but the book itself says to do that in order to round. Once again I'll state that my book and my professor both suck.
One piece of good coding practice is to typecast stuff when you're converting it; this'll stop the annoying warnings you're no doubt getting.
Basically...
num2 = num1;
num3 = num2;
should be...
num2 = (int)num1;
num3 = (float)num2;
Doubt it'll fix your problem but any warning is a bad warning and should be fixed before you consider your code sound and stable <!--emo&:D--><img src='http://www.unknownworlds.com/forums/html/emoticons/biggrin.gif' border='0' style='vertical-align:middle' alt='biggrin.gif'><!--endemo-->
*Edit*
Didn't do anything, still no rounding.
You said that when you run the code and type in 5.55 you get 5.00 not 5.60. That exactly what will happen. Casting a floating point number to an integer (which is what you do implicitly in the num2 = num1 line) truncates the floating point value to an integer value.
If your textbook told you that casting a floating point value to an integer value will round it to arbitrary precision then your textbook completely out in left field. It will always round down to the nearest integer.
If goal of the assignment is to write code to round a floating point number down to arbitrary precision (or even something fixed like say tenths), then you've got your work cut out for you. I think you'll have to pick the number apart and subtract off everything of finer precision than you want. Or at least that's the quickest way I can think of to do it at the moment.
float round_me = 4.678;
int temp;
round_me = round_me * 100 + 0.5; // equivalent to: (round_me + 0.005)*100
temp = (int) round_me; // cut off everything after the decimal point
round_me = ((float) temp) / 100;
Anyway, CrystalSnake's code segment thingy should work, although it'd be even nicer if instead of using (int), you used static_cast<int>(<i>value you want to round down</i>)
static_casts are safer to use, in general. They're built for this very purpose.
Edit: Ugh! And why are you declaring your functions inside of main?
double std::floor(double x) will round the value down
double std::ceil(double x) will round the value up.
2: I can't see anything wrong with what your algorithm, but your compiler should be generating warnings when you try to assign the float to an int, because of loss of precision.
3: If you're going to reinvent the wheel, do it properly. The idea of making it a function is that you pass it the value to round, not that you split it into a function and ask for input in the function! The idea of putting something into a function is to make it reusable - if your rounding function takes the value as an argument it means later code can use it to round any values they like. The way you've got it, the round function will only ever get the values from cin. It's very limited, and I don't think it's what your professor had in mind if he said he wanted it to use a function.
4: Declaring a parameter list as void is a deprecated practise. That means: while it's still in the standard you aren't supposed to do it, and they may retract it from the standard. Dunno if that's how you're being taught, but it's bad practise. Likewise, #include <****.h> is deprecated as well; the c++ way is to #include <****> (without the .h) and then use the std namespace. If that's too confusing, never mind.
5: Er... why exactly are your prototypes inside main() ?
<!--QuoteBegin--></span><table border='0' align='center' width='95%' cellpadding='3' cellspacing='1'><tr><td><b>QUOTE</b> </td></tr><tr><td id='QUOTE'><!--QuoteEBegin-->Forgive me if I'm wrong but isn't main the last one you should mention? <!--QuoteEnd--></td></tr></table><span class='postcolor'><!--QuoteEEnd-->
You are. It doesn't matter <!--emo&:p--><img src='http://www.unknownworlds.com/forums/html/emoticons/tounge.gif' border='0' style='vertical-align:middle' alt='tounge.gif'><!--endemo-->.
The order of the functions should be with readability in mind. Functions can appear in any order, as long as their prototypes appear before they are used (and all functions except main() should have prototypes as a matter of course)
C++ doesn't mind it (And your compiler won't throw a wobbley) buts its good practice and makes your code easier to look at (Apparently)
double std::floor(double x) will round the value down
double std::ceil(double x) will round the value up.
2: I can't see anything wrong with what your algorithm, but your compiler should be generating warnings when you try to assign the float to an int, because of loss of precision.
3: If you're going to reinvent the wheel, do it properly. The idea of making it a function is that you pass it the value to round, not that you split it into a function and ask for input in the function! The idea of putting something into a function is to make it reusable - if your rounding function takes the value as an argument it means later code can use it to round any values they like. The way you've got it, the round function will only ever get the values from cin. It's very limited, and I don't think it's what your professor had in mind if he said he wanted it to use a function.
4: Declaring a parameter list as void is a deprecated practise. That means: while it's still in the standard you aren't supposed to do it, and they may retract it from the standard. Dunno if that's how you're being taught, but it's bad practise. Likewise, #include <****.h> is deprecated as well; the c++ way is to #include <****> (without the .h) and then use the std namespace. If that's too confusing, never mind.
5: Er... why exactly are your prototypes inside main() ?
<!--QuoteBegin--></span><table border='0' align='center' width='95%' cellpadding='3' cellspacing='1'><tr><td><b>QUOTE</b> </td></tr><tr><td id='QUOTE'><!--QuoteEBegin-->Forgive me if I'm wrong but isn't main the last one you should mention? <!--QuoteEnd--></td></tr></table><span class='postcolor'><!--QuoteEEnd-->
You are. It doesn't matter <!--emo&:p--><img src='http://www.unknownworlds.com/forums/html/emoticons/tounge.gif' border='0' style='vertical-align:middle' alt='tounge.gif'><!--endemo-->.
The order of the functions should be with readability in mind. Functions can appear in any order, as long as their prototypes appear before they are used (and all functions except main() should have prototypes as a matter of course) <!--QuoteEnd--> </td></tr></table><span class='postcolor'> <!--QuoteEEnd-->
1) Thats what she told us to do. She follows the book to the letter, and unfortunately the book is ancient and poorly written, as well as the code inside.
2) I'm not getting any. Course then again I'm using a POS compiler inside a unix environment that wont even recognize half of the iomanip library, and I have to add parameters on the end of it to recognize the math library. i.e. when I compile the line to do it looks like this: cxx hwk06.cxx -lm
3) That function is the way she wants it. The idea is that main calls the function, the function returns the value, then main sends that value over to the ceilingfloor function.
4) If I change how professor doesnt want it done thats 50% off homework right there. Many people have informed me of what you say here.
5) See above
Yes I know its pointless, but once again this is how she wants it done, and I think shes a friggin idiot. Good thing I'm transferring university after this year, and possibly even semester. I asked her if we could just use an if then statement and shes like no that wouldn't be fair to the people who don't know how to do it. I said it wouldn't be fair to me seeing as I have knowledge of it, and would save me all this trouble.
Any class that holds you back for any reason, whether it's for slower people to catch up or whatever is a bad class.
In playstation programming I was always racing ahead of the current lesson; eager to learn how to make cool stuff and the professer was almost always willing to tell me about the code I needed or show me where I could get it to do so.
The best learning is done at <b>Your</b> pace, not some timetable set by clueless teachers <!--emo&:p--><img src='http://www.unknownworlds.com/forums/html/emoticons/tounge.gif' border='0' style='vertical-align:middle' alt='tounge.gif'><!--endemo-->
You poor **** <!--emo&:(--><img src='http://www.unknownworlds.com/forums/html/emoticons/sad.gif' border='0' style='vertical-align:middle' alt='sad.gif'><!--endemo-->
ie 2.4 should round down to 2, and 2.4+0.5=2.9 which trunc's to 2
2.6 should round up to 3, and 2.6+0.5 = 3.1 which trunc's to 3!
...i think this is what crystal snake was trying to say, but im not sure..
{
return (x-int(x))>=0.5?int(x)+1:int(x);
}
Can't remember why but back in school the 5 would round the number next to it up 1, which in the case makes it 0.5 which rounds to 1.
But then again... it's less than 0.5 to start with so shouldn't it be 0? ~blink~
I'm lost @.@
Can't remember why but back in school the 5 would round the number next to it up 1, which in the case makes it 0.5 which rounds to 1.
But then again... it's less than 0.5 to start with so shouldn't it be 0? ~blink~
I'm lost @.@<!--QuoteEnd--></td></tr></table><span class='postcolor'><!--QuoteEEnd-->
The purpose of rounding: to represent a number with fewer digits, while keeping the error (difference between the original number and the rounded number) as small as possible.
Before you round a number, you have to decide how many digits you want the rounded number to have.
f = 1.67134
f rounded to the next integer = 2
f rounded to 1 digit after the decimal point = 1.7
f rounded to 2 digits after the decimal point = 1.67
The whole concept of computer science is the ability to copy and reuse code!
99% of everything out there has been written already! The 1% you have to do is figuring out what order to put everything in.
yes its annoying to have to write basic ****, but at least we get to sleep in class.
The whole concept of computer science is the ability to copy and reuse code!
99% of everything out there has been written already! The 1% you have to do is figuring out what order to put everything in. <!--QuoteEnd--> </td></tr></table><span class='postcolor'> <!--QuoteEEnd-->
I pointed this out myself, and got an answer. How about scrolling up and reading it?