% absolute1() determines the absolute value using if-then-else
% to check if the number is positive or negative. If it is
% negative, then multiply by -1
function absolute1 (number : real) : real
if (number >= 0) then
result number
else
result number * (-1)
end if
end absolute1
% absolute2() determines the square root by squaring the
% number and then taking the square root.
function absolute2 (number : real) : real
result sqrt (number ** 2)
end absolute2
% a program to test the absolute() functions
% the program will continue to ask for numbers until
% the user enters a value that is not a number
var input : string
var number : real
put "To quit, enter something that is not a number"
put "---------------------------------------------"
loop
put "Please enter your number: " ..
get input
if strrealok (input) then
% change input from a string to real
number := strreal (input)
put "absolute value using if-then-else: " ..
put absolute1(number)
put "absolute value using square & root: " ..
put absolute2(number)
put ""
end if
exit when (strrealok (input) = false)
end loop
put "End of program"
Comments (0)
You don't have permission to comment on this page.