Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
30 views

Introduction To Computer Programming

The document provides an introduction to computer programming. It defines a computer as an electronic device that operates under stored instructions and has components like a central processing unit, memory, and input/output devices. A computer program is a sequence of instructions that is stored in memory and executed by the CPU. The development of a computer program follows a typical life cycle including problem definition, analysis, algorithm development, coding, testing, and maintenance. Data is processed into information which must be accurate, timely, and relevant for the user.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
30 views

Introduction To Computer Programming

The document provides an introduction to computer programming. It defines a computer as an electronic device that operates under stored instructions and has components like a central processing unit, memory, and input/output devices. A computer program is a sequence of instructions that is stored in memory and executed by the CPU. The development of a computer program follows a typical life cycle including problem definition, analysis, algorithm development, coding, testing, and maintenance. Data is processed into information which must be accurate, timely, and relevant for the user.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 49

1.

INTRODUCTION TO COMPUTER PROGRAMMING

1.1 Computer

A computer is an electronic device operating under the control of instructions stored in its
memory. It has the following characteristics:
 A computer has input and output circuitry
 A computer has a central processing unit for carrying out arithmetical and logical
operations
 A computer has memory for the storage of programs and data
 A computer can execute sequences of instructions.

The basic components of a computer are as shown in Figure 1.1:

Figure 1.1: Basic components of a computer

Central Processing Unit (CPU) is the part of the computer that executes the instructions
(program) stored in memory. The CPU is made up of the Control Unit and the
Arithmetic/Logical Unit. Control Unit is the component of the CPU that controls the actions of
the other components so that instructions are executed in the correct sequence.

Arithmetic/Logical Unit (ALU) is the component of the CPU that performs arithmetic and
logical operations.

Memory Unit is used for internal data storage in a computer. The memory is comprised of a large
number of individual cells, each with a unique number, known as its address, associated with it.
Each memory cell is made up of a set number of binary digits (bits). A Bit can store one of two
values, denoted by a 1 or 0.The Memory Unit stores several different types of data:
 Numbers- integers, reals, etc
 Characters - alphabetic, punctuation, special symbols
 Logical data - True/False results.

1
 Instructions - Program data that instructs the computer as to what to do.

A computer's memory is volatile, that is, its contents are lost when power is turned off and thus
the need for storage devices. Storage devices fall under the peripherals.

Peripheral Devices are the equipment attached to a computer and include the following:

 Input Devices - Instruments for entering data into a computer.


 Keyboard
 Mouse
 Microphone
 Scanners
 CD-ROM Drive
 Web-Cam etc.
 Joystick
 Touch screen
 Output Devices - Components for displaying or presenting data to users.
 Monitor
 Printer
 Speaker
 CD-ROM Burner etc.

 Storage Devices - Components for long-term storage of data.


 Floppy Disk
 Hard Disk
 Rewritable Optical Disk
 Flash drive
 Memory Card etc.

1.2 Computer Program

A Computer program is a sequence of instructions which the CPU interprets as arithmetical and
logical operations. A computer program is stored in memory and also consists of data and
instructions. The input-output circuits (I/O) enable the machine to transfer data between the
machine and the external world.

Instructions and programs imply that there is some order in what the computer does, that is, there
is an element of design. A program designer determines what instructions and data structures are
required in order to get the machine, the computer, to complete some given task. Most
significantly the designer determines the sequence in which the instructions will be carried out
(executed) in order to successfully complete the task.

Before coding a program, the programmer should plan the solution to the problem in detail and
write down the processing logic. Programming is expensive and time consuming task and care
must be taken in writing a good program. A well developed program has characteristics outlined
below:

2
1. Accuracy: the program should do what it is suppose to do, meet criteria in
specification
2. Reliability: the program must always do what is suppose to do and never crash
3. Efficiency: the program should use the available storage space and resources in such
away that the systems speed is not wasted
4. Robustness: the program should cope with invalid data without creating errors or
stopping without indication of cause.
5. Usability: the program must be easy to use and well documented
6. Maintainability: the program must be easy to amend, having good structuring and
documentation.
7. Readability: the code in the program must be well laid out and explained with
comments.

A computer program model

Since a computer program is a sequence of instructions which operate on data we can show this
in a simple model:

Figure 1.2: Computer program model

The model implies that the program processes data which comes from a source and goes to a
destination - the data sink. Although it isn't always the case, in larger programs and programming
systems the source and sink are usually external to the program. A data source will be an input or
storage device like a keyboard, disk drive, scanner, tape drive; a data sink will be an output or
storage device like a computer screen, printer, disk drive, tape drive, plotter etc.

1.3 Data and Information

These terms tend to be used interchangeably. Data is a collection of facts and figures which can
be processed into information. In an educational environment data can be names of students of
student etc. In business environment it could be names of employs number of hours worked,
stock inventory etc.

Data processing is the collection, manipulation and distribution of data to achieve certain
objectives. The processing may involve calculation, comparisons, decision marking and/or other

3
logic to produce the required result. The processing of data is usually done by a computer or a
human being or both.

Information is data which has been summarised in a way desired by the user. The end product of
data processing is information available where wanted, when wanted and in the form wanted.
The information must be accurate, timely, complete, concise and relevant.

1.5 Computer Program Development Life Cycle

A program may result from some problem, in this case problem means some task requiring a
solution, or from a bright idea, or because it is a project which has been set for student. No
matter what their first causes computer programs, aka software, and software systems,
collections of software, have quite well-defined life cycles. The need for a program is
recognised, the requirements are analysed, a possible solution is designed, the prototype is built
and tested, and finally the program is put into use. Figure 1.3 shows the typical life-cycle for a
computer program.

1. Problem Definition

6. Implementation
and 2. Problem Analysis
Maintenance

5. Testing
3. Algorithm
and
Development
Debugging

4. Coding
and
Documentation

Figure 1.3 Phases in development of program.

Phase 1: Problem definition

During phase one the emphasis is on fact-gathering and determining where the boundaries of the
problem are. It involves carefully reading and rereading the problem until you understand
completely what is required.

It is vitally important that problem definition be thoroughly done to ensure user satisfaction, and
to facilitate the subsequent stages in the program development cycle. Any failure at this stage
usually results in a program that will not work as intended or may not work at all.

4
To ensure that adequate information is collected to ensure success at this phase, the fact finding
techniques that may be used include: Interviewing, questionnaires, observation, document review
and user workshops

Interviewing is the process of obtaining information without upsetting the other party by means
of conversation. Interviewing is probably the most productive fact finding activity since
interviews not only allow you to collect fact but also to verify the facts collected and to provide
an opportunity to meet and overcome any possible user resistance.

Questionnaires may be most practical method of fact finding where (i) information is located
over a widely spread geographical area; (ii) verification of data found by other means is required;
(iii) a large number of people are required to furnish the data; (iv) 100% coverage is not required.
Care must be taken to ensure that the questionnaires are properly designed, tested and validated.

Observation involves watching an operation for a period of time to see for oneself what exactly
happens. This technique is important in tracing bottlenecks and checking facts that have already
been noted.

Document review this is very important when developing a program for an organisation. It is
used for understanding the structure of the organisation, it operation and the history of the
system. It is usually performed at the beginning of the systems study to get insight into into

User workshops get the users to participate in the analysis. Attendees are from various
departments having interest in the proposed program (software). One of the program designers
acts as the facilitator in the workshop and she or he should encourage open discussion about the
new system.

Phase 2: Analysis of the problem

Phase 2 involves identification of the variables and processes which make up the problem. To
help with this analysis, the problems should be divided into three separate components:

1. The inputs: a list of source data provided to the problems


2. The outputs: a list of the outputs required
3. The processing: a list of actions needed to produce the required outputs.

When reading the problem statement, the input and output components are easily identified,
because they use descriptive words such as nouns and adjectives. The processing components are
also easily identified. The problem statement usually describes the processing steps as actions,
using verbs and adverbs.

When dividing a problem into the three components, you should simply analyse the actual words
used in the specification and divide them into those which are descriptive and those which imply
action. It may help to underline nouns, adjectives, and verb parts used in the specification.

5
Some programming problems may not have clear inputs, processes and outputs. In such cases
you should concentrate on the outputs required. Doing this well help decide on most inputs and
the way well then be set to determine the processes steps required to produce the required output.

At this stage the processing section should be a list of what actions need to be performed, not
how they are accomplished. Do not attempt to find the solution until the problem has been
completely defined.

