IPT - A Virtual Approach IPT A Virtual Approach by Peter Whitehouse
Quick Links:
 
 
Information and Intelligent Systems Social and Ethical Implications Human Computer Interaction Software and Systems Engineering eXercise Files Course Outline and Assessment A-Z of Geeky Acronyms Terrace Work Program 2004 Sillybus FAQ = Frequently Asked Questions Help
 
 

eXercise #12

Sub-Programs With Parameters

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

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

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

  4. 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).

  5. 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'.

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

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


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

  9. 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).

Solutions

wonko@wonko.info
©Copyright t 1992..2017+. Edition 25.150117
wonkosite
Creative Commons License
This work is licensed under a
Creative Commons Attribution-NonCommercial-ShareAlike 2.1 Australia License
.