- mystery
- a and b
- int
- function mystery (a, b : real) : int
- If the first parameter is smaller than the second it returns -1, if it is bigger returns 1 and
if they are equal it returns 0
% returns the argument that is smaller (comes first in ASCII)
function first (a, b : char) : char
if a < b then
result a
else
result b
end if
end first
% returns true if the argument is an even number
% false if it is an odd number
function even (value : int) : boolean
var temp : boolean := false
if value mod 2 = 0 then
temp := true
end if
result temp
end even
- first and second
- height and width
- int
-
The height is 12 and the width is 8
They differ by: 4
|
- It wouldn't make any difference. PosDiff finds the positive difference so it automatically subtracts the smaller
argument from the bigger one.
function posDiff (first, second : int) : int
result abs(first - second)
end posDiff
- (c) is invalid because put is not a function. There is no result to store in x.
(d) is invalid because abs is a function. You need to do something with the result. You have to say put abs(x) or x := abs(x). You can't call it like a procedure.
% a
function ceiling (number : real) : int
if number >= 0 then
if round(number) = number then
result round(number)
else
result round(number + 0.5)
end if
else
if round(number) = number then
result round(number)
else
result round(number - 0.5)
end if
end if
end ceiling
% b
put ceiling(3.1)
put ceiling(25.9)
put ceiling(67.0)
put ceiling(0.0)
put ceiling(-1.1)
put ceiling(-67.5)
put ceiling(-15.9)
function meanValue(a : array 1 .. * of int) : real
var sum: int := 0
for i : 1 .. upper(a)
sum := sum + a(i)
end for
result sum / upper(a)
end meanValue
function smallest(a : array 1 .. * of real) : real
var small: real := a(1)
for i : 1 .. upper(a)
if a(i) < small then
small := a(i)
end if
end for
result small
end smallest
% a
function numDigits(number : int) : int
var count : int := 1
var temp : int := number
loop
temp := temp div 10
exit when temp = 0
count := count + 1
end loop
result count
end numDigits
% b
var a : array 1 .. 6 of int
for i : 1 .. 6
put "Enter number ", i, " of 6"
get a(i)
end for
put "Number Number of Digits"
put "--------------------------"
for i : 1 .. 6
put a(i):6, numDigits(a(i)):12
end for