Example

Specification:

A program is required to read in the length and width of a rectangular house block and the
length and width of a rectangular house which has been build on the block. The algorithm
should then compute and display the time required to cut the grass around the house, at the rate
of two square metres per minute.

Analysis:

To establish the input and out components in this problem, the most relev nouns or objects
should be underlined as shown below:

A program is required to read in the length and width of a rectangular house block and the
length and width of a rectangular house which has been build on the block. The algorithm
should then compute and display the time required to cut the grass around the house, at the rate
of two square metres per minute.

By reading these words, it can be seen that the input is the length and width of the block and the
length and width of the house. The output is the time required to cut the grass.

The input and output can be setup in the following diagram referred to as the defining diagram:

Input Processing Output


block_length mowing_time

block_width

house_length

house_width

Now the verbs and adverbs in the problem statement can be underlined.

A program is required to read in the length and width of a rectangular house block and the
length and width of a rectangular house which has been build on the block. The algorithm
6
should then compute and display the time required to cut the grass around the house, at the rate
of two square metres per minute.

The processing steps can now be added to the defining diagram as follows:

Input Processing Output


block_length Prompt for block measurements mowing_time

block_width Get block measurements

house_length Prompt for house measurements

house_width Get house measurements

Calculate mowing area

Calculate mowing time

These steps are sufficient to establish the requirements of the problem. You must be absolutely
confident of what is to be done in the program before you attempt to establish how it is done

Phase 3 Algorithm developments:

Once the problem has been defined and analysed, you may decide to break the problem up into
smaller tasks or tasks and establish an outline solution. This initial outline is usually a rough
draft of the solution which may include the:

1. major processing steps involved


2. major subtasks (if any)
3. major control structures (e.g. repetition loops)
4. major variables and record structures and
5. mainline logic

The solution outline may also include hierarchy or structure charts that are discussed in detail in
other sections in the module.

The outlined solution is then expanded into an algorithm, which is a set of precise steps that
describe exactly the tasks to be performed and the order in which they are carried out. Algorithm
may be represented in pseudocode (a form of structured English) or flowcharts (pictorial form of
algorithm representation) or a combination of both.

The algorithm developed should be checked for correctness. This is done through desk checking.
The purpose of desk checking the algorithm is to identify major logic errors early so that they
can be easily corrected. Test data needs to be walked through each step in the algorithm to check
that instructions described in the algorithm will actually do what it is suppose to. The

7
programmer walks through the algorithm just as a computer would do, keeping track of all errors
on a sheet of paper. Algorithm development is discussed in detail in chapter 2.

Phase 4: Coding and documentation

In phase 4, you convert the algorithm you developed in phase 3 into your favourite programming
language. Programming Language is a set of rules, symbols, and special words used to construct
a computer program.

If you take sufficient care in the algorithm development phase coding is almost a mechanical
process since most of the hard work is already done in the design phases (phases 1 to 3).

Documentation involves development of user guides which are intended to help the user to use
the program, that is, to operate it with minimal or no guidance; and technical manuals which are
intended for systems analyst/programmers to enable maintenance and modification of the
programs design and code.

Phase 5: Testing and Debugging

Debugging is detection and correction of errors that may exist in the program. The only way to
be completely confident of that a program will behave as expected, under all circumstances that
can conceivably arise, is to test the program thoroughly before implementing it. Program testing
involves creating test data designed to produce predictable output, and comparing the actual
output with the predicted output.

Phase 6: Implementation and Maintenance

At this stage, the system designer will actually install the new system, create data files and train
people to use the new system. A common practice is to run the new system in parallel with the
old system in parallel with the old system to ensure that if something does go wrong, the
business does not come to grinding halt.

After a number of successful parallel runs, the old system will be abandoned and the new system
will become fully operational. At this point, the system goes into the final stage of monitoring
and review or evaluation where the performance of the system is evaluated with reference to
initial requirement.

Maintenance is done during the active use of the system until it is declared obsolete. It may
undergo enhancements or it may have bugs fixed during its lifetime.

N/B: There is a strong connection between phases 2 and 3 since the process of designing a
solution to a problem is an iterative process and often involves a great deal of discovery. For
example as you refine an algorithm you might discover that there are variables and/or processes
that weren't obvious during phase 2. This is to be expected, after all you are just learning about

8
this kind of thing, but you can expect to meet this kind of discovery right throughout your life as
a program designer.

1.5 Structured Programming

This is a method of writing computer programs so that each instruction obeys the Structure
Theorem. Structured Programming also incorporates top-down development and modular design.

Structure theorem is theorem states that it is possible to write any program by using only three
basic control structures. These control structures are: sequence, selection, and repetition. These
are discussed in detail in chapter 2.

Top-down development is the division of a problem into subtasks (understandable chunks) as the
first step towards designing the solution algorithm. The programmer develops an algorithm
which incorporates the major tasks first, and only considers the more detailed steps when all the
major tasks have been completed. This gradual breaking down of a programming problem into
more detailed steps until the most detailed level is reached is referred to as functional
decomposition or stepwise refinement.

Modular Design involves grouping tasks together because they all perform the same function.
Modular design is connected to directly to top-down development, as the steps or subtasks into
which the programmer breaks up the program solution will actually form the future modules of
the program. Good modular design aids in the reading and understanding of the program.

1.6 Object oriented programming (OOP)

Object oriented programming (OOP) uses all the features used in structured programming.
Object oriented design (OOD) involves the process of breaking a complex problem into smaller,
simpler components. However, these components are often tested independently of the program
which will use them. The major difference between object oriented programming from structured
programming is that in OOD the program is viewed as a collection of interacting objects rather
than functions.

An object is considered as a container for a set of data values and a set of operations to be
performed on it. Thus an object can be defined as having the following properties: It has a name;
it has a set of prescribed values; there exists a set of operations to be performed on it.

9
2. ALGORITHM DEVELOPMENT

Structured programming techniques require a program to be properly designed before coding


begins and it is this design process that result into the development of an algorithm.

What is an algorithm?

A Computer Program can be equated to the statement:

Programs = Algorithms + Data

The algorithm is part of the blueprint or plan for the computer program, an algorithm is:

"An effective procedure for solving a problem in a finite number of steps."

Effective means that an answer is found and it finishes and that is it has a finite number of steps.
A well-designed algorithm will always provide an answer, it may not be the answer you want but
there will be an answer. It may be that the answer is that there is no answer. A well-designed
algorithm is also guaranteed to terminate.

2.1 The key features of an algorithm

Here is an example of an algorithm for making a pot of tea:

1. If the kettle does not contain water then fill the kettle
2. Plug the kettle into the power point and switch it on.
3. If the teapot is not empty then empty the teapot.
4. Place tea leaves in the teapot.
5. If the water in the kettle is not boiling then go to step 5
6. Switch the kettle off.
7. Pour water from the kettle into the teapot.

You can see that the algorithm has a number of steps, that some steps (steps 1, 3 and 5) involve
decision-making and one step (step 5) involves repetition, in this case the process of waiting for
the kettle to boil.

Algorithms show these three features:

1. Sequence (also known as Process)


2. Decision (also known as Selection)
3. Repetition (also known as Iteration or Looping)

Note that this is the control structures that define the structure theorem.

10
Sequence

Sequence means that each step or process in the algorithm is executed in the specified order. In
the example algorithm above each process must be in the correct place otherwise the algorithm
will most probably fail.

The Decision constructs - If ... then, If ... then ... else...

In algorithms the outcome of a decision is either true or false, there is no in between. The
outcome of the decision is based on some condition that can only result in a true or false value,
for example:

if today is Wednesday then collect pay

is a decision and the decision takes the form:

if proposition then process

A proposition in this sense is a statement which can only be true or false, it is either true
that today is Wednesday or it is false that today is Wednesday. It can't be both true and
false. If the proposition is true then the process which follows the then is executed.

The decision can also be stated as:

if proposition
then process1
else process2

this is the if ... then ... else ... form of the decision. This means that if the proposition is
true then execute process1 else or otherwise execute process2.

The first form of the decision if proposition then process has a null else, that is, there is
no else.

The Repetition constructs - Repeat and While

Repetition takes two forms, the Repeat loop and the While loop.

The repeat loop is used to iterate or repeat a process or sequence of processes until some
condition becomes true. It has the general form:

11
Repeat
Process1
Process2
Process N
Until proposition

