|
|
|
|
eXercise #12
Sub-Programs With Parameters
- Write a procedure called WriteTo which accepts
an integer or integer expression and which writes all the integers
from 1 to the value of this expression, each in a field of width 5.
For example, the call to
WriteTo (5);
would write the numbers 1 .. 5
WriteTo(sqr(13))
would write all the integer from 1 to 169, the square of 13.
- Write a procedure called DrawSquare which accepts
an integer expression and which draws a square of asterisks on the
screen, where the size is determined by the value of the supplied
expression. For example, the request to
DrawSquare(7);
would draw a square seven asterisks by seven asterisks. Note that
this is a solid square, not a border. It is suggested that you use
a nested for..do loop for this exercise.
- Write an integer function called Cube which accepts
an integer value and which returns the cube of this value. For example,
the call
writeln (Cube(5));
would produce 125 (the cube of 5), and
writeln (Cube(17-14));
should print out the cube of 3, i.e., 27.
- Write a real function called AverageOf containing
TWO integer parameters: the first paramete determines the UPPER limit
of the random range, the second integer parameter determines how many
random numbers between (0 .. 1st Parameter) then returns the average
of the random numbers. Thus, the request:
writeln (AverageOf (20,100));
should return the average of 100 random numbers (all of which were
in the range 0..20).
- Given the definition
type days = (SUN, MON, TUE, WED, THU, FRI, SAT)
define a function of type days which is called DayAfter
which returns the day of the week after the day sent to it as a parameter.
Thus, the request:
DayAfter (WED)
should return the value THU
while the more difficult request:
DayAfter (SAT)
should correctly return the value SUN.
Team this with a procedure called WriteThe which
writes the days of the week so that, for example, the call:
WriteThe (DayAfter (SUN))
would print out 'Monday'.
- Write a procedure called FetchRandomCoordinates
which returns two random numbers, the first in the range 1..80 and
the second in the range 1..25. Then, using this procedure, modify
your 'Buzzy Bee' program - using your procedure to initially place
the honeypot ('H'), then, use your procedure to provide coordinates
to 'teleport' the Bee ('B') around until they find nectar heaven on
top of each other.
- Write a function EarlierOf which accepts two characters
and which returns these in alphabetical order. As well, the function
should return as its own value, the earlier character. For example,:
var chl, ch2 : char,
begin
readln (chl, ch2);
If chl=ch2
then writeln ('Characters are the same.')
else writeln (EarlierOf (chl,ch2), ' is the earlier of chl, ' and ', ch2)
should output:
D is the earlier of D and M.
- Write a procedure Sort which accepts exactly three
integers and which swaps their values so that the actual parameters
end up with values in ascending order; e.g., if we then write:
i := 8;
j := 7;
k := 3;
sort (i,j,k);
writeln (i, j, k)
the output:
3 7 8
is obtained.
- As mentioned in previous exercise sets, a card from a normal deck
can be represented using the integer range 0..51 (=52 cards, no jokers).
A Pack of cards is divided up into 4 suits (clubs, spades,
diamonds and hearts). Each suit contains Ace, 2,
3, 4, 5, 6, 7, 8, 9, 10, J, Q and K.
The suit can be extracted from the cardNumber by using the
DIV operator:
cardNum div 13 = suitNumber (which will be 0, 1, 2 or 3)
cardvalue can be extracted from the cardNumber by using the
MOD operator:
cardNum MOD 13 = cardValue (which will be 0, 1, 2, 3, 4, 5, 6, 7,
8, 9, 10, 11 or 12)
Write functions called CardValue and CardSuit
that accept an integer in the range 0..51, and output their value
and suit respectively.
Then write a procedure called PrintCard that accepts
an integer in the range 0..51, and calls CardValue
and CardSuit to successfully print out the card on
the screen (as, say, 7 of Hearts).
|
|
|
|
|
|
|