Study Group 1 Variables and Types 1. Define: (a) Variable, (b) Constant, (c) Type, (d) Enumerated Type, (e) Identifier. 2. What does the byte 00100110 represent? 3. What is the purpose of the declarations section of a Pascal program? Why are there so many different types of whole number? 4. Write Pascal statements that compute the roots of a quadratic equation ax 2 + bx + c = 0. 5. The function Ord can be applied to any ordinal type, including enumerated types. In particular, for working with ASCII characters (type Char): Ord(ch) computes the ASCII value (ordinal position) of character ch Chr(n) computes the character whose ASCII value is n; Thus Ord( A ) = 65, Ord( a ) = 97, Chr(122) = z, Chr(90) = Z, etc. The inbuilt type string is made of up of the sequence characters, numbered from 1. The [] notation is used to refer to a character at a particular position in a string. So for the string S = LIAM, S[1] = L, S[2] = I, etc. The function LENGTH can be applied to a variable type of string to return the number of characters making up that string, thus LENGTH( LIAM ) = 4. The lucky number for a name is made by adding the values of the letters, then repeating until the value is less than 10. So: LIAM = 12 + 9 + 1 + 13 = 35 = 8 Write formal statements (which could be Pascal) that compute the lucky number for (a) Liam, (b) your name. 6. Write Pascal statements that change the string SEAL into the string COAT by changing one letter at a time, each time making a new (proper) word. 7. Define an enumerated type for the days of the week. Consider some useful functions on this type (e.g. before, after, weekend, weekday, n days time, etc), how they might be programmed, and what their type signature would be. 8. In The Republic, Plato introduces his Theory of Forms, using as an example distinction between 2 as a real number (used by practical people for counting) and the number 2 of pure mathematics. Point out to Plato where he was going wrong. The, 2001.
Study Group 2 Loops and Conditionals A cipher is a general term for any cryptographic technique in which the letters of a string, called the plaintext, are changed into another string, called the ciphertext. A transposition cipher is a cipher where the characters stay the same but change position in the string; a substitution cipher is a cipher where the characters change but retain the same relative positions. The double rail transposition cipher mixes alternative characters: e.g. hello world = h e l l o w o r l d = hloolelwrd One possible substitution cipher is to exchange each letter n places from the start of the alphabet with a letter n places from the end of the alphabet, e.g. substitute z for a, y for b, and so on. 1. Write a program to encode and decode a plaintext using the double rail cipher described above. 2. Write a program to encode and decode a plaintext using the substitution cipher described above. 3. Generalise the program of Question 1 for an arbitrary number of rails. 4. Write a program to encode and decode a plaintext using a transposition cipher which swaps adjacent letter pairs. 5. The Caesar Shift Cipher. Write a program to encode and decode a plaintext using a substitution cipher in which the letters of the alphabet are shifted n places, i.e. for n =1, a b, b c, etc.; for n =2,, a c, b d, etc. The, 2001.
Study Group 3 Algorithm Design Using the Vignere Cipher, a keyword is used to index into a set of Caesar Shift Ciphers, each shifted by a different number of places: A B C D E F a b c d e f g h i j k l m n o p q r s t u v w x y z 1 b c d e f g h i j k l m n o p q r s t u v w x y z a 2 c d e f g h i j k l m n o p q r s t u v w x y z a b 3 d e f g h i j k l m n o p q r s t u v w x y z a b c 4 e f g h i j k l m n o p q r s t u v w x y z a b c d 5 f g h i j k l m n o p q r s t u v w x y z a b c d e 6 g h i j k l m n o p q r s t u v w x y z a b c d e f So using the keyword DAB (row 4, row 1, row 2), a plaintext would be enciphered by repeating the keyword over the plaintext and looking at the row of the table to encode each letter: Keyword Plaintext Ciphertext D A B D A B D A B D A B D A B D A B h e e d t h e i d e s o f m a r c h l f g h u j i j f i t q j n c v d j Note how the same letters in the plaintext can be encoded as different letters in the ciphertext, and how different letters in the plaintext are encoded as the same letter in the ciphertext. 1. Design a program (i.e. specify an algorithm in pseudo-code) to encode and decode a plaintext using the Vignere Cipher. 2. Implement your design, by writing a program to encode and decode a plaintext using the Vignere Cipher. Hint. Use a string constant to represent the Vignere Table and use modulo arithmetic. The, 2001.
Study Group 4 Procedures and Functions 1. Write a function that computes the greatest common divisor (GCD) of two numbers. 2. Write a function that computes whether or not two numbers are co-prime (relatively prime), i.e. their GCD is 1. 3. Write a procedure that gets as user input two numbers, p and q, checks that they are both prime, computes N = p*q, M = (p-1)*(q-1), and asks for another number e which must be less than p*q, checks that M and e are co-prime, and returns the values of M, N, and e. 4. Write a function that computes the value of d in the equation e * d MOD M = 1 Hint. One way is to test successive numbers of d. Another way is to recognize that trying to calculate d in d * e MOD M = 1 is the same as computing d such that (de - 1) is evenly divisible by (p-1)(q-1), i.e. M. Mathematicians write this as de = 1 (mod (p-1)(q-1)), and they call d the multiplicative inverse of e. This is easy to do - simply find an integer x which causes d = (x(p-1)(q-1) + 1)/e to be an integer, then use that value of d. Then we are trying to calculate: d = xm + 1/e de = xm + 1 de xm = 1 de + (-x)m = 1 There is an algorithm called Extended Euclid s algorithm which can be used to compute the greatest common divisor, g, of two positive integers, a and b, and coefficients, h and j, such that g = ha + jb. 5. Write a function that will compute modulos of very big exponentials, bearing in mind the following example: A e mod N =(A e1 * A e2 ) mod N where e = e1 + e2, and if e is a power of 2, and: A e mod N = (A 2 ) e/2 mod N = (A 2 mod N) e/2 mod N The, 2001.
Study Group 4a Procedures and Functions 1. Write a function maxof2 that returns the larger of two values. Write another function minof2 that returns the smaller of two values. Write a function maxof3 that returns the largest of three values, and another function minof3 that returns the smallest of 3 values. Write a function midof3 that returns the middle value of three numbers, using one statement, using maxof2 and minof2 functions only. 2. Write a function which takes as input a number in the range 1..365 and which returns a date in DD/MM format. In the first version, ignore leap years. Write a second version which takes as an extra parameter the year, and returns the date in DD/MM/YY format. In other words, you will need to write a (Boolean) function leapyear, according to the rules: 1. If a year is divisible by 4 it is a leap year; 2. Unless it is divisible by 100, in which case it is not a leap year; 3. Unless it is divisible by 400, in which case it is still a leap year. This means that 1800 and 1900 were not leap years, but 2000 was. 3. Define a type which stores the vertices of a quadrilateral. Write a function which checks if the four vertices form a square. 4. Write procedures which draw right-angle triangles using characters, of a specified size and shape. The shape depends on which corner the right angle is in, and can be one of four possibilities: south-west, south east, north-west or north-east * * ***** ***** ** ** **** **** *** *** *** *** **** **** ** ** ***** ***** * * south-west south east north-west north-east The, 2001.
Study Group 5 Data Structures 1. Redesign the Vignere Cipher program using an array. Write a program to implement the redesign. 2. One way to crack a substitution cipher is frequency analysis. That is, the codebreaker looks for the most common letters occurring in the ciphertext, and supposes that these are the ciphers for the most commonly occurring letters in natural text. Write a program that counts the number of occurrences of each letter in a ciphertext. 3. Write a program that displays a graph of the results from Question 2. 4. Design a program that would propose solutions to a Caesar Shift Cipher. The, 2001.
Study Group 6 Dataflow Design The ADFGVX Cipher features both transposition and substitution. The process is: Draw up a 6-by-6 grid, filling the 36 squares at random with the 26 letters of the alphabet and the 10 digits. Each row and column of the grid is identified by a letter from A, D, F, G, V and X. The first stage of encryption is to take each letter of the plaintext, locate it in the grid, and substitute it with the letters labeling its row and column. The second stage is to use a keyword, the letters of which are written on the top of a fresh grid. The letters from the first stage are then written in rows across the grid. The grid itself is then rearranged so the letters of the keyword are in alphabetical order, then the letters are read off column by column. For example: given the grid: A D F G V X A z l x 6 f 9 D u 8 m g v e F 3 t h n d 2 G k i s c o y V j w b r 5 p X 4 a 0 1 q 7 hello world then codes as FF DX AD AD GV VD GV VG AD FV Assume we have the keyword LIAM. The code letters are then arranged and transposed L I A M A I L M F F D X D F F X A D A D A D A D G V V D V V G D G V V G V V G G A D F V F D A V Giving a final code of DAVVFFDVVDFAGGAXDDGV 1. Design (dataflow and pseudocode) a program to encode and decode using this cipher. 2 Write a program that implements the design. The, 2001.
Study Group 7 Noughts and Crosses Consider the design and pseudo-code from Lecture 9 (Tic Tac Toe). Then consider the following declarations: type var OandX = (nought, cross, blank) ; Tttboard1D = array [1..9] of OandX ; Tttboard2D = array [1..3,1..3] of OandX ; Frame1D : Tttboard1D ; Frame2D : Tttboard2D ; Player : OandX 1. Write code to initialize the frame at the start of a new game, using: (a) using a function; (b) using a procedure; (c) using a procedure with global variables. 2. Write a procedure that displays the state of the game at any point, for each of Frame1D and Frame2D. 3. Write a procedure or function that implements MakeTurn, i.e. inputs a board location from the user, validates it, and once valid input is entered, updates the game board. Specify whether you are using Tttboard1D or Tttboard2D and how the user enters input. 4. Write a procedure or function that calculates the game status from the current state of the board (GameOn, GameOver, GameWon, and GameDrawn). 5. Given a single additional data structure, it is possible to define a boolean function for checking GameWon using a single for loop whose body is a simple statement (a single assignment). Define this data structure and implement this function. 6. Define and implement a procedure that would allow the computer to make a turn. The, 2001.
Study Group 8 File Input and Output Processing TextFiles 1. Write a program that takes as input a textfile, codes it using some cipher algorithm, and outputs it as another textfile. 2. Write another program which decodes encrypted textfiles. 3. Write a program which extends the ADFGVX cipher program by reading in the grid and keyword from some configuration file. Processing Typed Binary Files A program is required to process football results and update a league table. The league consists of twenty teams. Each week a number of matches are played between the two teams. The results of the matches are of the form: Arsenal 5 Tottenham Hotspur 3 Newcastle United 0 West Ham United 2 Manchester United 4 Brentford 7 And so on. The league table will store, for each of twenty teams, the team name, the number of game played, the number of home matches won, drawn and lost, the number of away matches won, drawn, and lost, the goals scored for and against, and the total number of points (3 for each win, 1 for each draw and 0 for each loss). 4. Write declarations for: a date type of team records, a data type for a league table; a data type for results; a file of type results, and a file of type team records. 5. Design and implement a program which reads in a file of team records into a league table, then processes a file of results, updating the league table according to the results, then outputs the new league table to a new file. 6. Write out the new file sorted in order of points, then goal difference, then alphabetical order. ± 7 The, 2001.
Study Group 9 Abstract Data Types 1. A stack is a dynamic data structure providing a reserved amount of memory for storing data items. A stack can be used, for example, used for arithmetic calculations, or to keep track of internal operations. Stacks keep track of the sequence of routines called in a program. For example, if one routine calls another, which calls another and so on. As each routine is completed, the computer returns control to the calling routine all the way back to the first one that started the sequence. Stacks used in this way are LIFO based: the last item, or address, placed (pushed) onto the stack is the first item removed (popped) from the stack. Write a unit containing an abstract data type for a stack using an array. You should provide four access procedures: PUSH (push an item onto the stack), POP (remove the topmost item off the stack), PEEK (look at the top item on the stack without processing it), and SIZE (how big the stack is). You can use any kind of data item for the type of data stored on the stack. 2. Re-implement the stack ADT of Q1 using a dynamic data structure. [Requires Pointers.] 3. Write a unit containing an abstract data type for a queue. A queue is like a stack except it is LILO based (i.e. last in last out). Again, design and implement two implementations, one using an array and one using a dynamic data structure. The, 2001.