Here is an example:

Repeat
Put water in kettle
Until kettle is full

The process is put water in kettle, the proposition is kettle is full.

The repeat loop does some processing before testing the state of the proposition, what happens
though if in the example above the kettle is already full? If the kettle was already full at the start
of the repeat loop then putting more water in will lead to an overflow.

In this case the While loop is more appropriate:

While kettle is not full


put water in kettle

Since the decision about the kettle being full or not is made before putting water in then the
possibility of overflow is eliminated.

2.2 Different ways of stating algorithms

One way of stating an algorithm has already been shown above. This method can be referred to
as Step-Form. In this course you will study four different ways of stating algorithms:

1. Step-Form
2. Pseudocode
3. Flowchart
4. Nassi-Schneiderman

The first two are written forms. The tea-making example, discussed above, is in Step-Form and
as you saw with the Step-Form (SF) the written form is just normal language. A problem with
human language is that it can seem to be imprecise. In terms of meaning, what one writes may
not be the same as what you read. Pseudocode is also human language but tends toward more
precision by using a limited vocabulary.

The last two are graphical methods (pictorial), that is they use symbols and language to represent
sequence, decision and repetition.

12
Before studying in detail the different ways of stating algorithms, we need to cover two
important topics: variables and data types.

2.3 Variables

Since Programs = Algorithms + Data we should return to the topic of data. You know that data
is a symbolic representation of value and that programs set the context which gives data meaning
- in programs data is transformed into information. The question is: How is data represented in
programs?

Just about every algorithm contains data and usually the data is "contained" in a thing called a
variable. The variable is a container for a value which may vary during the execution of the
program. For example, in our tea-making algorithm the level of water in the kettle is variable, the
temperature of the water is variable, the quantity of tea leaves is also variable.

Each variable in a program is given a name, for example:

 Water_Level
 Water_Temperature
 Tea_Leaves_Quantity

and at any given time the value which is represented by Water_Level for instance may be
different to its value at some other time. The statement:

 If the kettle does not contain water then fill the kettle

could also be written as:

 If Water_Level is 0 then fill the kettle

or

 If Water_Level = 0 then fill the kettle

At some point Water_Level will be the maximum value, whatever that is, and the kettle is full.

Naming of variables

You should always try to choose meaningful names for variables in algorithms to improve the
readability of the algorithm or program. This is particularly important in large and complex
programs.

In the tea-making algorithm we used plain English. Below is shown how you might use variable
names for some of the algorithm variables. In the right-hand column are names that, although
shorter than the original, don't hide the meaning of the original phrase. Underscores are used to
indicate the words belong together and represent a variable

13
1. If the kettle does not contain 1. If kettle_empty then fill the kettle
water then fill the kettle 2. Plug the kettle into the power point
2. Plug the kettle into the power and switch it on.
point and switch it on. 3. If teapot_not_empty then empty
3. If the teapot is not empty then the teapot.
empty the teapot. 4. Place tea leaves in the teapot.
4. Place tea leaves in the teapot. 5. If water_not_boiling then go to
5. If the water in the kettle is not step 5
boiling then go to step 5 6. Switch the kettle off.
6. Switch the kettle off. 7. Pour water from the kettle into the
7. Pour water from the kettle into the teapot.
teapot.

There are many conventions of naming variable in use which are more of cultural to the language
one uses. It is a good idea to adopt a conventional way of naming variables as for a given
language. Otherwise for algorithm development, the advice above should suffice. Also note that
your algorithms and programs can benefit from using naming conventions for processes too.

Data types

The data used in algorithms can be of different types. The simplest types of data that an
algorithm might use are:

 numeric data e.g. 19, 11.45, 901.


 alphabetic or character data e.g. 'A', 'Z' or 'This is alphabetic'.
 logical data, that is propositions with true/false values.

2.4 A strategy for designing algorithms

Now that we know something about algorithms and data we can devise a strategy for designing
algorithms. The simplest strategy is given in the following steps:

Step 1: Investigation step

1. Identify the processes


2. Identify the major decisions
3. Identify the loops
4. Identify the variables

Step 2: Preliminary algorithm step

1. Devise a "high level" algorithm


2. Step through the algorithm. Does this "walk-through" reveal any major problems? If it
does correct the problems.

14
Step 3: Refining the algorithm step

1. Incorporate any refinements indicated in step 2.


2. Group together processes where appropriate
3. Group together variables where appropriate
4. Test the algorithm again by stepping through it

We will use and refine this strategy during the rest of this module but for now we should just
look at the steps.

The first step - Investigation - implies that you read through the statement of the problem to be
solved and do some analysis of the terms used. In the tea-making example we can see that there
are a number of processes, filling the kettle, plugging the kettle into the power point and so on.
There are decisions, variables and loops.

The second step - Preliminary algorithm - is our first attempt at solving the problem. It can be
quite crude but Step 2.2 will test it and bring out obvious errors.

The third step - Refining - is the most difficult step, this step needs practice and strictly speaking
the learning of some of the skills implied in this step won't really occur until you have gained
some programming experience. It won't hurt though to give the refining step some thought in this
course.

2.5 Step-form algorithms

This form of algorithm is the simplest and consists of a sequence of numbered steps or points. It
is the easiest to learn at first since it is rather like a "to-do" list however once you have mastered
other ways of stating algorithms you are unlikely to continue using this form.

You have already used this form in the tea-making algorithm. Here is another example:

First of all the problem to solve is:

Design a program which counts all the vowels on a given page of text.

Remember in the section 2.4 we came up with a strategy for designing algorithms so use this as a
starting point:

Step 1: Investigation step

1. Identify the processes


2. Identify the major decisions
3. Identify the loops
4. Identify the variables

Step 2: Preliminary algorithm step

15
1. Devise a "high level" algorithm
2. Step through the algorithm. Does this "walk-through" reveal any major problems? If it
does then correct the problems.

Step 3: Refining the algorithm step

1. Incorporate any refinements indicated in step 2.


2. Group together processes where appropriate
3. Group together variables where appropriate
4. Test the algorithm again by stepping through it

1.1 Identify the processes

Beginners in algorithm design are often a bit uncertain about how to start identifying processes
but a useful starting point is to remember that processes do work, in other words a process is an
action that is carried out. Another problem for beginners is determining just how "big" a process
should be. An example here may be a good way to explain this. In this case of the vowel-
counting problem we could be bold and state it like this:

Count all the vowels

The implication here is that some processes do a great deal work hence "big" processes. This
process is the heart of the problem - counting vowels - but it gives no clue as to how the vowel-
counting will be done. We need to analyse the process further so that we can specify how vowels
can be recognised and counted. Since a vowel is a letter then one process we require is the
process of reading a letter in the text. A vowel is one of the set A,E,I,O or U so another process
needs to determine if the letter we have read is in this set. A third process is conditional on the
letter which was read and tested being a vowel, if it is we need to count it. Lastly we need to
determine if we have reached the end of the page.

So far we have the following processes:

1. Read a letter from the text


2. If the letter is a vowel then increment the vowel counter
3. Determine if the end of the page has been reached

You can see that in the process of identifying the processes we have also identified some
decisions, I've shown these in bold text, and some variables which I've shown in italic text. The
only thing in our strategy step 1 which seems not to have been done is to identify the loops. But
wait, there's more. What happens in process 3 if we haven't reached the end of the page? We
continue with the next letter, which will mean repeating the first two processes. We have even
found a loop and we are ready for strategy step 2, devising and testing the high level algorithm.
Here is my first attempt:

1. Read a letter from the text


2. If the letter is a vowel then increment the vowel counter

16
3. If the letter is not the last letter then got to step 1

Now do a "walk-through", that is go through each step of the algorithm and see if it works. Later
when we look at trace tables I will show you a more formal way to test the algorithm. Assume
we have a sample page which contains the text:

This is a page of text

We read a letter, the first letter 'T', we test it and find it isn't vowel so the vowel counter is not
incremented. We also (somehow) determine that this is not the end of the page so we go back to
step 1 and read the next letter 'h', and so on. That seems fine but there are still some problems.
For instance: how do we start at the first letter? what value does the vowel counter start with?
and how do we know when we have reached the end of the page? These problems aren't really an
issue for human readers since we have conventions which govern how text is read - for English
text you start at the top left and finish at the bottom right. And we assume the vowel counter is
zero at the start. However we are not designing an algorithm for a human we are designing an
algorithm to be coded for a computer and the machine will require all the conditions of the
algorithm to be precisely specified. To help us out assume we have two numeric variables
current_letter_position and last_letter_position. Assume also that we can determine or we know
how many letters are on the page which will be the value of last_letter_position. Later you will
see how we would do this in practice using sentinel values.

