previous page main index next page
 

Designing Programs

In order to write a program, you first need to decide exactly what it is supposed to do. That might sound obvious but if you make mistakes at this stage it's unlikely that your program will work properly. There are four important processes to consider:

i) analyze the problem, decide what the program is meant to do and design a solution

ii) code the program - type in the Pascal code

iii) test the program - check that it produces correct results

iv) evaluate the solution - does it do what it's supposed to? Could it be improved?

We'll run through the following example to show you how the process works.
 

Example 3

A bus company requires a program which will calculate the cost of a journey given the rate per mile and the length of the journey. For example, if the company charges $0.75 per mile and the journey is 15 miles then the cost of the journey is 0.75 x 15 = $11.25

1. Design a Solution

Many programs follow a basic design that looks something like this:

1.  input information
2.  process the information
3.  output the results

We'll use that approach to solve our example and start with the following:

1.  get information from user
2.  calculate cost of journey
3.  print out results

We now take each of these three steps in turn and write down, in more detail, what each one does. The first step is to ask the user to enter the information required. What information do we need? Remember that the user needs to be prompted, or told, what to type in, otherwise they won't know what to do...

1.1  ask user to enter rate per mile
1.2  read in rate per mile
1.3  ask user to enter mileage
1.4  read in mileage

Notice the numbering system - step 1 has been broken down into four separate steps 1.1 to 1.4

The second step is fairly simple and can be described in one line...

2.1  calculate cost = rate x mileage

Step 3 is also straightforward...

3.1  print the mileage
3.2  print the rate per mile
3.3  print the cost

So we started with a simple design and refined it, putting in the necessary detail. The complete design is shown below:
 

1.1  ask user to enter rate per mile
1.2  read in rate per mile
1.3  ask user to enter mileage
1.4  read in mileage

2.1  calculate cost = rate x mileage

3.1  print the mileage
3.2  print the rate per mile
3.3  print the cost

 
It is important to note that the reason for doing this is not to make extra work for ourselves - it's so that we can use the design to code the program.
 

2. Code the Program

The program needs to use three numbers - the number of miles, the rate per mile and the cost of the journey. We now need to decide i) what to call these numbers, an identifier and, ii) what type of numbers they are. Here's our suggestion, listed in a table:
 

identifier type
rate REAL
miles INTEGER
cost REAL
 
Remember that whole numbers are INTEGERs. Numbers which might have a decimal part are called REAL numbers. So, now for the code...
 
PROGRAM bus_journey;      {to calculate journey cost}
VAR                       {by A Programmer}
  rate  : REAL;
  miles : INTEGER;
  cost  : REAL;
BEGIN
  WRITELN('enter the rate per mile ');
  READLN(rate);
  WRITELN('enter the number of miles ');
  READLN(miles);
  cost := rate * miles;
  WRITELN('number of miles = ', miles);
  WRITELN('rate per mile = $', rate);
  WRITELN('cost of journey = $', cost)
END.
 
Notice how closely the code corresponds to our design.
  • type in the program and save it as bus.pas
  • compile and run the program

Again, you'll see that the results are not what you perhaps expected.

  • edit the last but one and last but two lines to read:

   WRITELN('rate per mile = $', rate :5:2);
   WRITELN('cost of journey = $', cost :5:2);

  • compile and run the program
  • explain why it was not necessary to edit the line

   WRITELN('number of miles = ', miles);

We're going to make one or two more changes now...

  • edit line 7 to read

   WRITE('enter the rate per mile ');

(notice we're using WRITE instead of WRITELN)

  • and edit line 9 to read

   WRITE('enter the number of miles ');

  • compile and run the program
  • describe what difference the changes make when you run the program:
  • which do you prefer?
  • save this version of the program

3. Test the Program

Once you have the program working, you should test it to make sure it works properly. For example, we might draw up a table like the one below and check out various possibilities. What happens if you enter 6.5 for the number of miles? It is, after all, supposed to be a whole number.

  • copy and fill in the rest of this table
     
rate miles expected answer result or comment
2 50    
0.25 100    
0 100    
3 6.5    
3 mmm    
 
A summary of the results might go something like:

"The program gives correct results for whole numbers. If the user enters a decimal number for the number of miles then a 'run-time error' occurs and the program stops. This also happens if the user enters a letter instead of a number."

4. Evaluation

An evaluation should say whether the program does the job that is was designed for and whether it could be improved. For example:

"The program correctly calculates the cost of a journey based on the information provided by the user. Run-time errors occur if the user doesn't enter the correct type of data and it would be better if the program would allow the user to re-enter incorrect data instead of just stopping."
 

previous page main index next page
 
© 2001 by Mike Hardy