previous page main index next page
 

Example 4

A gardener requires a program which will calculate the length of fencing needed for a rectangular garden, given that she knows the length and width.

1. Design a Solution

Using a similar approach to the last example, we'll start with the following:

1.  get information from user
2.  calculate length of fence
3.  print out results

As before, we take each of these steps in turn and write down in more detail what each one does. What information do we need? How do we calculate the length of fencing?

Step 1 becomes:

1.1  ask user to enter length of garden
1.2  read in the length
1.3  ask user to enter width of garden
1.4  read in the width

Step 2 becomes:

2.1  total length of fencing is 2 x length + 2 x width

Step 3 becomes:

3.1  print the length
3.2  print the width
3.3  print the total

The complete design is shown below.
 

1.1  ask user to enter length of garden
1.2  read in the length
1.3  ask user to enter width of garden
1.4  read in the width

2.1  total length of fencing is 2 x length + 2 x width

3.1  print the length
3.2  print the width
3.3  print the total

 
2. Code the Program

The program needs to use three numbers - the length and width of the garden and the total length of fencing. What type of numbers should they be - whole numbers or decimal numbers?

  • decide what type of numbers you should use - INTEGER or REAL - then copy the table into your work book and complete it.
     
identifier type
length  
width  
total  
  • open your Pascal editor and type in the code for the program - you can use the last example as a guide
  • save your program as fencing.pas
  • compile and run the program

3. Testing the Program

When you've got the program running, you need to test it.

  • draw up a table similar to the one in the previous example and choose some suitable numbers to test your program
  • write down a short summary of the results

4. Evaluation

Have a look at the evaluation for the last example and use it as a guide for this one.

  • write an evaluation of your program
     
previous page main index next page
 (C) By Mike Hardy