Given these variations we can now write an algorithm which looks like this:

1. Set current_letter_position to 1
2. Set last_letter_position to number of letters on the page
3. Set vowel_counter to 0
4. Read letter at current_letter_position
5. If letter is a vowel then increment vowel_counter
6. Increment current_letter_position
7. If current_letter_position <= last_letter_position go to step 4

The symbol <= in line 7 means less than or equal to.

This apparently simple example has turned out to be quite an exercise and it has also enabled us
to use the algorithm-designing strategy we met a couple of lessons ago. It is not far from being a
complete design for a computer program.

2.6 Trace tables

Since an algorithm is a sequence or series of steps which seek to solve a problem you can expect
that if you "freeze" the algorithm at any point then you have a snapshot of the state of all the

17
variables. The trace table is a very useful tool which allows you to see the state of your algorithm
with as much detail as you wish. It consists of a table in which each row shows the state of step
in the algorithm and each column shows the value of a variable at that step. The trace table
allows you to check the algorithm for errors.

Here is a trace table which shows the states of our vowel counting algorithm, first the algorithm
again:

1. Set curr to 1
2. Set last to number of letters on the page
3. Set count to 0
4. Read letter at curr
5. If letter is vowel then increment count
6. Increment curr
7. If curr <= last go to step 4

Note that I have abbreviated the variable names, this is so that I can keep the columns in the trace
table to a reasonable width. Each variable is shown in italic in the algorithm.

Next the data: A page

The algorithm has six variables - the numeric variables curr, last, count, the character variable
letter and the logical values letter is vowel and curr <= last so the table will need at least six
columns. Sometimes there is also a column which indicates a step number or which contains the
statement being executed.

Step curr last count letter letter is vowel curr <= last
1,2,3 1 6 0 ? ? ?
4 1 6 0 'A' ? ?
5 1 6 1 'A' True ?
6 2 6 1 'A' True ?
7 2 6 1 'A' True True
4 2 6 1 '' True True
5 2 6 1 '' False True
6 3 6 1 '' False True
7 3 6 1 '' False True
4 3 6 1 'p' False True
5 3 6 1 'p' False True
6 4 6 1 'p' False True
7 4 6 1 'p' False True
4 4 6 1 'a' False True
5 4 6 2 'a' True True
6 5 6 2 'a' True True
7 5 6 2 'a' True True
4 5 6 2 'g' True True
5 5 6 2 'g' False True

18
6 6 6 2 'g' False True
7 6 6 2 'g' False True
4 6 6 2 'e' False True
5 6 6 3 'e' True True
6 7 6 3 'e' True True
7 7 6 3 'e' True False

The trace table reveals some interesting things about algorithms. For instance it stresses that a
variable doesn't change value until it is re-evaluated by a statement which operates on the
variable. You can see in line two that the variable letter contains 'A' but the state of the condition
letteris vowel is unknown (?). Line three in the trace table shows the execution of statement five
of the algorithm - If letter is vowel then increment count - and now the variable letter is vowel in
the trace table takes on a value. Another thing shown by the trace table is the amount of work
done by algorithms which use repetition. Large repetitious algorithms with large amounts of data
will lead to enormous trace tables. The size of the table can be minimised by only showing the
start and stop states of a loop and then showing one iteration of the loop in a separate table. Yet
another thing revealed by the trace table is the initialisation of variables - setting variables to a
known state. Initialising variables is a good defensive algorithm design technique and is strongly
recommended. Remember that your algorithm will end up as a computer program and you
should not assume that the particular programming language will take care of the initialisation of
variables. It is good practice to assume that it will not.

Exercise 1:

1. Design a step-form algorithm to count the pages in a book.


2. Design a program which reads all the vowels in a book and which displays a count of vowels
on each page and finally a total of the number of vowels in the book.

19
2.7 Pseudocode

Pseudocode, like step-form, is a written way of stating algorithms but uses a much more
restricted vocabulary. It is very much like a 3GL and for many programmers and program
designers is the preferred way to state algorithms and program specifications.

Although there is no standard for pseudocode it is generally quite easy to read and use, for
instance here is some pseudocode:

DOWHILE kettle_empty
Add_Water_To_Kettle
END DOWHILE

As you can see it is a precise statement of a while loop.

Pseudocode is a written statement of an algorithm using a restricted and well-defined vocabulary.


In the next section you look at the vocabulary in detail.

2.7.1 Input, output, iteration, decision and processing in pseudocode

It is useful to separate the pseudocode into several groups as shown here:

Group Key words Example


Input/Output INPUT, READ INPUT counter
Used to get values from a data source, a
keyboard for instance DISPLAY new_value

DISPLAY
Used to output values to a data sink, a
screen or printer for instance
Iteration REPEAT SET count_value TO 0
statement REPEAT
UNTIL <condition> DISPLAY count_value
ADD 1 TO count_value;
The Repeat ... Until loop which was UNTIL count_value > 10
introduced in the introductory lesson on
algorithms. The REPEAT loop executes DOWHILE count_value <
the statement until the condition 10
becomes true. DISPLAY count_value
count_value =
DOWHILE <condition> count_value + 1
statement END DOWHILE
END DOWHILE
FOR count = 1 to 10
This is the While loop also introduced in DISPLAY count + count

20
the introductory lesson on algorithms. ENDFOR
The WHILE loop executes the statement
while the <condition> is true. The statements inside the
loops are indented to aid
FOR <var> = <start value> to <stop the readability of the
value> pseudocode.
ENDFOR

You haven't seen this loop before but it


is a special case of the While loop. The
FOR loop iterates for a fixed number of
steps. The While and Repeat loops can
be for a variable number of steps.
Decision IF <condition> THEN IF count > 10
statement THEN DISPLAY count
ENDIF ENDIF

IF <condition> IF count > 10


THEN statement THEN DISPLAY 'count >
ELSE statement 10'
ENDIF ADD 4 to sum
ELSE DISPLAY 'count
Both of these decision forms were first <= 10'
introduced in the algorithms ADD 3 to sum
introductory lesson. ENDIF
Processing ADD, SUBTRACT, COMPUTE, ADD 3 TO count
SET SUBTRACT 5 FROM
count
SET count TO 12
COMPUTE 10 + count
GIVING new_count

Although the processing group are very useful most program designers tend to prefer to use
operators like:

 + add
 - subtract
 * multiply
 / divide
 = assign

These are the arithmetic operators.

21
These are more intuitive since we use them in arithmetical expressions and they are easier to
write, for instance:

count = count + 22
x = count / 12
sum = sum - count
x = count * sum

The assignment operator, = (as count = count + 32) means assign the results of the operation on
the right-hand side to the variable on the left-hand side.

There are two other classes of operators which are useful in psuedocode. One class is called the
relational operators. These are used to evaluate the relationship between variables and are:

 == equal to (a pair of equals signs)


 <= less than or equal to
 >= greater than or equal to
 <> not equal to

Here are some examples of the use of the relational operators:

IF count == 32 THEN ... This decision states that if the variable count contains
32 execute the statement following THEN.
DOWHILE count <= 50 The statements in the while loop will execute while the
... value of count is 50 or less.
END DOWHILE
REPEAT The statements in the repeat loop will execute until the
... value of count exceeds 12.
UNTIL count > 12
IF count <> total THEN ... If the value of count is not the same as the value of
total execute the statement following THEN.

Don't confuse == (equal) with = (assignment). The first determines if a variable is the same as a
value or another variable, but the second (assignment) makes a variable become some value.
Remember == is two equals signs.

A statement like count <= 50 is a proposition, you might remember from an earlier lesson that a
proposition is a statement that has a truth value, it can be TRUE or FALSE. If count is less than
or equal to 50 then the proposition count <= 50 is true. For example some pseudocode might
help:

count = 51
IF count <= 50 THEN DISPLAY 'Count is less than 51' ENDIF
IF count > 50 THEN DISPLAY 'Count is greater than 50' ENDIF

22
The other class is called the logical operators which are used to form compound propositions and
the logical operators are:

 AND
 OR
 NOT

Using logical operators


