Teaching Programming II

Copyright © by Mark Baker 1996

Return to Contents


Introductory Exercises

These exercises get the students to choose which of the structures are relevant to a particular situation. Some effort needs to made to show them that although more than one structure can often be used in any one task (especially true of loops), some choices are better than others. In particular, I often find students using REPEAT UNTIL loops (probably because they are fresher in the mind!) when a FOR loop would be much easier.

The introductory exercises are also designed to get the students to build up the complexity of their programs by putting more and more structures together. As far as is reasonable, I try to get them to program in a thorough way, expecting them to validate data entry and give the user adequate prompts. Once I have managed, as a user, to get their precious creations to misbehave a couple of times, they put much more effort into trying to close all the loopholes before I test their programs! As with much of teaching, there is a fine judgement to be made about the balance between positive encouragement and constructive criticism. Some pupils need more of one, whilst others thrive on the other!

The Exercises

  1. Read in a name and print it out 5 times in a list.
  2. Read in a name and allow the user to choose how many times to print it out.
    Test the program by entering zero, when asked how many times you want your name printed out. Often this traps the program into an infinite loop, which teaches an important lesson about validation.
  3. Read in a name and print it out in the four corners of the screen.
    This can teach important lessons about staying within the bounds of the screen. Students will often go to the last character position on the right and start to write from there, forgetting about the length of the string they are writing. Using a WRITELN instead of a WRITE on the bottom line will cause the screen to scroll up, losing the top line.
  4. Enter 2 numbers and print out the largest one.
    Test the students' programs by entering two equal numbers. Does the program cater for this situation?
  5. (Add this on to program 4) Ask the user if they wish to continue or not. If they say no, end the program.
    If they say yes, repeat the program.
    The "Continue, Y/N?" feature teaches the students a lot about the nature of a loop, in particular, where loops have to start and end. They may also be confused to start with, as the "Y/N?" suggests that a selection structure is needed, rather than a repetition. A second, nested loop is needed to validate the Y/N entry, ie. REPEAT read character UNTIL character is y,Y,n or N.
  6. Print out all the integers from 1 to 15.
    Was a FOR loop used? If not, why not? Which is the easiest loop to use?
  7. Print out the 5 times table.
    Students are encouraged to output 1 x 5 = 5, 2 x 5 = 10, etc., rather than just 5,10,15...
  8. Print out the 'n' times table, where n is chosen by the user and is between 1 and 12.
    Do the students' programs validate values less than 1 and more than 12? Is an appropriate error message output? Students often go on to extend this progam by producing more than the first 12 multiples. This can introduce them to paging their output.
  9. Change program 8 so that the user can choose to continue or to end.
  10. Read in a character and make it appear to move across the screen.
    The character must be erased from its last position, written into the next position and then a delay procedure called to enable the effect to be seen. This process must be repeated as the character moves from one side of the screen to the other. I have had students try and do this long-hand, writing out instructions for every single character position! Whilst they may have used cutting and pasting to reduce the workload, this is clearly not acceptable. It is useful when it happens however, as it allows valuable teaching points to be made about the labour-saving properties of loops, and the uses of variables.

Progx Programs

The students have been presented with some of the basic building blocks. It is now time for them to see some of these elements put together to create something more substantial. I originally devised a series of 9 programs, Prog2 to Prog10, building on from the Hello World! program. Now I spend more time introducing the basic structures and do not need to use some of the early programs. However, the later ones are useful for introducing procedures, following this sequence:

Prog7 - This program asks the user to input a number between 1 and 5. If an invalid number is input, then a procedure is called which outputs an error message. This procedure has no parameters.
Prog8 - This program extends Prog7, with the error message procedure accepting two parameters, one for the text colour and one for the background colour.
Prog9 - This next extension adds a second procedure, where the value of one of the parameters is returned to the calling section. The students are also asked to write a program which will allow the user to calculate the mean of UP TO ten numbers. This usually proves quite a challenge, with most trying to use ten different variables and producing programs that can only work with exactly ten numbers. Most students need prompting to use only two variables, a running total and a count of the number of values entered
Prog10 - A new program, which has a single function "Mean" and illustrates the differences between procedures and functions.

The students can either copy the programs from a sheet, or download them from a shared directory. They are told what to do by the comments at the top of the program. Amongst other things, they have to add their own comments, to explain what different sections of the program are doing. This forces them to use the step-through and watch facilities, offered by Borland's IDE. Students can be very resistant to using the debugging tools, they would prefer that their teacher find their mistakes for them! They can need considerable encouragement to start actively debugging by themselves, sometimes prefering a random-change policy, to any kind of systematic approach!


Example - Prog7


program Prog7;

{This program shows you how to use a procedure.
 1. Enter the program.
 2. Add comments to explain what EACH part of the program does. Use f7 and
    f8 to step through the program, to help you. You can try using
    Right mouse button/Add watch to set up watches on particular
    variables, but don't worry if you cannot get this to work.
 3. Put a comment at the top to say what the whole program does.
 4. Change the error message to one of your own.
 5. Explain the difference between the statements "writeln ();"
                                             and  "write ();"
    (hint: try changing some writeln statements for write statements!)}


uses crt,unit_tex;
{crt is a Turbo Pascal library unit to do with screen display (crt - Cathode Ray Tube).
 unit_tex is my own library unit, see "Reference Guide for Practical Work".}


var value: integer;

{--------------------------------------------------------------------------}
  procedure ErrorMessage;
{--------------------------------------------------------------------------}
{}
  const message = 'You have entered an invalid number!';

  begin
  {}
  Warning(message);      {From unit_tex}
  {}
  textbackground(0);     {From crt}
  clrscr;                {From crt}
  {}
  writeln('You may only enter whole numbers between 1 and 5 inclusive.');
  writeln;
  writeln('Please try again, press  to continue...');
  writeln;
  writeln;
  {}
  readln;
  end;  {ErrorMessage}

{--------------------------------------------------------------------------}

begin  {main program}

repeat
  clrscr;
  {}
  writeln('Please enter a number between 1 and 5.');
  {}
  readln(value);
  {}
  if (value > 5) or (value < 1) then ErrorMessage;
{}
until (value > 0) and (value < 6);

end.


Return to Contents

Teaching Programming I
Teaching Programming III
Teaching Programming IV


Author: Mark Baker, e-mail mbaker@rmplc.co.uk
Last revision: 22/10/96