Copyright © by Mark Baker 1996
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!
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!
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.
Teaching Programming I
Teaching Programming III
Teaching Programming IV