AND IF (x = = 32) AND (y = = 7) THEN sumxy = x + y
OR IF (letter = = 'A') OR (letter = = 'E') THEN DISPLAY 'Vowel'
NOT IF NOT (letter = = 'A') THEN DISPLAY 'Not letter A'

The logical operators can be used to combine relational expressions.

The statement IF (x == 32) AND (y == 7) THEN sumxy = x + y says that if it is true that x ==
32 AND it is also true that y == 7 then add x and y together and store the result in variable
sumxy.

The statement IF (letter == 'A') OR (letter == 'E') THEN DISPLAY 'Vowel' says that if the
variable letter contains either 'A' or 'B' then display the word vowel on the output device. The
whole expression (letter == 'A') OR (letter == 'E') is false if both letter == 'A' is false and letter
== 'E' is false.

The NOT operator in the last example statement IF NOT (letter = 'A') THEN DISPLAY 'Not
letter A' is used to reverse the sense of a relational expression. If it is true that letter is equal to
'A', the NOT operator in NOT (letter = 'A') will give a value of FALSE.

Here are some examples using the relational


and logical operators, assume that A
contains 20 and B contains 15
Expression Result
A >= 20 TRUE
A > 20 FALSE
A == B FALSE
A == B + 5 TRUE
(A > B) AND (A > 20) FALSE
(A > B) OR (B > A) TRUE
(A < B) OR (B > A) FALSE
NOT (A > B) FALSE
NOT (NOT (A > B)) TRUE

23
2 .72 Problems with evaluating expressions:

What do we do with an expression like x = a * b + 7? If a is 10 and b is 5 is the answer 57 or is it


120? In other words do we do the multiplication part first (a * b) followed by the addition ( + 7)
or do we do the addition part first (b + 7) then multiply by a?

You might remember from school that there is an order of precedence for evaluating arithmetic
expressions. In my day it was called PODMAS or BODMAS which I think meant parentheses
(or brackets) outside divide, multiply, add, subtract. This means do what inside the parentheses
first, and do divide, multiply before add and subtract. If we apply the PODMAS rule in the case
of x = a * b + 7 the correct answer is 57.

The same applies in computer languages and for our operators, arithmetic, logical and relational
the order of precedence is:

 ( ) parentheses
 NOT
 * / AND
 + - OR
 == > < <> >= <=

The precedence is from top to bottom and on each line from left to right. Evaluate whatever is in
parentheses first, then NOT followed by *, /, AND then +, -, OR then last the relational
operators.

Probably the best way to start with pseudocode is to see an example so I've copied the vowel-
counting sample from an earlier lesson and re-written it using pseudocode:

Step-Form Pseudocode
1. Set curr to 1 curr = 1
2. Set last to 6 last = 6
3. Set count to 0 count = 0
4. Read letter at curr REPEAT
5. If letter is vowel then increment READ letter
count IF letter is vowel THEN count = count +
6. Increment curr 1
7. If curr <= last go to step 4 curr = curr + 1
UNTIL curr > last

Like the step-form the pseudocode executes from the first statement at the top down to left
obeying any branching statements that might occur in decisions and loops.

2.7.3 Algorithms and pseudocode

Whenever you are acquiring new skills there is no substitute for practice but first I will show you
another example.

24
The problem: Design a program which accepts as input two numbers in the range 0 to 99
inclusive and which sums the numbers, finds the difference of the numbers and displays both the
sum and the difference.

We have some processes:

 Get two numbers or get a number but do it twice


 Add the numbers together
 Find the difference between the numbers
 Display the sum
 Display the difference

and perhaps not so obvious,

 ensure that the numbers are in the range 0 to 99 inclusive.

We have some variables:

1. Number_1
2. Number_2
3. Sum
4. Difference

We have some decisions:

 Ensure that Number_1 is in the range 0 - 99


 Ensure that Number_2 is in the range 0 - 99

There are no loops that I can see so it's time to build a preliminary algorithm.

Here is a first effort:

Pseudocode algorithm example 1


INPUT Number_1 The first stab is OK except
INPUT Number_2 that I haven't checked to
Sum = Number_1 + Number_2 make sure the numbers are
Difference = Number_1 - Number_2 in the correct range. My next
DISPLAY Sum attempt is one way I could
DISPLAY Difference do that.
1 REPEAT You can see that I was
2 INPUT Number_1 wrong about there being no
3 UNTIL (Number_1 >= 0) AND (Number_1 <= 99)

25
loops in the algorithm.
4 REPEAT
5 INPUT Number_2 I've used repeat loops that
6 UNTIL (Number_2 >= 0) AND (Number_2 <= 99) will ensure that Number_1
and Number-2 are in the
7 Sum = Number_1 + Number_2 correct range. In the case of
8 Difference = Number_1 - Number_2 Number_1 the loop will not
9 DISPLAY Sum terminate until it is true that
10 DISPLAY Difference Number_1 is greater than or
equal to 0 AND it is true that
Number_1 is less than or
equal to 99.

The numbers next to the


statements are just for the
trace table which follows.

Variable names abbreviated to minimise the column widths:

Step N1 N2 Sum Diff N1 >= 0 N1 <= 99 N1 >= 0 AND N2 >= 0 N2 <= 99 N2 >= 0 AND
N1<= 99 N2 <= 99
1,2,3 -1 ? ? ? FALSE TRUE FALSE ? ? ?
1,2,3 102 ? ? ? TRUE FALSE FALSE ? ? ?
1,2,3 10 ? ? ? TRUE TRUE TRUE ? ? ?
4,5,6 10 6 ? ? TRUE TRUE TRUE TRUE TRUE TRUE
7 10 6 16 ? TRUE TRUE TRUE TRUE TRUE TRUE
8 10 6 16 6 TRUE TRUE TRUE TRUE TRUE TRUE

In the first two lines of the trace table you can see that since the values of Number_1 are outside
the range the expression (Number_1 >= 0) AND (Number_1 <= 99) evaluates to FALSE. This
means that the algorithm stays in the repeat loop.

2.7.4 Nesting loops - loops within loops

The situations where nested loops might be used are quite common, they are the kind of situation
where we need to get a piece of data from a number of pieces and carry out some processing on
each piece. For example we might be reading student records from a disk drive and for each
student record we might count the number of subjects that the student is enrolled in and calculate
what fees the student should pay.

26
Here are some examples of the structure of nested loops:

REPEAT DOWHILE In_Loop_1


REPEAT DOWHILE In_Loop_2
... ...
UNTIL Inside_Loop_Done END DOWHILE
UNTIL Outside_Loop_Done END DOWHILE

REPEAT DOWHILE In_Loop_1


REPEAT DOWHILE In_Loop_2
REPEAT DOWHILE In_Loop_3
... ...
UNTIL End_1 END DOWHILE
UNTIL End_2 END DOWHILE
UNTIL End_3 END DOWHILE

To demonstrate the use of nested loops I'll borrow the student records example from above.
Assume that we have a database of student records and each student record is something like:

 Student name
 Subject title, subject cost
 Subject title, subject cost
 Subject title, subject cost
 Subject title, subject cost

There are one hundred students and each student is enrolled in four subjects. I need to know the
total fees that will be collected.

Repeat and While nested loops


st_count = 0 st_count = 0
total_fee = 0 total_fee = 0
REPEAT DOWHILE st_count < 100
INPUT student_name INPUT student_name
subject_count = 0 subject_count = 0
REPEAT DOWHILE subject_count < 4
INPUT subject_title INPUT subject_title
INPUT subject_cost INPUT subject_cost
total_fee = total_fee + subject_cost total_fee = total_fee + subject_cost
subject_count = subject_count + 1 subject_count = subject_count + 1
UNTIL subject_count = 4 END DOWHILE
st_count = st_count + 1 st_count = st_count + 1
UNTIL st_count = 100 END DOWHILE

There is no real limit to how deeply loops can be nested but it is not common to find loops nested
more than three deep.

27
2.7.5 Multiway selection - nesting decisions

So far we have used simple single-level or two-way decision constructs but we can nest
decisions within decisions to get multiple-level or multiway decision constructs:

IF number > 0
THEN IF number < 101
THEN CALL Process_Number(number)
ENDIF
ENDIF

This example terminates each IF ... THEN with ENDIF but this isn't absolutely necessary:

IF number > 0
THEN IF number < 101
THEN CALL Process_Number(number)
ENDIF

