| 
  • If you are citizen of an European Union member nation, you may not use this service unless you are at least 16 years old.

  • You already know Dokkio is an AI-powered assistant to organize & manage your digital files & messages. Very soon, Dokkio will support Outlook as well as One Drive. Check it out today!

View
 

Functions - 8a Turing Solution

Page history last edited by Steve Sweeney 15 years, 1 month ago

% 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.