Rounding Up

This is a discussion on Rounding Up within the C Programming forums, part of the General Programming Boards category; Hi, I'm new to c code and have got quite alot to learn. im currently working on a uni assignment ...

  1. #1
    Registered User
    Join Date
    Sep 2012
    Posts
    1

    Rounding Up

    Hi,
    I'm new to c code and have got quite alot to learn.
    im currently working on a uni assignment have got an issue with interpreting some pseudocode that i have been given that i have to put into c code.
    << Round up to n decimal points >>
    FUNCTION roundUP(value, decimalPlaces)
    BEGIN
    SET magnitude = 1
    FOR d = 0 TO decimalPlaces
    magnitude = magnitude * 10
    END FOR

    value = value * magnitude
    value = value + 0.5
    value = value / magnitude
    RETURN value
    END

    i have got some code that finds the median in an odd number of columns and i know i use the function above to round up to the middle column.
    my problem is im having trouble understanding how this pseudocode works and also how i go about interpreting it into c code.
    any help would be great.

    thanks

    anthony

  2. #2
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,675
    The formula is actually the pseudocode ,so let's analyze the pseudocode

    Code:
    FUNCTION roundUP(value, decimalPlaces)
    the prototype of the function...it's name is roundUP and it has two arguments,
    you have to think what type they are..int,char,double,what?
    Also do not forget that every function has a return value.
    Actually when a function returns nothing the returning type is void
    .If you can not decide now what the function shall 
    return wait until we reach at the end of the function
    BEGIN
    Every function(even main) starts with a left bracket {
    SET magnitude = 1
    We need a factor for rounding up,that is called magnitude..
    So we have to declare a variable with this name and set it to 1,thus assign the value one to the variable.
    Again take a second and think what the type of this variable is
    FOR d = 0 TO decimalPlaces
    Here we have a loop.Actually the pseudocode implies to you that you should use the for-loop.
    How many times will the loop be executed?We know,that's why we use for,decimalPlaces times.
    Mind that we need to declare a variable that actually plays the role of the counter(counts how many loops are going to be made).
    The counter here is d( shall we declare it int? ;) ) For loop - Wikipedia, the free encyclopedia 
    As you can see in the link we need to have a condition,that which be used as a flag for the loop,to know when
     it has to end(else we have an infinite loop).When the condition turns false then the loop is terminated.
    The condition usually contains the counter(variable d here).
    We want the loop to be executed decimalPlaces times..so we set d to 0 and we must find a condition that makes the loop to be executed exactly decimalPlaces times.
    So the condition shall be d<...Find what you should replace in the ...
    magnitude = magnitude * 10
    Multiply variable magnitude by 10
    END FOR
    Remember to wrap the body of the loop inside brackets 
    {/*i am the body of the loop and i feel great*/}
    
    value = value * magnitude
    value = value + 0.5
    value = value / magnitude
    You understand them
    RETURN value
    Oh,look!The function returns something,so the return value we discussed at the start is surely not void!What it is then?
    It must be the type of value...
    If you declare value as char the return value should be char..
    Of course you will now declare value as char,but as ...
    Again replace the ...
    END
    Every function ends with a right bracket  }
    Tip:You may wrap your code in order to be shown more clear.Simply type [key]/*Hi,i am the code of anthony*/[/key] .Replace key with code in order this to work
    Last edited by std10093; 09-03-2012 at 05:17 PM.

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    I detect awesome Linux-speak!! ^^^

    I just WISH TO HEAVEN, that Mr. Linux had not lived next door to Mr. Verbose, for so many years! Gives me the heebie-jeebies.

  4. #4
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,675
    Quote Originally Posted by Adak View Post
    I detect awesome Linux-speak!! ^^^

    I just WISH TO HEAVEN, that Mr. Linux had not lived next door to Mr. Verbose, for so many years! Gives me the heebie-jeebies.
    If this goes for me , i do not seem to get the point :/

  5. #5
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by std10093 View Post
    If this goes for me , i do not seem to get the point :/
    It's meant to be a humorous compliment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with rounding INT's
    By iAmFedor in forum C++ Programming
    Replies: 2
    Last Post: 05-19-2008, 04:03 PM
  2. Rounding
    By mdoland in forum C# Programming
    Replies: 2
    Last Post: 10-04-2007, 01:18 AM
  3. rounding
    By limitmaster in forum C++ Programming
    Replies: 8
    Last Post: 06-24-2006, 11:00 PM
  4. need help with rounding
    By blindleaf in forum C Programming
    Replies: 1
    Last Post: 02-20-2003, 02:49 PM
  5. rounding
    By Kings in forum C++ Programming
    Replies: 3
    Last Post: 02-11-2003, 02:14 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21