The general guide is that ENDIF is used where it is needed to clarify or emphasise where an IF
ends. The next example uses IF ... THEN ... ELSE but only one ENDIF. My argument is that in
this example the ENDIF for the inner decision isn't necessary since it is obvious where the inner
decison ends.

IF number >= 1
THEN IF number <= 50
THEN DISPLAY 'In the range 1 to 50'
ELSE DISPLAY 'In the range 1 to greater than 50'
ELSE DISPLAY 'Number is negative'
ENDIF

If it was obvious where the inner decision ended in the previous example, what about this one?

IF number >= 1
THEN IF number <= 50
THEN DISPLAY 'In the range 1 to 50'
ELSE DISPLAY 'Number is negative'
ENDIF

Which IF does the ELSE belong to? The general rule is that an ELSE belongs to the last IF and
in this example the pseudocode would look like this:

IF number >= 1
THEN IF number <= 50
THEN DISPLAY 'In the range 1 to 50'
ELSE DISPLAY 'Number is negative'
ENDIF

28
and this would be wrong, our program would be syntactically correct but logically wrong. We
would have to include the ENDIF:

IF number >= 1
THEN IF number <= 50
THEN DISPLAY 'In the range 1 to 50'
ENDIF
ELSE DISPLAY 'Number is negative'
ENDIF

There is no limit to how deeply decisions can be nested:

IF condition1
THEN do_thing_1
ELSE IF condition2
THEN do_thing_2
ELSE IF condition3
THEN do_thing_3
ELSE IF condition4
THEN do_thing_4
ELSE do_thing_5

In this example we have a number of conditions (1 to 4) any one of which might be true. If
condition1 is true then we execute the process do_thing_1 and the multiway selection is finished.
If condition1 isn't true then we go on to evaluate the remaining conditions. If none of the
conditions is true then we execute the subprocess do_thing_5.

2.7.6 The compound statement and indentation

You have probably noticed that I have tended to use


indentation within loops. There is a method to my
madness. One aim is to make the algorithms easier
to read; the other is to stress that processes that are
indented to the same level belong together. In the
diagram here I have exaggerated and outlined the
indentation so that you can see how the statements
in the algorithm belong together. In some cosmetic
respects it isn't necessary to use indentation but
there is a situation where the use of indentation is
mandatory, otherwise your program designs are
going to be misleading or, worse, wrong.

29
For instance: Is the piece of pseudocode on the left different from the piece on the right?

x = 22 x = 21
y=1 y=1
IF x = = 22 IF x = = 22
THEN y = x + 7 THEN y = x + 7
z=y+2 z=y+2
j=z+9 j=z+9
DISPLAY y, z, j DISPLAY y,z,j

There is no obvious difference except that the code on the right uses indentation and the
indentation is meant to show that the statements y = x + 7, z = y + 2 and j = z + 9 will only
execute if x = = 22 is true. This kind of arrangement of statements is called a compound
statement.

Often when using the IF statement it isn't necessary to use ENDIF but in cases where compound
statements are used it is better to make use of ENDIF to avoid any confusion:

x = 22 x = 21
y=1 y=1
IF x = = 22 IF x = = 22
THEN y = x + 7 THEN y = x + 7
ENDIF z=y+2
z=y+2 j=z+9
j=z+9 ENDIF
DISPLAY y, z, j DISPLAY y,z,j

2.7.7 Subprocesses and subroutines

The last topic in this lesson on pseudocode deals with an important concept in program design -
the concept of the subprocess. Subprocesses are also known as subroutines, subprograms,
procedures and functions. The topic isn't essential to your understanding of pseudocode and you
might decide to skip it for now. However it will be very useful to read this if you are planning to
go on to do some programming.

The subprocess is important for a number of reasons, amongst these are:

 it provides a means of simplifying programs by making common processes available to a


wide number of programs
 it permits the modularisation of complex programs
 it makes for more reliable programs since once it is shown that a process works then it
can be made a subprocess and need not be tested again

30
In pseudocode we can write a process and give it a name, for example:

GET_NUMBER: REPEAT
INPUT X
UNTIL (X >= 0) AND (X <= 99)
RETURN X

You will recognise this as the first repeat loop from Pseudocode algorithm example 1 except
that I've added a couple of things. I've named the repeat loop GET_NUMBER: and I've added a
new pseudocode statement RETURN.

It might help you to understand it if I first use it then I explain it.

Using CALL, RETURN and END


CALL GET_NUMBER (Number_1) The CALL statement is another new pseudocode
CALL GET_NUMBER (Number_2) statement. It directs the algorithm to find a
Sum = Number_1 + Number_2 subprocess called GET_NUMBER and execute it.
Difference = Number_1 - Number_2 The CALL statement also passes a parameter in
DISPLAY Sum parentheses (Number_1 or Number_2) to the
DISPLAY Difference subprocess. In effect CALL transfers the sequence
END to the subprocess. When the subprocess finishes it
has a RETURN statement which returns to the main
GET_NUMBER: program to the statement following the original
REPEAT CALL. The RETURN statement also returns a value
INPUT X for the parameter.
UNTIL (X >= 0) AND (X <= 99)
RETURN X In this example the first CALL has Number_1 as a
parameter and the subprocess returns whatever
value X had which becomes Number_1. The second
CALL has Number_2 as a parameter and the
subprocess returns whatever value X had which
now becomes Number_2.

The END statement at the end of the main program


separates the main program from the subprocess or
subprocesses if there happens to be more than one.

31
Here is a diagram which might help explain the concept:

Exercise 2:

1. Given a classroom full of people:

 find the average height


 the number of people
 the height of the tallest person
 the height of the smallest person

All measurements should be in metres.

2. Assume you have the following data stored somewhere:

Fred,Joan,Brian,Bert,Selie,Sue,Jack,Ng,Jacques,CLASS,Chris,Cheryl,Pam,Allan,CLASS
,END and it represents students in different classes. What might be the sentinel values in
the data?

3. Design a program which:

a) reads the data and displays the names of the students in the class
b) counts the number of students in each class
c) counts the number of classes

32
2.7.8 Protecting programs from errors

It is a fact of life in program design but there seems to be always one last bug or error to be
corrected. We can broadly classify the errors as:

 Syntax errors - this class of error means that a mistake is made in the language used to
state the algorithm.
 Logic errors - the algorithm is syntactically correct but doesn't do what is intended.
 Data range and data type errors - the algorithm is syntactically correct and logically
correct but can be threatened by the wrong kind of data or by values which are out of
range.

The syntax errors aren't a serious issue during the program design phase since in practice, after
designing and testing the design, the program will be implemented in a computer program
language and it is at this point that syntax errors become a problem. Even so syntax errors are a
minor problem since the process of building the program will capture the errors. The program
simply won't build until all the syntax errors are removed. If you do some programming after this
course you will get to understand the issue of syntax errors.

The logic errors are a much more serious problem since there is no way to eliminate these other
than rigorously testing the program design.

The data errors are also serious errors and in some respects are harder to deal with than logic
errors.

2.7.9 Testing program designs

A simple strategy for testing our designs and this should be enough for the present. A strategy for
testing program designs is by :

1. Use a trace table to test program flow and the states of variables. This is quite a practical
way to test small programs but can become impractical with large and complex programs.
2. Trying to test all data values. Again this is practical for small programs but not for
programs which process large and diverse amounts of data. The best approach here is to
test the data boundaries and we look at that next.

2. 7.10 Error data and boundary conditions.

Much of the data we use comes from limited sets of data, for example:

 positive integers (whole numbers)


 letters of the alphabet ('A' to 'Z', 'a' to 'z')
 days of the week

For the set of positive integers a negative number or a real number like 98.675 are invalid data.
For the set of letters of the alphabet the digits 1, 2, 3 etc are invalid data. We can say for instance

33
that the boundaries of the set of letters are the letters 'A' and 'Z' or the lower case letters 'a' and
'z'. We can set boundaries in our programs:

DOWHILE (number > 0) AND (number < 101)


INPUT number
IF (number > 0) AND (number < 101)
THEN CALL Process_Number(Process_Number)
ELSE DISPLAY 'Error: invalid data was entered.'
ENDIF
END DOWHILE

Here we have set boundaries for the while loop and the call to Process_Number. The only integer
values which will take part in processing are integers in the range 1 to 100 inclusive.

