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 #14

Compound Data Structures - The SET

  1. Assuming the definition:
        Type  byteset = set of byte;
    


    Write simple but efficient sub-programs (procedures or functions) that perform the following functions:

    1. Procedure PrintSet(theSet:byteset)
      Given a set as a value parameter, output all of the elements that are contained within the set
    2. Procedure AddElement(var theSet:byteset; element:byte)
      Given a set as a REFERENCE parameter, and a valid element as a value parameter, return the set with the element added to it (use set union '+')
    3. Function Cardinality(theSet:byteset):byte;
      Given a set as a reference parameter, return the number of elements contained within the set (use a simple iterative tallying process)
    4. Procedure RemoveElement(var theSet:byteset; element:byte)
      Given a set as a REFERENCE parameter, and a valid element as a value parameter, return the set with the element removed from it, if it was there in the first place (use set difference '-')
    5. Procedure CreateRandomSet(var theset:byteset; num:byte)
      Get this procedure to generate a set of 'num' random elements, use your sub-programs above to verify this process is working well. For example, CreateRandomSet(ken,5) should return a set that contains 5 different elements.


  2. Using the definitions from the previous question:
    Write a LOTTO Syndicate Game Generator program that does the following things (in this order):
    1. prompt for and accept the number of numbers the syndicate can afford
    2. ask for that many numbers and store them in a set - this section of code should be 'clever' enough to ensure that only different numbers are stored in the set
    3. prompt for and accept how many numbers are required in each game
    4. prompt for and accept the number of required games
    5. generate and print onto the screen the number of required games, ensuring that each game contains the required number of numbers
    Please note, a working copy of this program is available for you to trial.

    1. Assuming the definitions
      	Type	days = (sun,mon,tue,wed,thu,fri,sat);
      
      		DaySets	= set of days;
      declare the following four set variables in Pascal.
      
      	EnglishDays,
      
      	MathematicsDays,
      
      	ComputerDays and
      
      	BiologyDays
      (These variables are going to contain all those days on which you have English, Mathematics, IPT and Biology.)

    2. Suppose your timetable looked like the table which follows (you should be so lucky!). Note that '--' means a spare.
      
      
      	Sunday:
      
      	Monday:		en   ma   ma   ip   bi
      
      	Tuesday:	ma   ip   --   bi   en
      
      	Wednesday:	--   --   bi   bi   --
      
      	Thursday:	en   en   bi   ip   ip
      
      	Friday:		--   ip   ip   ma   ma
      
      	Saturday:	
      
      
      Now, complete the following exercises.

      (a) Make appropriate assignments to the four set variables EnglishDays. MathematicsDays, IPTDays and BiologyDays which will inform the machine of your timetable.

      (b) Write Pascal boolean expressions which would indicate whether or not you:

      (i) had English on Monday
      (ii) had Biology on both Wednesday and Thursday
      (iii) had IPT on either Monday or Wednesday

      (c) Write small program fragments which would indicate those days on which you had

      (i) Mathematics
      (ii) both Mathematics and Biology
      (iii) either English or IPT
      (iv) Mathematics but not IPT
      (v) nothing

      (d) Write a small function which determines the number of days on which you have English.



  3. Write a BOOLEAN function called Yes which returns true if the user types 'y' or 'Y', and false if s/he types 'n' or 'N'. (Note that any other character must be ignored by the function.)

  4. Write a simple INTEGER function called Cardinality that accepts a set of byte and returns the number of elements in it.

  5. Write a simple procedure that operates on a set of char, changing all of the lower case letters present in the set to their uppercase equivalents.

  6. Mastermind is a simple game where you have to guess the unknown contents of a set of three letters.

    Write a simple but polite program that generates and stores in a set 3 different uppercase letters. Allow the user up to 10 guesses of three letters each guess - the feedback the user gets should ONLY include the number of correct letters (unless they guess all three)

Solutions

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