More C++

OttoDestructOttoDestruct Join Date: 2002-11-08 Member: 7790Members
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.

Comments

  • HawkeyeHawkeye Join Date: 2002-10-31 Member: 1855Members
    Welcome to the wide world of coding.

    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...
  • NumbersNotFoundNumbersNotFound Join Date: 2002-11-07 Member: 7556Members
    edited October 2003
    So how isn't it working?

    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)
  • GeminosityGeminosity :3 Join Date: 2003-09-08 Member: 20667Members
    unfortunately if you convert a float to an int and back again it won't round; it'll just chop off all the parts after the decimal lol.
    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-->
  • OttoDestructOttoDestruct Join Date: 2002-11-08 Member: 7790Members
    edited October 2003
    <!--QuoteBegin--404NotFound+Oct 12 2003, 05:03 PM--></span><table border='0' align='center' width='95%' cellpadding='3' cellspacing='1'><tr><td><b>QUOTE</b> (404NotFound @ Oct 12 2003, 05:03 PM)</td></tr><tr><td id='QUOTE'><!--QuoteEBegin--> So how isn't it working?

    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.
  • GeminosityGeminosity :3 Join Date: 2003-09-08 Member: 20667Members
    Not that it matters but I usually declare prototypes outside of main like global variables =3

    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-->
  • OttoDestructOttoDestruct Join Date: 2002-11-08 Member: 7790Members
    edited October 2003
    I get 0 warnings with what I copy pasted when I compile but I'll try that.

    *Edit*

    Didn't do anything, still no rounding.
  • SpookSpook Join Date: 2003-07-29 Member: 18542Members
    I get a few warnings when I copy and paste and compile what you posted (namely a deprecated header warning, an assignment from float to int warning and an assignment from int to float warning) - but that's neither here nor there.

    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.
  • CrystalSnakeCrystalSnake Join Date: 2002-01-27 Member: 110Members
    So you want to round a float to the nearest 2 digits, and you're required to convert it to an integer and back. Why not do it like this:

    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;
  • FieariFieari Join Date: 2002-10-22 Member: 1566Members, Constellation
    edited October 2003
    If I were your C++ teacher, I'd be harping on you for putting the input of your program in the function there. However, since I'm not...

    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?
  • VenmochVenmoch Join Date: 2002-08-07 Member: 1093Members
    Forgive me if I'm wrong but isn't main the last one you should mention?
  • SoulSkorpionSoulSkorpion Join Date: 2002-04-12 Member: 423Members
    edited October 2003
    1: Why does your professor keep setting you exercises which are a re-writing of the standard library?

    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)
  • VenmochVenmoch Join Date: 2002-08-07 Member: 1093Members
    Meh my learning book told me all functions should be in front of main.

    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)
  • CreepieCreepie Join Date: 2003-02-19 Member: 13734Members
    Numbers in C++ are great. They never quite behave as you want them. Rounding is a classic - truncation city.
  • OttoDestructOttoDestruct Join Date: 2002-11-08 Member: 7790Members
    <!--QuoteBegin--SoulSkorpion+Oct 13 2003, 04:09 AM--></span><table border='0' align='center' width='95%' cellpadding='3' cellspacing='1'><tr><td><b>QUOTE</b> (SoulSkorpion @ Oct 13 2003, 04:09 AM)</td></tr><tr><td id='QUOTE'><!--QuoteEBegin--> 51: Why does your professor keep setting you exercises which are a re-writing of the standard library?

    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.
  • GeminosityGeminosity :3 Join Date: 2003-09-08 Member: 20667Members
    Sounds like you really need to get out of there lol.
    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-->
  • SoulSkorpionSoulSkorpion Join Date: 2002-04-12 Member: 423Members
    <!--QuoteBegin--OttoDestruct+Oct 13 2003, 08:29 PM--></span><table border='0' align='center' width='95%' cellpadding='3' cellspacing='1'><tr><td><b>QUOTE</b> (OttoDestruct @ Oct 13 2003, 08:29 PM)</td></tr><tr><td id='QUOTE'><!--QuoteEBegin--> 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. <!--QuoteEnd--> </td></tr></table><span class='postcolor'> <!--QuoteEEnd-->
    You poor **** <!--emo&:(--><img src='http://www.unknownworlds.com/forums/html/emoticons/sad.gif' border='0' style='vertical-align:middle' alt='sad.gif'><!--endemo-->
  • Umbraed_MonkeyUmbraed_Monkey Join Date: 2002-11-25 Member: 9922Members
    heres a simple trick for this I learned a while ago. if you want to round instead of truncating it, add 0.5 to the number before converting it to int.

    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..
  • FireStormFireStorm Join Date: 2002-11-06 Member: 7390Members
    inline float my_round(float x)
    {
    return (x-int(x))>=0.5?int(x)+1:int(x);
    }
  • GeminosityGeminosity :3 Join Date: 2003-09-08 Member: 20667Members
    One thing that always bothered me about rounding... sure, everyone knows that 0.5 rounds up to 1, but what about 0.45?
    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 @.@
  • OttoDestructOttoDestruct Join Date: 2002-11-08 Member: 7790Members
    Yea I figured out that I needed to multiply by 100, add .5, change to int then back to float and divide by 100. Thanks for help though. Next assignments are EASY. One is to take the inputs and find taxes on salary, then the one after that is converting farenheit to celcius.
  • CrystalSnakeCrystalSnake Join Date: 2002-01-27 Member: 110Members
    <!--QuoteBegin--Geminosity+Oct 13 2003, 05:36 PM--></span><table border='0' align='center' width='95%' cellpadding='3' cellspacing='1'><tr><td><b>QUOTE</b> (Geminosity @ Oct 13 2003, 05:36 PM)</td></tr><tr><td id='QUOTE'><!--QuoteEBegin-->One thing that always bothered me about rounding... sure, everyone knows that 0.5 rounds up to 1, but what about 0.45?
    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
  • HawkeyeHawkeye Join Date: 2002-10-31 Member: 1855Members
    Why must computer science students be compelled to write standard library code?

    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.
  • ZelZel Join Date: 2003-01-27 Member: 12861Members
    because theyre LEARNING the BASICS. we write simple code to teach students basic stuff, you cant jsut jump into massive data structures.

    yes its annoying to have to write basic ****, but at least we get to sleep in class.
  • TenSixTenSix Join Date: 2002-11-09 Member: 7932Members
    This is why Im majoring in Electrical Engineering, theres a formula for everything, as long as you have a scientific calculator you can't go wrong! <!--emo&:D--><img src='http://www.unknownworlds.com/forums/html/emoticons/biggrin.gif' border='0' style='vertical-align:middle' alt='biggrin.gif'><!--endemo-->
  • SoulSkorpionSoulSkorpion Join Date: 2002-04-12 Member: 423Members
    <!--QuoteBegin--Hawkeye+Oct 14 2003, 01:16 AM--></span><table border='0' align='center' width='95%' cellpadding='3' cellspacing='1'><tr><td><b>QUOTE</b> (Hawkeye @ Oct 14 2003, 01:16 AM)</td></tr><tr><td id='QUOTE'><!--QuoteEBegin--> Why must computer science students be compelled to write standard library code?

    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?
Sign In or Register to comment.