Examples like this don't tell the whole story. We can ensure that we don't process an integer
value outside the boundaries but how do you protect this program from someone who enters a
real number instead of an integer? For example the value 17.543 is in the range of 1 to 100 but it
is not an integer. It is valid in the sense that it is inside the boundaries but invalid because it is the
wrong type of data. This is quite a difficult issue to deal with and will be left for a later course
but the principle is that the input to the program is screened on a character by character basis. In
the case of entering positive integers into a program the only valid characters are the digits '0' to
'9' and everything else, decimal points, negative signs, letters etc will be rejected. The sample
above might be re-written as:

DOWHILE (number > 0) AND (number < 101)


CALL Input_Positive_Integer( number)
IF (number > 0) AND (number < 101)
THEN CALL Process_Number(number)
ELSE DISPLAY 'Error: invalid data was entered.'
ENDIF
END DOWHILE

You can see that there is a subprocess Input_Positive_Integer which will return a positive
integer. How it does it isn't our business at the moment.

2.7.11 Terminating loops with sentinel values

A sentinel or guard value is a value which is used to terminate a loop. A typical use might be in
the situation where a loop is used to accept values from the input device and a specific value
indicates the end of data input. Here is an example:

price = 0
total = 0
DOWHILE price >= 0
DISPLAY 'Enter the value of the item sold. A negative amount will terminate input.'
INPUT price

34
total = total + price
END DOWHILE
DISPLAY total

In this example the sentinel value is any negative amount. If the user of the program enters a
negative amount then the while loop terminates.

The trace table for the loop will be something like this:
statement price total price >=
0
1 price = 0 0 ? ?
2 total = 0 0 0 ?
3 DOWHILE price >= 0 0 0 TRUE
4 INPUT price 12.50 0 TRUE
5 total = total + price 12.50 12.50 TRUE
3 DOWHILE price >= 0 12.50 12.50 TRUE
4 INPUT price 22.00 12.50 TRUE
5 total = total + price 22.00 34.50 TRUE
3 DOWHILE price >= 0 22.00 34.50 TRUE
4 INPUT price -10.00 34.50 TRUE
5 total = total + price -10.00 24.50 TRUE
3 DOWHILE price >= 0 -10.00 24.50 FALSE

There is a problem isn't there? In this algorithm the sentinel value takes part in the processing of
the total but the only purpose of the sentinel value is to terminate the loop. The result is that total
is wrong. This is a logic error in the algorithm and the algorithm needs to be modified:

price = 0
total = 0
DOWHILE price >= 0
DISPLAY 'Enter the value of the item sold. A negative amount will terminate input.'
INPUT price
IF price >= 0 THEN total = total + price
ENDIF
END DOWHILE
DISPLAY total

In the lesson on step-form design we had an example based on reading letters from the page of a
book and determining if a letter was a vowel:

1. Set curr to 1
2. Set last to number of letters on the page
3. Set count to 0
4. Read letter at curr
5. If letter is vowel then increment count
6. Increment curr
35
7. If curr <= last go to step 4

Step 2 assumed that somehow we knew what the number of letters on any given page was. In
many practical situations we won't be so fortunate as to know how letters there are on each page
but we almost certainly will be able to use sentinel values since each page will be separated by
an end of page marker or a new page marker. A book stored on a computer will usually be stored
page by page and each page is separated from the previous and next pages by some value we can
look for. In practice this is a letter or character called FORMFEED. We could amend the
algorithm to look for a FORMFEED:

count = 0
letter = ' '
DOWHILE letter <> FORMFEED
INPUT letter
IF letter is vowel THEN count = count + 1
ENDIF
END DOWHILE

You can see that this algorithm is quite different from the original. Apart from looking for
FORMFEED I've also gotten rid of the counter which kept track of which letter on the page we
were reading. I did this so that I could introduce the notion that each time a letter is read from
the page the algorithm will automatically move on to the next letter position.

2.7.12 The while loop and priming reads

There are situations where using sentinel values in while loops can get you into trouble. Here is
an example of a typical trouble situation:

We have some data - a shopping list:

eggs,milk,fruit,bread,juice,yoghurt,END

You can see I am very health conscious.

The sentinel value is END. The program calls the local grocer's computer via a modem and
transmits the shopping list. It uses the pseudocode:

CALL connect_to_grocer
IF connect_OK
THEN CALL send_credit_card_number
DOWHILE shopping_item <> 'END'
INPUT shopping_item
CALL transmit(shopping_item)
END DOWHILE
CALL disconnect_link

36
ELSE DISPLAY 'Connection not available'
ENDIF

The grocer charges $5 for the use of the dial-in order system no matter whether you order a 100
items or 0 items. With the shopping list eggs,milk,fruit,bread,juice,yoghurt,END everything
works OK but what if the shopping list is empty? That is, it just contains:

END

We send an empty shopping list to the grocer and he charges us $5 for the privilege. It would be
better to only enter the while loop which actually sends the shopping list if there is a shopping
list to send - we need to set up or prime the while loop. This means that we must establish the
while loop condition - in this example shopping_item <> 'END' - before the while loop starts.

Dial-in shopping list algorithm


CALL connect_to_grocer To prime the loop I have done two
IF connect_OK things:
THEN CALL send_credit_card_number
INPUT shopping_item 1. a read from the shopping list
DOWHILE shopping_item <> 'END' before the loop starts
CALL transmit(shopping_item) 2. moved the INPUT in the
INPUT shopping_item while loop to the end of the
END DOWHILE loop
CALL disconnect_link
ELSE DISPLAY 'Connection not available' The first sets the state of
ENDIF shopping_item <> 'END', the second
ensures that the first INPUT isn't
over-written in the event of a valid
shopping list, and that the last thing
done in the loop, apart from getting
data, is the setting of the state of
shopping_item <> 'END'.

Exercise 3:

1. You need to pack some books in cardboard boxes. Design a program which measures the
volume of boxes by accepting as input W (width), H (height), B (breadth). Since only
positive integer values are permitted assume that you have a subprocess called
Input_Positive_Integer and use it. You don't have to write the pseudocode for this
subprocess. The permissible range of volumes is 100 volume units to 600 volume units
inclusive. Any box outside this range is rejected. The books you are packing amount to 6000
volume units in total.
2. The dial-in shopping list algorithm has room for improvement. What problems does it have?
Rewrite the algorithm to overcome the problems.

37
2.8 Flowcharts

Step-form and pseudocode program designs are both text-based, the statements are written. Flow
charts are a graphical method of designing programs and once the rules are learned are very easy
to draw. The basic symbols used with flow charts are as given in Figure 2.1.

INPUT/ OUTPUT

A
A

Figure 2.1: Basic Symbols used in flowcharts

The major symbols are the DECISION (also known as selection) and the SEQUENCE (or
process) symbols. The START and STOP symbols are called the terminals. The SUBPROCESS
symbol is a variation on the sequence symbol. There are also connectors drawn between the
symbols and you will see these used in the examples below. The INPUT/OUTPUT used to
represent input and output. The connectors are used to indicate continuation of broken flow
charts.

There are some important rules concerning the symbols and these rules apply also to other ways
of stating algorithms:

 Processes have only one entry point and one exit point.
 Decisions have only one entry point, one TRUE exit point and one FALSE exit point.

38
2.8.1 Repeat loop

The repeat loop is represented in a flowchart as shown in the Figure 2.2.

Figure 2.2: Flowchart representation of a repeat loop

Note that the repeat loop has the process preceding the decision. This means that a repeat loop
will always execute the process part at least once. This is an important point to remember
because it may not be what you want to do. For instance assume you are a world power in
control of an arsenal of nuclear weapons and have written a program to launch missiles in the
event of an attack. Your program contains a loop which launches a missile each time you are
struck by an enemy missile, for example:

REPEAT
LAUNCH MISSILE
UNTIL ENEMY STOPS

Is a repeat loop a good idea in this case? Probably not since, if we assume you are not under
attack and you run the program, the repeat loop executes the process at least once and you will
probably start the next world war. A while loop would be a safer and more humane choice

2.8.2 While loop


The while loop is basically the reverse of the repeat loop, the decision comes first, followed by
the process. The while loop is usually written so that it iterates while the condition is true, the
repeat iterates until the condition becomes true. An interesting question is: When should a repeat
loop be used rather than a while loop? and vice-versa. The while loop should be used when it is
possible that the process or processes which are in the scope of the decision (that is, in the loop)
may not need to execute. For example assume you have a designed an air-conditioner controller
program and the program turns on the compressor while the ambient temperature is above the

39
desired temperature. A while loop is a good choice here since the ambient temperature may be at
the desired level before the compressor part of the program is executed. If a repeat loop was used
then the compressor would be turned on but it wouldn't be necessary. That would be wickedly
ignorant of green sensitivities. A repeat loop would be a good candidate for the kind of situation
in which a program needs to check for an external event at least once. For example: assume you
have now written a program for a video cassete recorder and it has a menu for doing things like
tuning TV channels, setting the date and time, programming events and so on. When the menu is
displayed it is a QUIT option along with all the others, the VCR doesn't know which option will
be chosen so it stays in the menu mode, that is repeats it, until QUIT is selected.

The while loop is represented in a flowchart as shown in the Figure 2.2.

Figure 2.2: Flowchart representation of a while loop

2.8.3 IF ... THEN

The IF ... THEN construct is shown here and is also known as the NULL ELSE, meaning that
there is no ELSE part. I have use lines with arrow-heads (connectors) to indicate the flow of
sequence. Although this is important in flow charts once you have gained some skill in using
them and if you draw them carefully you will find that determining the sequence is straight
forward. A typical rule is to use arrow-heads on connectors where flow direction may not be
obvious.

40
Figure 2.3: Flowchart representation of if then decision construct

2.8.4 IF ... THEN ... ELSE...

The IF ... THEN ... ELSE ... construct has a process at each branch of the decision symbol. The
only difference here is that each value of the decision (TRUE/FALSE) has a process associated
with it.

Figure 2.4 : Flowchart representation of if then decision construct

41
2.8.5 Using flow charts to design programs

Example design of a flowchart is given below:

The algorithm sums all the even numbers between 1 and 20 inclusive and then displays the sum.
It uses a repeat loop and contains a null else within the repeat loop.

The equivalent pseudocode is:

sum = 0
count = 1
REPEAT
IF count is even THEN sum = sum + count

42
count = count + 1
UNTIL count > 20
DISPLAY sum

You can see quite clearly from this example what the price of flow charting is. There is quite a
bit of drawing to do in addition to writing the legend in the symbols. The pseudocode is quite
simple by comparison so why would you use flow charts?

The major reasons are that the flow chart.

 is easier to read
 more closely follows a standard, this is not the the case with pseudocode
 probably lends itself more readily to computer-aided techniques of program design

2.8.6 Some rules for flow charts

Well-drawn flow charts are easy to read. What must you do to draw well-drawn flow charts?
Here are a few rules:

1. Every flow chart has a START symbol and a STOP symbol


2. The flow of sequence is generally from the top of the page to the bottom of the page. This
can vary with loops which need to flow back to an entry point.
3. Use arrow-heads on connectors where flow direction may not be obvious.
4. There is only one flow chart per page
5. A page should have a page number and a title
6. A flow chart on one page should not break and jump to another page
7. A flow chart should have no more than around 15 symbols (not including START and
STOP)

2.8.7 Flow charts and subprocesses

There is one last topic to do while we are running hot on flow charts - dealing with subprocesses.
Remember that when you studied pseudocode you learned about subprocesses and the benefits of
using them.

The subprocess is useful because:

 it provides a means of simplifying programs by making common processes available to a


wide number of programs.
 it permits the modularisation of complex programs.
 it makes for more reliable programs since once it is shown that a process works then it
can be made a subprocess and need not be tested again.

In flow charts subprocesses are also useful in dealing with the flow charting rule that a flow chart
should have no more than 15 or so symbols on a page. Here is an example of the use of
subprocess in flow charts.

43
A subprocess flow chart can contain other subprocesses, there is no limit to how deeply these
could be nested.

2.8.8 Using nested loops in flow charts

The nested while loop is shown here. This example is


much simplified, it doesn't show any initialisation of
either of the loops, the outer loop doesn't do any
processing apart from the processing the inner loop,
neither loop shows any statements which will lead to
the termination of the loops.

Each single step through the outer loop will lead to


the complete iteration of the innner loop. Assume
that the outre loop counts through 10 steps and the
inner loop through 100 steps. The sequence in the
inner loop will be executed 10 * 100 times. Nested
loops will do a lot of work.

44
The repeat loop shown here, like the while loop
example, is much simplified. It does show two
processes, sequence 1 and sequence 2, one process in
the outer loop and one process in the innner loop.

Like the while loop the nested repeat loop will see a
great deal of work done. If the outer loop does a
thousand iterations and the inner loops does a
thousand iterations then sequence 2 will be executed
1000 * 1000 times.

2.8.9 Using multiway selection in flow charts

The flow chart form of


multiway selection is shown
here. You can see it how it
shows quite clearly the notion
of decisions nested within
decisions.

If decision 1 is true then


sequence 1 is executed and the
multiway selection is finished.
If decision 1 is false then
decision 2 is tested, if this is
true then sequence 2 is done
and the multiway selection is
finished. If decision 2 is false,
you get the picture.

45
Exercise 4

1. Draw a flow chart and trace table for the following problem:

Fred sells bunches of flowers at the local shopping centre. One day Fred's boss, Joe, tells
Fred that at any time during the day he (Joe) will need to know:

o how many bunches of flowers have been sold


o what was the value of the most expensive bunch sold
o what was the value of the least expensive bunch sold
o what is the average value of bunches sold

2. With your answer for question 1 modify the flow chart so that it has a main flow chart and
shows each of the following as subprocess flow charts:
a. the initialisation of the variables
b. the process or processes for calculating how many bunches of flowers have been sold
c. the process or processes for calculating what was the value of the most expensive
bunch sold
d. the process or processes for calculating what was the value of the least expensive
bunch sold
e. the process or processes for calculating what is the average value of bunches sold
f. the display of all the results

3. Assume you have the following data stored somewhere:

Fred,Joan,Brian,Bert,Selie,Sue,Jack,Ng,Jacques,CLASS,Chris,Cheryl,Pam,Allan,CLASS,
END

and it represents students in different classes.

Design a program using flow charts which:

o reads the data and displays the names of the students in the class
o counts the number of students in each class
o counts the number of classes

4. Rewrite the nested loops and the multiway selection from above as pseudocode.

46
2.9 Nassi-Schneiderman diagram

The Nassi-Schneiderman (NS) diagram is another graphical method of stating algorithms.

This diagram indicates an algorithm with a process (Process 1) followed


by a decision. Each outcome of the decision has a process associated with
it. If the decision evaluates as true then Proc 2 is carried out, if false then
Proc 3 is carried out. In the table that follows you will see the symbols
that are usd in NS diagrams. As with flow charts the symbols are few and
easily remembered. The different symbols are "fitted" together like pieces
in a jigsaw puzzle.

2.9.1 The NS symbols and constructs

NS symbols and constructs


The process symbol looks much the same as
the program flow chart symbol. You'll see
soon though that NS diagrams look quite
different from the flow charts you have used
before.
The decision symbol is quite different. You
will see soon how it is used.

The repeat construct is also quite different


and like the while construct shown below it
gives a good indication of the scope of the
repeat. The repeat condition extends up on
the left of the processes it controls.

You can see that the while construct is the


reverse of the repeat. The while condition
extends down on the left to border the
processes it controls.

47
2.9.2 Designing programs with NS diagrams

Drawing program designs with NS diagrams is quite simple but the NS method differs from the
program flow chart method. Apart from different symbols NS does not use connectors between
the symbols. A comparison of an NS chart and a flow chart is shown below:

As with the flow the direction of sequence in the NS chart is from top to bottom. There are some
problems with the NS charts that don't really occur with flow charts:

 It is not as easy to stress the process one entry-one exit point rule as it is in flow charts.
 The null-else is not really obvious.

48
 Modification of an NS diagram probably requires more work than the equivalent flow
chart.

Exercise 5

Sketch NS diagrams for the following pseudocode fragments:

(1) (2) (3)

IF x = = 100 X=1 X=0


THEN IF x == 120 REPEAT Y=0
THEN IF x == 140 X=X+1 DOWHILE X < 10
THEN DISPLAY 'A' DISPLAY X DISPLAY X
ELSE DISPLAY 'B' UNTIL X > 10 REPEAT
ELSE DISPLAY 'C' Y=Y+1
ELSE DISPLAY 'D' UNTIL Y > 5
ENDIF X=X+1
Y=0
END WHILE

49

You might also like