Pascal Programming Exercises 2

  1. Write a program which outputs lines of stars in a triangle, 1 star on row 1, 3 on row 2, 5 on row 3, etc. Answer
  2. Write a program which implements a 'number chain' such as : "Think of a number. If it's even halve it; if it's odd multiply it by 3 and add 1. Display each value calculated. Continue this with the result of the previous calculation until the sequence reaches 1." Try changing the chain rules and observe the effects.
  3. Write a program which inputs a value and returns its square root. Do not use the sqrt() function, instead use the 'bisection' method (an 'iterative' method) as follows :

    Enter a number, n;
    enter a first estimate of its square root, e;
    enter the level of accuracy required (eg 0.01), ac;
    set a counter, c, to 1;
    repeatedly
        take the square of e;
        if square of e = n then you have the root;
        if the square of e is < n then add c to e else if square of e > n then subtract c from e;
        divide c by 2;
    until c < acc.
  4. Write a program which outputs the values in Pascal's triangle to a width of ten values at the base. Use the function from the previous exercise and d!/r!(d-r)! to calculate each value - d is the number of the diagonal and r is the number of the row in the triangle.

    [1] diagonal 0
    [2] 1 diagonal 1, etc.
    [3] 3 1
    [4] 6 4 1
    [] = row 1
    You will need some clever formatting to output this as a triangle. You might use :
    d := 0; r:= 0;
    repeat
    for i := 0 to ? do
    begin
    pascal := fact(?)/fact(?) * fact(???); {fact is the function from Exercise 62}
    write (?);
    end
    inc (?); inc (?);
    writeln;
    until ?
  5. (loops, function, algorithm) Write a program which tests whether a number is prime. 
  6. (loops, function, algorithm) Write a program which displays the prime numbers up to a user-supplied value.
  7. Write a program which displays recurring decimals in a fraction such as 1/7.
  8. Write a program which displays 'Pythagorean triples'. In D*D=X*X+Y*Y, eg 5*5=4*4+3*3.
  9. Write a program which implements the 'stamp' problem - eg what amounts can be made up from stamps of value 4p and 5p or 7p and 9p. In general, what amounts can be made up from values x and y?
  10. (Repeat) Write a program which repeatedly takes inputs from the keyboard until a particular character is pressed (eg '@'). Count the number of letters, punctuation marks and digits read. (Use "in ['A'..'Z', 'a'..'z']", "in [0..9]" and "in ['.', '.', ';', ':', '#', '!', '"', '£']")
  11. (Arrays, structure diagrams) Write a program which counts the number of different letters, numbers and punctuation marks in a string of input. Use an array with subscript range ['.'..'z'] and refer to each cell as count['A'], count ['g'], etc. where 'A' = ord[input character].
  12. (ANSI,) Write a program which declares foreign characters and uses them to output a message in French (é = 130, ç = 135, ê = 136, è = 138).
  13. Write a program which performs the addition of two 3x3 matrices.
  14. Write a program which performs the multiplication of two 3x3 matrices.
  15. (sets) Write a program which implements the game of Snakes and Ladders. Take input from the user in the range 1-6, define the starting numbers of Snakes and Ladders as sets of numbers. Count the position of the user in the grid of cells. If they land on a snake head their position changes to the bottom of the snake, if they land at the bottom of a ladder their position changes to the top. 
  16. Write a program which stores a word and a count of its occurrences in a record structure. There should be an array of records which you will fill as you read a text file (ignore certain words like 'the', 'a', 'an', 'this', etc.). 
  17. (Arrays, algorithm) Write a program which computes the first 20 terms in the Fibonacci sequence (0, 1, 1, 2, 3, 5, 8, 13...). Store the result in an array to create a look-up table and offer the user a choice between displaying a part of the series or an individual value.
  18. (Records, Enumerated types, multi-dimensional arrays) Write a program which records the details of stocks of jeans kept according to their make, waist size, inside leg measurement and colour. Create enumerated types for the indices of the arrays eg type waist = 20..40 ... colour = (red, blue, green) and so on. Then : stock = array [make, waist, leg, colour] of integer. 
  19. (Seek) Write a program which creates a file of passengers on some means of transport and then outputs them in order 1,3,5 etc and 2,4,6 etc.
  20. (Recursion, parameters) Write a program which uses recursion to compute the GCD of two numbers.
  21. (Recursion, function) Write a program which uses a recursive method to calculate the first n terms in the Fibonacci sequence. (PG) 
  22. (Recursion, function) Write a program which uses a recursive function to provide a sequence of moves to solve the 'Towers of Hanoi' problem. (PG)
  23. Write a program to play noughts and crosses.
  24. (Enumerated types, ord) Write a program which defines a type 'weekday' as (monday, tuesday, wednesday, thursday, friday) and then uses this to declare an array type 'hours_worked'. Declare three variables of type hours_worked for junior, supervisory and managerial staff and then collect data on the hours worked, add them up and output the results.
  25. Extend the previous exercise with a procedure to insert a record in the sorted position (smallest first). 
  26. Write a program which implements a stack using an array.
  27. Write a program which implements a queue in an array. Use taxi calls as data.
  28. Write a program which implements a linked list in an array.
  29. (Pointer types - ^real, ^integer, ^string, new, dispose, SizeOf) Write a program which declares pointer types to a range of variables of normal and pointer type and then assigns values to each one. Use the SizeOf function to return the memory used by each variable and then use Dispose to remove the dynamic items from the Heap.
  30. Write a program which defines a linked list of numbers as a record containing a number and a pointer to the next record. One procedure should take inputs from the user and place each new number in a new record in the linked list. Another procedure should display the list (unsorted). 
  31. Write a program which implements a binary tree using pointers. Place as many of the tree manipulation routines as you can in a unit.
  32. Write a program which implements a concordance tool by storing stores words and a counter in a record structure, along with pointers to maintain the records in a tree structure. (PG)
  33. Write a program which accepts an expression from the user and converts it into Reverse Polish.
  34. Write a program which allows the computer to play the 'Game of Life'. Answer.

Back to Pascal Home Page