C++ rounding to nearest multiple of 5

Discussion in 'Programming & Software Development' started by ring wraith, Mar 6, 2006.

  1. OP
    OP
    ring wraith

    ring wraith Member

    Joined:
    Jan 18, 2002
    Messages:
    2,120
    Location:
    A.K.A Frank
    figured it out :p

    logic for me went

    first round up any decimal points

    then take the number, then using remainders and an if statement to find the closest multiple of 5.

    i wont post code, since (no offense to the maquarie guys here) but I dont wanna be burnt by plagarising.
     
  2. OP
    OP
    ring wraith

    ring wraith Member

    Joined:
    Jan 18, 2002
    Messages:
    2,120
    Location:
    A.K.A Frank
    well bugger it, heres my solution to the issue

    first turn it into a whole number
    numberToRound = ceil(x-0.5)

    ensure it needs further rounding
    if (numberToRound%5 != 0)

    then the statement to round to the closest multiple of 5

    int rem, x;
    rem = numberToRound%5;
    if (rem > 2)
    {
    x=5-rem;
    numberToRound = numberToRound+x;
    }
    else
    {
    numberToRound = numberToRound-rem;
    }


    and it works too (thats the important part)

    the problem is that the lecturer would prefer a solution that DOESNT use if statements... and thats something thats got me completely baffled. any suggestions on that (mind you im sticking with this solution as im pretty happy with it :p)
     
    Last edited: Mar 21, 2006
  3. Ze.

    Ze. Member

    Joined:
    Sep 13, 2003
    Messages:
    7,871
    Location:
    Newcastle, NSW
    How does it handle negative numbers? :)
    If you don't have to handle negative numbers then its a piece of piss :) if you do then i don't think it can be done :)

    Camh's solution handles positive numbers without an if statement.
     
  4. OP
    OP
    ring wraith

    ring wraith Member

    Joined:
    Jan 18, 2002
    Messages:
    2,120
    Location:
    A.K.A Frank
    no negative numbers need be calculated.

    im gonna stew on this a bit more today.
     
  5. camh

    camh Member

    Joined:
    May 23, 2002
    Messages:
    140
    Location:
    Sydney
    You can cheat and use the ternary operator ?: - its an if but not an if :)

    int n = static_cast<int>(rounf(f))
    return ((n+2*(n ? n/abs(n) : 1))/5)*5)

    Assuming sane division semantics with negative numerator. Otherwise you'll need to use div(3).

    Omit the first line for integer input.

    It's essentially the same as my first post, except for negative numbers you need to subtract 2 instead of adding 2. The expression n/abs(n) yields -1 or 1, which can be used to reverse the sign of +2 for negative inputs. Unfortunately n==0 needs to be handled without a divide by zero needing the ternary operator ?:
     
  6. OP
    OP
    ring wraith

    ring wraith Member

    Joined:
    Jan 18, 2002
    Messages:
    2,120
    Location:
    A.K.A Frank
    yea, see they say, sure use any solution BUT we PREFER (will award marks) if you code a solution using ONLY what you've learnt in lectures thus far...

    no typecasting, no if statements to name a few.

    heck, it works, and Im happy.
     
  7. Pacifist

    Pacifist Member

    Joined:
    Mar 10, 2002
    Messages:
    1,949
    Location:
    Gosford
    Hmm no if statements and no typecasting, i like a challenge

    Try this - i haven't simplified it and watch the brackets

    ( ( ( (x+2)/5 ) - ( x/5 ) ) + ( x / 5 ) ) * 5

    Only works with unsigned integers though
     
    Last edited: Mar 25, 2006
  8. Ze.

    Ze. Member

    Joined:
    Sep 13, 2003
    Messages:
    7,871
    Location:
    Newcastle, NSW
    The -x/5 and the +x/5 will cancel each other out leaving you with camh's code on the first page :)

    Just like to add awake for over 42 hours and it stands out like a sore thumb.
     
  9. Pacifist

    Pacifist Member

    Joined:
    Mar 10, 2002
    Messages:
    1,949
    Location:
    Gosford
    Heh i was up waaay to late last night and had a few beers, i logged on now because i just relised what i had posted was really stupid and needed changing - anyway back to bed for me :p
     
  10. OP
    OP
    ring wraith

    ring wraith Member

    Joined:
    Jan 18, 2002
    Messages:
    2,120
    Location:
    A.K.A Frank
    the final solution ended up being something like


    floor((number/5)+0.5)*5

    yea... I cant believe its that simple lol (but im awful at maths)
     

Share This Page

Advertisement: