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