Sie sind auf Seite 1von 47

# ALGORITHMS, FLOWCHARTS, DATA TYPES

AND PSEUDOCODE
1. ALGORITHMS
The term algorithm originally referred to any computation performed via a set of rules
applied to numbers written in decimal form. The word is derived from the phonetic
pronunciation of the last name of Abu Ja'far Mohammed ibn Musa al-Khowarizmi, who
was an Arabic mathematician who invented a set of rules for performing the four basic
arithmetic operations (addition, subtraction, multiplication and division) on decimal
numbers.
An algorithm is a representation of a solution to a problem. If a problem can be defined
as a difference between a desired situation and the current situation in which one is, then
a problem solution is a procedure, or method, for transforming the current situation to the
desired one. We solve many such trivial problems every day without even thinking about
it, for example making breakfast, travelling to the workplace etc. But the solution to such
problems requires little intellectual effort and is relatively unimportant. However, the
solution of a more interesting problem of more importance usually involves stating the
problem in an understandable form and communicating the solution to others. In the case
where a computer is part of the means of solving the problem, a procedure, explicitly
stating the steps leading to the solution, must be transmitted to the computer. This
concept of problem solution and communication makes the study of algorithms important
to computer science.
Throughout history, man has thought of ever more elegant ways of reducing the amount
of labour needed to do things. A computer has immense potential for saving time/energy,
as most (computational) tasks that are repetitive or can be generalised can be done by a
computer. For a computer to perform a desired task, a method for carrying out some
sequence of events, resulting in accomplishing the task, must somehow be described to
the computer. The algorithm can be described on many levels because the algorithm is
just the procedure of steps to take and get the result. The language used to describe an
algorithm to other people will be quite different from that which is used by the computer,
however the actual algorithm will in essence be the same. An example of an algorithm
people use would be a recipe to make a cake.
"4 extra large eggs, beaten
1&1/2 C. stock
1/2 teaspoon salt
1 scallion, minced
1 C. small shrimp or lobster flakes
1 t. soy sauce
1 Tablespoon oil
1. Mix all the ingredients, except the oil, in a deep bowl.
Page no: 1

2.
3.
4.
5.

Put 1" water in wide pot, then place deep bowl of batter inside.
Cover pot tightly and steam 15 min.
Heat oil very hot and pour over custard.
Steam 5 more min.
Serves 4 people"

This breaks down 'Making Chinese egg custard' into smaller steps. To make the product
one still needs to know how to execute each of the steps in the procedure and understand
all of the terms.

Definition:
A procedure is a finite sequence of well-defined instructions, each of which can be
mechanically carried out in a finite amount of time.
The procedure must break up the problem solution into parts that the recipient party can
understand and execute. In the case of a computer, the problem solution is usually in the
form of a program that encompasses the algorithm and explains to the computer a clearly
defined procedure for achieving the solution. The procedure must consist of smaller steps
each of which the computers understand. There may be no ambiguities in the translation
of the procedure into the necessary action to be taken. A program is then just a specific
realisation of an algorithm, which may be executed on a physical device.
A computer is essentially a physical device designed to carry out a collection of primitive
actions. A procedure is a sequence of instructions written in terms of which evoke a
proper operation. To make effective use of an algorithm on a computer one must not only
find and understand a solution to the problem but also convey the algorithm to the
computer, giving the correct sequence of understood commands that represent the same
algorithm.
Definition:
An algorithm is procedure consisting of a finite set of unambiguous rules (instructions)
which specify a finite sequence of operations that provides the solution to a problem, or
to a specific class of problems for any allowable set of input quantities (if there are
inputs). In other word, an algorithm is a step-by-step procedure to solve a given
problem
Alternatively, we can define an algorithm as a set or list of instructions for carrying out
some process step by step. A recipe in a cookbook is an excellent example of an
algorithm. The recipe includes the requirements for the cooking or ingredients and the
method of cooking them until you end up with a nice cooked dish.
In the same way, algorithms executed by a computer can combine millions of elementary
steps, such as additions and subtractions, into a complicated mathematical calculation.
Also by means of algorithms, a computer can control a manufacturing process or co-

Page no: 2

ordinate the reservations of an airline as they are received from the ticket offices all over
the country. Algorithms for such large-scale processes are, of course, very complex, but
they are built up from pieces.
One of the obstacles to overcome in using a computer to solve your problems is that of
translating the idea of the algorithm to computer code (program). People cannot normally
understand the actual machine code that the computer needs to run a program, so
programs are written in a programming language such as C or Pascal, which is then
converted into machine code for the computer to run.
In the problem-solving phase of computer programming, you will be designing
algorithms. This means that you will have to be conscious of the strategies you use to
solve problems in order to apply them to programming problems. These algorithms can
be designed though the use of flowcharts or pseudocode.

2. FLOWCHARTS
Flowcharting is a tool developed in the computer industry, for showing the steps
involved in a process. A flowchart is a diagram made up of boxes, diamonds and other
shapes, connected by arrows - each shape represents a step in the process, and the arrows
show the order in which they occur. Flowcharting combines symbols and flowlines, to
show figuratively the operation of an algorithm.
In computing, there are dozens of different symbols used in flowcharting (there are even
national and international flowcharting symbol standards). In business process analysis, a
couple of symbols are sufficient. A box with text inside indicates a step in the process,
while a diamond with text represents a decision point. See the figure for an example.
If the flowchart is too messy to draw, try starting again, but leaving out all of the decision
points and concentrating on the simplest possible course. Then the session can go back
and add the decision points later. It may also be useful to start by drawing a high-level
flowchart for the whole organisation, with each box being a complete process that has to
be filled out later.
From this common understanding can come a number of things - process improvement
ideas will often arise spontaneously during a flowcharting session. And after the session,
the facilitator can also draw up a written procedure - a flowcharting session is a good way
of documenting a process.
Process improvement starts with an understanding of the process, and flowcharting is the
first step towards process understanding.

Page no: 3

Flowcharting Symbols
There are 6 basic symbols commonly used in flowcharting of assembly language
programs: Terminal, Process, input/output, Decision, Connector and Predefined Process.
This is not a complete list of all the possible flowcharting symbols, it is the ones used
most often in the structure of Assembly language programming.
Symbol

Name

Function

Process

## Indicates any type of internal operation

inside the Processor or Memory

input/output

## Used for any Input / Output (I/O) operation.

Indicates that the computer is to obtain data
or output results

Decision

## Used to ask a question that can

be answered in a binary format (Yes/No,
True/False)

Connector

## Allows the flowchart to be drawn without

intersecting lines or without a reverse flow.

Predefined Process

## Used to invoke a subroutine or an

interrupt program.

Terminal

## Indicates the starting or ending of the

program, process, or interrupt program.

Flow Lines

## Generally, there are many standard flowcharting symbols.

General Rules for flowcharting
1. All boxes of the flowchart are connected with Arrows. (Not lines)
2. Flowchart symbols have an entry point on the top of the symbol with no other
entry points. The exit point for all flowchart symbols is on the bottom except for
the Decision symbol.
3. The Decision symbol has two exit points; these can be on the sides or the bottom
and one side.

Page no: 4

4. Generally a flowchart will flow from top to bottom. However, an upward flow
can be shown as long as it does not exceed 3 symbols.
5. Connectors are used to connect breaks in the flowchart. Examples are:
From one page to another page.
From the bottom of the page to the top of the same page.
An upward flow of more then 3 symbols
6. Subroutines and Interrupt programs have their own and independent flowcharts.
7. All flow charts start with a Terminal or Predefined Process (for interrupt
programs or subroutines) symbol.
8. All flowcharts end with a terminal or a contentious loop.
Flowcharting uses symbols that have been in use for a number of years to represent the
type of operations and/or processes being performed. The standardised format provides a
common method for people to visualise problems together in the same manner. The use
of standardised symbols makes the flow charts easier to interpret, however, standardising
symbols is not as important as the sequence of activities that make up the process.
Flowcharting Tips

Chart the process the way it is really occurring. Do not document the way a written
process or a manager thinks the process happens.

People typically modify existing processes to enable a more efficient process. If the

desired or theoretical process is charted, problems with the existing process will not
be recognised and no improvements can be made.

## Note all circumstances actually dealt with.

Test the flow chart by trying to follow the chart to perform the process charted. If
there is a problem performing the operation as charted, note any differences and
modify the chart to correct. A better approach would be to have someone unfamiliar
with the process try to follow the flow chart and note questions or problems found.

Include mental steps in the process such as decisions. These steps are sometimes left

out because of familiarity with the process, however, represent sources of problems
due to a possible lack of information used to make the decision can be inadequate or
incorrect if performed by a different person.

## Examples of Algorithms and Flowcharts

Example 1. Design an algorithm and the corresponding flowchart for adding the test
scores as given below:
26, 49, 98, 87, 62, 75
Page no: 5

a) Algorithm
1. BEGIN
2.
Sum 0
3.
INPUT the first testscore
4.
5.
INPUT the second testscore
6.
7.
INPUT the third testscore
8.
9.
INPUT the Forth testscore
10.
11.
INPUT the fifth testscore
12.
13.
INPUT the sixth testscore
14.
15.
PRINT the sum
16. END
b) The corresponding flowchart is as follows:
Start
Sum 0
Get first testscore
To sum
Get second testscore
To sum
1

Page no: 6

1
Get third testscore

to sum
Get Forth testscore

to sum
Get Fifth testscore

## Get Six testscore

sum
Output Sum
STOP
The algorithm and the flowchart above illustrate the steps for solving the problem of
adding six testscores. Where one testscore is added to sum at a time. Both the algorithm
and flowchart should always have a Start step at the beginning of the algorithm or
flowchart and at least one stop step at the end, or anywhere in the algorithm or flowchart.
Since we want the sum of six testscore, then we should have a container for the resulting
sum. In this example, the container is called sum and we make sure that sum should start
with a zero value by step 2.

Page no: 7

Example 2: The problem with this algorithm is that, some of the steps appear more than
once, i.e. step 5 get second number, step 7, get third number, etc. One could shorten the
algorithm or flowchart as follows:
1.
2.
3.
4.
5.
6.
7.

BEGIN
sum 0
INPUT a value
sum sum + value
Go to step 3 to get next Value
PRINT the sum
END
Start
Sum 0
Get a value
Sum sum + value

Output
STOP
This algorithm and its corresponding flowchart are a bit shorter than the first one. In this
algorithm, step 3 to 5 will be repeated, where a number is obtained and added to sum.
Similarly the flowchart indicates a flowline being drawn back to the previous step
indicating that the portion of the flowchart is being repeated. One problem indicates that
these steps will be repeated endlessly, resulting in an endless algorithm or flowchart. The
algorithm needs to be improved to eliminate this problem. In order to solve this problem,
we need to add a last value to the list of numbers given. This value should be unique so
that, each time we get a value, we test the value to see if we have reached the last value.
In this way our algorithm will be a finite algorithm which ends in a finite number of steps
as shown below. There are many ways of making the algorithm finite.
The new list of numbers will be 26, 49, 498, 9387, 48962, 1, -1. The value 1 is a unique
number since all other numbers are positive.
1. BEGIN
2.
Sum 0
3.
INPUT a value

Page no: 8

4.
IF the value is equal to 1, go to step 7
5.
Add to sum ( sum sum + value)
6. Go to step 3 to get next Value
7. PRINT the sum
8. END
Corresponding flowchart

START
Sum 0

Get a value

Value
= -1

Yes

No
Sum Sum + Value

Output Sum

STOP

3. DATA TYPES
Although some contemporary languages allow programmers to invent his own data
types, and define their related operations, there are a number of traditional data types
found in most languages:
INTEGER
Integers are numeric data items, which are either positive or negative including zero, i.e.
1, 488, -22, 0, 456. Some programming languages put restrictions on the magnitude of
integers which may be used in program instructions. These restrictions are usually
dependent on the size of the memory location of the computer in which the language
may run.

Page no: 9

REAL
There are two types of real numbers, Fixed-Point and Floating Point.
Fixed Point
Fixed point data items are numbers which have embedded decimal point i.e. 1.5,
458.4589, -0.569.
Floating Point
Floating point data items are numbers, which are, held as binary fractions by a computer.
The numbers are expressed in a form where you have a mantissa and an exponent, for
example
Number
12.3
= 0.123 * 102
123000 = 0.123 * 106
0.000123 =0.123 * 10-3

Mantissa
0.123
0.123
0.123

Exponent
2
6
-3

Floating point representation of data is used to overcome the restrictions placed on the
magnitude of numbers by the size of computers memory locations.
CHARACTER
A single character, could be a digit, letter of the alphabet or symbol, for example:
"a", "@", "\$", "3", "x" etc.
STRING
String data, may consist of any digits, letters of the alphabet or symbols which,the internal
coding system of the computer is capable of representing. Many programming languages
require character data to be enclosed by quotation marks when used in program
instructions, for example PRINT HAPPY NEW YEAR.
BOOLEAN
Boolean data items are used as status indicators and may contain only one of two possible
values: True or False.
DATA ITEM
There are two basic methods of using data items in a program:
a)
Constants
Data items are sometimes required to keep their values throughout the program, hence the
term constant. A constant is a specific value or character string used explicitly in an
operation. Consider the constant values 47.5, 1, and 13 in the example below.
Multiply by 47.5
If = 13
Page no: 10

b)

## A variable is a symbolic name assigned to a data item by the programmer. At any

particular time, a variable will stand for one particular data, called the value of a variable,
which may change from time to time during a computing process. The value of a variable
may change many times during the execution of a program. A variable is usually given a
name by the programmer.
Assignment
The assignment operation has the form:
variable expression (Pseudocode)
variable := expression (Pascal), variable = expression (C/C++/Java)
The assignment operation is used to assign a name to a value. Thus it is used whenever
you need to keep track of a value that is needed later. Some typical uses include:

## initialize a variable ( count 0 )

increment/decrement a counter ( count count + 1 )

## accumulate values ( sum sum + item )

capture the result of a computation ( y 3*x + 4 )
swap two values ( t x; x y; y = t )

The assignment operator is not commute i.e. x = e is not the same as e = x. The variable
must be declared. Variables used in the expression must be defined (have values). The
type of the expression must be compatible with the type of the variable.
The order in which assignments are performed is important for example, if the first and
second assignments in the swap sequence were interchanged, x and y would end up
assigned to the same value. The input operation and the output operation share some of
the same constraints.

4. PSEUDOCODE
Pseudocode is one of the tools that can be used to write a preliminary plan that can be
developed into a computer program. Pseudocode is a generic way of describing an
algorithm without use of any specific programming language syntax. It is, as the name
suggests, pseudo code it cannot be executed on a real computer, but it models and
resembles real programming code, and is written at roughly the same level of detail.
Pseudocode, by nature, exists in various forms, although most borrow syntax from
popular programming languages (like PHP, C, Lisp, or FORTRAN). Natural language is
used whenever details are unimportant or distracting.
Page no: 11

Computer science textbooks often use pseudocode in their examples so that all
programmers can understand them, even if they do not all know the same programming
languages. Since pseudocode style varies from author to author, there is usually an
accompanying introduction explaining the syntax used.
In the algorithm design, the steps of the algorithm are written in free English text and,
although brevity is desired, they may be as long as needed to describe the particular
operation. The steps of an algorithm are said to be written in pseudocode.
Many languages, such as Pascal, have a syntax that is almost identical to pseudocode and
hence make the transition from design to coding extremely easy.
The following section deal with the control structures (control constructs) Sequence,
Selection and Iteration or Repetition.
CONTROL STRUCTURES OR LOGICAL STRUCTURES
The key to better algorithm design and thus to programming lies in limiting the control
structure to only three constructs. These are illustrated below:
The sequence structure
The first type of control structures is called the sequence structure. This structure is the
most elementary structure. The sequence structure is a case where the steps in an
algorithm are constructed in such a way that, no condition step is required. The sequence
structure is the logical equivalent of a straight line.
For example, suppose you are required to design an algorithm for finding the average of
six numbers, and the sum of the numbers is given. The pseudocode will be as follows
BEGIN
INPUT the sum
Average sum / 6
PRINT the average
END
The corresponding flowchart will appear as follows:

Page no: 12

Start
Get the sum

Average sum / 6
Output sum

Stop
Example 3: This is the pseudo-code required to input three numbers from the keyboard
and output the result.
BEGIN
sum, number1, number2, number3 AS INTEGER
INPUT number1, number2, number3
sum number1 + number2 + number3
PRINT sum
END
Example 4: The following pseudo-code describes an algorithm which will accept two
numbers from the keyboard and calculate the sum and product displaying the answer on
the monitor screen.
BEGIN
sum, product, number1, number2 AS REAL
PRINT Input two numbers
INPUT number1, number2
sum number1 + number2
print The sum is , sum
product number1 * number2
PRINT The Product is , product
END
Decision Structure or Selection Structure
The decision structure or mostly commonly known as a selection structure, is case where
in the algorithm, one has to make a choice of two alternatives by making decision
depending on a given condition.

Page no: 13

Selection structures are also called case selection structures when there are two or more
alternatives to choose from.
This structure can be illustrated in a flowchart as follows:

True

Condition

False

## In pseudocode form we get

IF condition is true THEN
ELSE
ENDIF
In this example, the condition is evaluated, if the condition is TRUE Task-A is evaluated
and if it is FALSE, then Task-B is executed.
A variation of the construct of the above figure is shown below

Condition

False

True

Page no: 14

## IF condition is true THEN

or
IF condition is true THEN do Task-A
ENDIF
In this case, if condition is false, nothing happens. Otherwise Task-A is executed.
The selection requires the following
Choose alternative actions as a result of testing a logical condition
Produce code to test a sequence of logical tests
Making Choices
There are many occasions where a program is required to take alternative actions. For
example, there are occasions where we need to take action according to the user choice.
All computer languages provide a means of selection. Usually it is in the form of If
statement and our pseudo-code is no exception to this.
We will use the if statement together with logical operators to test for TRUE or FALSE as
shown below.
(block structure)
(one liner)
IF a = b THEN
PRINT a = b
or
IF a = b THEN PRINT "a = b"
ENDIF
The action is only taken when the test is TRUE.
The logical operators used in our pseudo-code are
=
is equal to
>
is greater than
<
is less than
>=
is greater than or equal
<=
is less than or equal
<>
is not eaqual to
Example 5: The following shows how the selection control structure is used in a program
where a user chooses the options for multiplying the numbers or adding them or
subtracting.
BEGIN
choice AS CHARACTER
ans, number1, number2, AS INTEGER
PRINT choose one of the following
PRINT m for multiply
PRINT s for subtract
INPUT choice
PRINT input two numbers you want to use
INPUT number1, number2

Page no: 15

## IF choice = "m" THEN ans number1 * number2

IF choice = "a" THEN ans number1 + number2
IF choice = "s" THEN ans number1 -- number2
PRINT ans
END
Compound Logical Operators
There are many occasions when we need to extend the conditions that are to be tested.
Often there are conditions to be linked.
In everyday language we say things like If I had the time and the money I would go on
holiday. The and means that both conditions must be true before we take an action. We
might also say I am happy to go to the theatre or the cinema. The logical link this time
is or . Conditions in if statements are linked in the same way. Conditions linked with and
only result in an action when all conditions are true. For example, if a >b and a > c then
display a is the largest. Conditions linked with an or lead to an action when either or
both are true.
Example 6: The program is to input a examination mark and test it for the award of a
grade. The mark is a whole number between 1 and 100. Grades are awarded according to
the following criteria:
=
>
95 Distinction
=
>
80 Merit
=
>
60 Pass
<
60 fail
The pseudo-code is
BEGIN
mark AS INTEGER
INPUT mark
IF mark >= 95 THEN PRINT distinction
IF mark >= 80 AND mark < 95 THEN PRINT merit
IF mark >= 60 AND mark < 80 THEN PRINT pass
IF mark < 60 THEN PRINT fail
END
An if statement on its own is often not the best way of solving problems. A more elegant
set of conditions can be created by adding an else statement to the if statement. The else
statement is used to deal with situations as shown in the following examples.
Example 7: A person is paid at top for category 1 work otherwise pay is at normal rate.
IF work is category 1 THEN
pay-rate is top
ELSE
pay-rate is normal
ENDIF
The above is a block structure and below is a one liner structure:
IF work is category 1 THEN pay-rate is top ELSE pay-rate is normal
Page no: 16

The else statement provides a neat way of dealing with alternative condition. In pseudocode we write
IF work = cat1 THEN
p-rate top
ELSE
p-rate normal
ENDIF
or

## IF work = cat1 THEN p-rate top ELSE p-rate normal

The following example illustrate the use of IF ELSE statements in implementing double
alternative conditions:
IF salary < 50000 THEN
Tax 0
ELSEIF salary > 50000 AND salary < 100000 THEN
Tax 50000 * 0.05
ELSE
Tax 100000 * 0.30
ENDIF
The case statement
Repeating the if else statements a number of times can be somewhat confusing. An
alternative method provided in a number of languages is to use a selector determined by
the alternative conditions that are needed. In our pseudo-code, this will called a case
statement.
Example 8: The following program segment outputs a message to the monitor screen
describing the insurance available according to a category input by the user.
BEGIN
category AS CHARACTER
PRINT input category
INPUT category
IF category = "U" THEN
PRINT insurance is not available
ELSEIF category = "A" THEN
PRINT insurance is double
ELSEIF category = "B" THEN
PRINT insurance is normal
ELSEIF category = "M" THEN
PRINT insurance is medically dependent
ELSE
PRINT entry invalid
ENDIF
END
Page no: 17

## This can be expressed in a case statement as follows:

BEGIN
category AS CHARCTER
PRINT input category
INPUT category
CASE category OF
"U" : PRINT insurance not available
"A" : PRINT insurance is double
"B" : PRINT insurance is normal
"M" : PRINT insurance is normal
OTHERWISE PRINT insurance is normal
ENDCASE
END
Instead of using the word otherwise, one can use else.
Repetition or Iteration Structure
A third structure causes the certain steps to be repeated.

Condition

False

True

## The Repetition structure can be implemented using

Repeat Until Loop
The While Loop
The For Loop
Any program instruction that repeats some statement or sequence of statements a number
of times is called an iteration or a loop. The commands used to create iterations or loops

Page no: 18

are all based on logical tests. There three constructs for iterations or loops in our pseudocode.
The Repeat Until loop.
The syntax is
REPEAT
A statement or block of statements
UNTIL a true condition
Example 9: A program segment repeatedly asks for entry of a number in the range 1 to
100 until a valid number is entered.
REPEAT
PRINT Enter a number between 1 and 100
INPUT number
UNTIL number < 1 OR number > 100
Example 10. A survey has been carried out to discover the most popular sport. The
results will be typed into the computer for analysis. Write a program to accomplish this.
BEGIN
REPEAT
PRINT Type in the letter chosen or Q to finish
PRINT A: Athletics
PRINT S: Swimming
PRINT F: Football
PRINT Enter data
INPUT letter
IF letter = "A" THEN
athletics athletics + 1
IF letter = "S" THEN
swimming swimming + 1
IF letter = "F" THEN
football football + 1
IF letter = "B" then
UNTIL letter = "Q"
END
The WHILE loop
The second type of iteration we will look at is the while iteration. This type of conditional
loop tests for terminating condition at the beginning of the loop. In this case no action is
performed at all if the first test causes the terminating condition to evaluate as false.

Page no: 19

The syntax is
WHILE a condition is true
A statement or block of statements
ENDWHILE
Example 11: A program segment to print out each character typed at a keyboard until the
character "q" is entered.
WHILE letter <> "q"
INPUT letter
PRINT The character you typed is, letter
ENDWHILE
Example 12: Write a program that will output the square root of any number input until
the number input is zero.
BEGIN
number AS REAL
PRINT Type in a number or zero to stop
INPUT number
WHILE number <> 0
square number * number
PRINT The square of the number is, square
PRINT Type in a number or zero to stop
INPUT number
ENDWHILE
END
The FOR Loop
The third type of iteration, which we shall use when the number of iterations is known in
advance, is a for loop. This, in its simplest form, uses an initialisation of the variable as a
starting point, a stop condition depending on the value of the variable. The variable is
incremented on each iteration until it reaches the required value.
The pseudo-code syntax will be:
FOR initialize variable TO stopping value STEP increment
Statements
NEXT variable
Example 13.
FOR n 1 TO 4 STEP 1
PRINT Loop, n
NEXT n
The fragment of code will produce the output
Loop 1
Loop 2
Loop 3
Loop 4

Page no: 20

In the example, n is usually referred as the loop variable, or counting variable, or index of
the loop. The loop variable can be used in any statement of the loop. The variable should
not be assigned a new value within the loop, which may change the behaviour of the
loop.
In some cases, a variable has to be initialised before execution of the loop as shown in the
following example:
Example 14: Write a program to calculate the sum and average of a series of numbers.
The pseudo-code solution is:
BEGIN
n, count AS INTEGER
sum, number, average AS REAL
PRINT How many numbers do you want to input
INPUT count
sum 0
FOR n 1 TO count STEP 1
PRINT Input the number from your list
INPUT number
sum sum + number
NEXT n
average sum / count
PRINT The sum of the numbers is , sum
PRINT Average of the numbers is , average
END
Flowcharts have been used in this section to illustrate the nature of the three control
structures. These three are the basic control structures out of which all programs are built.
Beyond this, flowcharts serve the programmer in two distinct ways: as problem solving
tools and as tools for documenting a program.
Example
Design an algorithm and the corresponding flowchart for finding the sum of N numbers.
Pseudocode Program
BEGIN
Sum 0
PRINT Input N
INPUT n
FOR i 1 TO n STEP 5
INPUT value
sum sum + value
NEXT i
PRINT sum
END
In this example, we have used i to allow us to count the numbers, which we get for the
addition. We compare i with n to check whether we have exhausted the numbers or not in
order to stop the computation of the sum (or to stop the iteration structure). In such a
case, i is referred to as a counter.
Page no: 21

Start
sum 0
i1

Input a number

i>n

True

False

Output sum

## sum sum + number

Stop
ii+1

Exercise
1. Design an algorithm and the corresponding flowchart for finding the sum of
the numbers 2, 4, 6, 8, , n
2. Using flowcharts, write an algorithm to read 100 numbers and then display the
sum.
3. Write an algorithm to read two numbers then display the largest.
4. Write an algorithm to read two numbers then display the smallest
5. Write an algorithm to read three numbers then display the largest.
6. Write an algorithm to read 100 numbers then display the largest.

Page no: 22

## ALGORITHMIC PROBLEM SOLVING

In this chapter you will learn about
What an algorithm is.
The relationship between data and algorithm.
The characteristics of an algorithm.
Using pseudo-codes and flowcharts to represent algorithms.

32

Chapter 3

## Algorithmic Problem Solving

3.1 Algorithms
In Chapter 2, we expounded the working of problem solving from a general
perspective. In computing, we focus on the type of problems categorically
known as algorithmic problems, where their solutions are expressible in the
form of algorithms.
An algorithm 1 is a well-defined computational procedure consisting of a
set of instructions, that takes some value or set of values, as input, and
produces some value or set of values, as output. In other word, an algorithm
is a procedure that accepts data, manipulate them following the prescribed
steps, so as to eventually fill the required unknown with the desired value(s).

Input

Algorithm

Output

## Tersely put, an algorithm, a jargon of computer specialists, is simply a

procedure. People of different professions have their own form of procedure
in their line of work, and they call it different names. A cook, for instance,
follows a procedure commonly known as a recipe that converts the
ingredients (input) into some culinary dish (output), after a certain number of
steps.
An algorithm, whose characteristics will be discussed later, is a form that
embeds the complete logic of the solution. Its formal written version is
called a program, or code. Thus, algorithmic problem solving actually
comes in two phases: derivation of an algorithm that solves the problem, and
conversion of the algorithm into code. The latter, usually known as coding,
is comparatively easier, since the logic is already present it is just a matter
of ensuring that the syntax rules of the programming language are adhered to.
The first phase is what that stumbles most people, for two main reasons.
Firstly, it challenges the mental faculties to search for the right solution, and
secondly, it requires the ability to articulate the solution concisely into stepby-step instructions, a skill that is acquired only through lots of practice.
Many people are able to make claims like oh yes, I know how to solve it,
but fall short when it comes to transferring the solution in their head onto
paper.
Algorithms and their alter ego, programs, are the software. The machine
that runs the programs is the hardware. Referring to the cook in our analogy
again, his view can be depicted as follows:

The term algorithm was derived from the name of Mohammed al-Khowarizmi, a Persian mathematician in the ninth
century. Al-Khowarizmi Algorismus (in Latin) Algorithm.

3.1

Algorithms

33

ingredients

recipe
(software)

Cooking utensils
(hardware)

Ah-gong
bah-kut-teh

## The first documented algorithm is the famous Euclidean algorithm written

by the Greek mathematician Euclid in 300 B.C. in his Book VII of the
Elements. It is rumoured that King Ptolemy, having looked through the
Elements, hopefully asked Euclid if there were not a shorter way to geometry,
to which Euclid severely answered: In geometry there is no royal road!
The modern Euclidean algorithm to compute gcd (greatest common
divisor) of two integers, is often presented as followed.
1. Let A and B be integers with A > B 0.
2. If B = 0, then the gcd is A and the algorithm ends.
3. Otherwise, find q and r such that
A = qB + r where 0 r < B
Note that we have 0 r < B < A and gcd(A,B) = gcd(B,r).
Replace A by B, and B by r. Go to step 2.
Walk through this algorithm with some sets of values.

34

Chapter 3

## 3.2 Data Types And Data Structures

In algorithmic problem solving, we deal with objects. Objects are data
manipulated by the algorithm. To a cook, the objects are the various types of
vegetables, meat and sauce. In algorithms, the data are numbers, words, lists,
files, and so on. In solving a geometry problem, the data can be the length of
a rectangle, the area of a circle, etc. Algorithm provides the logic; data
provide the values. They go hand in hand. Hence, we have this great truth:
Program = Algorithm + Data Structures
Data structures refer to the types of data used and how the data are
organised in the program. Data come in different forms and types. Most
programming languages provides simple data types such as integers, real
numbers and characters, and more complex data structures such as arrays,
records and files which are collections of data.
Because algorithm manipulates data, we need to store the data objects
into variables, and give these variables names for reference. For example, in
mathematics, we call the area of a circle A, and express A in terms of the
radius r. (In programming, we would use more telling variable names such as
When the program is run, each variable occupies some memory location(s),
whose size depends on the data type of the variable, to hold its value.

## 3.3 Characteristics Of An Algorithm

What makes an algorithm an algorithm? There are four essential properties
of an algorithm.
1. Each step of an algorithm must be exact.
This goes without saying. An algorithm must be precisely and
unambiguously described, so that there remains no uncertainty. An
instruction that says shuffle the deck of card may make sense to
some of us, but the machine will not have a clue on how to execute it,
unless the detail steps are described. An instruction that says lift the
restriction will cause much puzzlement even to the human readers.
2. An algorithm must terminate.
The ultimate purpose of an algorithm is to solve a problem. If the
program does not stop when executed, we will not be able to get any
result from it. Therefore, an algorithm must contain a finite number
of steps in its execution. Note that an algorithm that merely contains
a finite number of steps may not terminate during execution, due to
the presence of infinite loop.

3.3

Characteristics Of An Algorithm 35

## 3. An algorithm must be effective.

Again, this goes without saying.

## 4. An algorithm must be general.

This means that it must solve every instance of the problem. For
example, a program that computes the area of a rectangle should work
on all possible dimensions of the rectangle, within the limits of the
programming language and the machine.
An algorithm should also emphasise on the whats, and not the hows,
leaving the details for the program version. However, this point is more
apparent in more complicated algorithms at advanced level, which we are
unlikely to encounter yet.

## 3.4 Pseudo-Codes And Flowcharts

We usually present algorithms in the form of some pseudo-code, which is
normally a mixture of English statements, some mathematical notations, and
selected keywords from a programming language. There is no standard
convention for writing pseudo-code; each author may have his own style, as
long as clarity is ensured.
Below are two versions of the same algorithm, one is written mainly in
English, and the other in pseudo-code. The problem concerned is to find the
minimum, maximum, and average of a list of numbers. Make a comparison.
Algorithm version 1:
First, you initialise sum to zero, min to a very big number, and max to a
very small number.
Then, you enter the numbers, one by one.
For each number that you have entered, assign it to num and add it to the
sum.
At the same time, you compare num with min, if num is smaller than min,
Similarly, you compare num with max, if num is larger than max, let max
After all the numbers have been entered, you divide sum by the numbers of
items entered, and let ave be this result.
End of algorithm.

36

Chapter 3

## Algorithmic Problem Solving

Algorithm version 2:
sum count 0

## { sum = sum of numbers;

count = how many numbers are entered? }
{ min to hold the smallest value eventually }
{ max to hold the largest value eventually }

min ?
max ?
for each num entered,
increment count
sum sum + num
if num < min then min num
if num > max then max num
ave sum/count

Note the use of indentation and symbols in the second version. What
should min and max be initialised with?
Algorithms may also be represented by diagrams. One popular
diagrammatic method is the flowchart, which consists of terminator boxes,
process boxes, and decision boxes, with flows of logic indicated by arrows.
The flowchart below depicts the same logic as the algorithms above.
start

Terminator box
Process box

sum count 0
min ?
max ?

Yes

Decision box

end of
input?
No
increment count
sum sum + num

num<min?
ave sum/count

min num

Yes

max num

No

num>max?
end

Yes

No

## Whether you use the pseudo-code or the flowchart to represent your

algorithm, remember to walk through it with some sets of data to check that
the algorithm works.

3.5

Control Structures 37

## 3.5 Control Structures

The pseudo-code and flowchart in the previous section illustrate the three types
of control structures. They are:
1. Sequence
2. Branching (Selection)
3. Loop (Repetition)
These three control structures are sufficient for all purposes. The sequence
is exemplified by sequence of statements place one after the other the one
above or before another gets executed first. In flowcharts, sequence of
statements is usually contained in the rectangular process box.
The branch refers to a binary decision based on some condition. If the
condition is true, one of the two branches is explored; if the condition is false,
the other alternative is taken. This is usually represented by the if-then
construct in pseudo-codes and programs. In flowcharts, this is represented by
the diamond-shaped decision box. This structure is also known as the selection
structure.
The loop allows a statement or a sequence of statements to be repeatedly
executed based on some loop condition. It is represented by the while and
for constructs in most programming languages, for unbounded loops and
bounded loops respectively. (Unbounded loops refer to those whose number of
iterations depends on the eventuality that the termination condition is satisfied;
bounded loops refer to those whose number of iterations is known beforehand.) In the flowcharts, a back arrow hints the presence of a loop. A trip
around the loop is known as iteration. You must ensure that the condition for
the termination of the looping must be satisfied after some finite number of
iterations, otherwise it ends up as an infinite loop, a common mistake made by
inexperienced programmers. The loop is also known as the repetition structure.
Combining the use of these control structures, for example, a loop within a
loop (nested loops), a branch within another branch (nested if), a branch within
a loop, a loop within a branch, and so forth, is not uncommon. Complex
algorithms may have more complicated logic structure and deep level of
nesting, in which case it is best to demarcate parts of the algorithm as separate
smaller modules. Beginners must train themselves to be proficient in using and
combining control structures appropriately, and go through the trouble of
tracing through the algorithm before they convert it into code.

3.6

Summary

## An algorithm is a set of instructions, and an algorithmic problem lends itself to

a solution expressible in algorithmic form. Algorithms manipulate data, which
are represented as variables of the appropriate data types in programs. Data
structures are collections of data.
The characteristics of algorithms are presented in this chapter, so are the
two forms of representation for algorithms, namely, pseudo-codes and
flowcharts. The three control structures: sequence, branch, and loop, which
provide the flow of control in an algorithm, are introduced.

38

Chapter 3

## Algorithmic Problem Solving

Exercises
In the exercises below, you are asked to find the answers by hand, and jot
down the steps involved for the generalized versions. You need not write
programs for them yet you will be doing that later you need only to
discover the steps and put them down into an algorithm.
1.

## Our coins come in denominations of 1 cent, 5 cents, 10 cents, 20 cents,

50 cents and 1 dollar. Assuming that there are unlimited number of
coins, how would you devise a coin-change algorithm to compute the
minimum number of coins required to make up a particular amount?
For example, for 46 cents you need 4 coins: two 20-cent coins, one 5cent coin, and one 1-cent coin.

2.

How would you sort 10 numbers in increasing order? Do you know any
of the basic sorting techniques?

3.

## A word is a palindrome if it reads the same when the order of its

characters is reversed. For instance, 123321, RADAR and step on
no pets are palindromes. Derive an algorithm to determine if a word is
a palindrome.

4.

Two words are anagrams of each other if they differ only by the order
of their characters. For example, HATE and HEAT, NOW and
WON, reset and steer are anagrams, but sell and less are not.
Write an algorithm to determine if two given words are anagrams.

5.

How do you test for primality? That is, given a positive integer, how
do you determine that it is a prime number?

6.

## In a mastermind game, the code maker hides a secret code consisting of

4 digits, and the code breaker is to provide 4-digit guess codes until he
gets the code right. The code maker gives feedback in this manner, a
sink is awarded to a perfect match of a digit plus its position, and a hit
refers to a right match of digit but in the wrong position. How do you
devise an algorithm to provide the correct feedback? First, work on the
special case that no digit in the code should be repeated. Then work on
the general case where digits can be duplicated.

Pseudo-Code Standards
<> : The angler brackets mean that user must provide the value of whatever is written within these brackets.
... : The three dots (ellipses) mean that items similar to previous can come here.
| : A pipe sign shows two or more options (on each side of the sign) either of which can be used.
IF : Anything written in capitol letters is a command and must be written exactly the way it is shown.

Pseudo-Code Commands

1. Data types
DATA TYPE

PURPOSE

INTEGER

REAL

BOOLEAN

## Logical. Only TRUE or FALSE.

CHARACTER

Any single character. Can't be used in arithmetic expressions. ("A", "1", "%"
"G", "-", "?", "5", "*", etc.)

STRING

## Any group of characters. Can't be used in arithmetic expressions.

("Ali Khan", "14-D/II Gulberg", "-123", "Hello!?!", "2*7/(3-2)" etc.)

DATE

## Date in any format. (12/12/2009, 01-03-2007 etc.)

2. Declaring variables
Syntax:

## <variable name> AS <data type>

Note: A variable name (also called an Identifier) must start with either an alphabet or an underscore followed by an
alphabet. It must not contain and special characters such as ^, & @ etc. Variable names should be meaningful, such as
student_name and not x or y etc. Also, try and keep them short so that they are easier to remember.

Page no: 23

3. Using variables
a) Hard coding values into variables
Ex:

a 10
name "Ali Khan"
price 345.98
average 45.77
total Price * Quantity

## b) Getting values from the user (the Input command)

Syntax: INPUT <variable>
Ex:

Input i
Input name
Input part_no, price, quantity

( Three values will be asked from the user and put into
these variables respectively )

## 4. Sending output to the screen

Syntax: PRINT <variable>
PRINT "any string"
PRINT <variable | constant> <operator> <variable | constant>
Ex:

PRINT
PRINT
PRINT
PRINT

i
"This is a message"
i+p
"The result is", i / 2

## ( the result of i + p will be printed )

( two items will be printed, the message in quotes and
the result of i / 2 )

PRINT

## ( this PRINT command without anything to print will

actually print an empty line )

Note: In some algorithms OUTPUT command is used instead of PRINT. They are both the same as far as pseudo-code is
concern. Just remember not to mix the two. Use only one of them throughout your code.

5. Decisions (branching)
a) Single-line 'IF' statement
Syntax:
IF <condition> THEN <statement> ( ELSE <statement> )
Note: Only one statement is allowed in both the true and false part. The ELSE part is optional and can be left is nothing is
desired in case the condition is found to be false. Note that there is no END IF in single-line IF condition.
Ex:
IF a < 10 THEN PRINT "a is less than 10" ELSE PRINT "a is not less than 10"
Note: Only one path will be taken depending on the evaluation of the condition. The other one will be ignored.
Ex:
IF is_working = TRUE THEN PRINT "Something is working" ( the ELSE part is optional. Nothing will
be done if is_working is equal to FALSE)

Page no: 24

## ( following block does not have the ELSE )

IF <condition> THEN

## b) Block 'IF' structure

Syntax: IF <condition> THEN
<statements>

or

<statements>
ENDIF

ELSE
<statements>

ENDIF
Note: A block 'IF' can be used when more than one statement is required in true and/or false sections. All block 'IF'
structures must be terminated with an ENDIF keyword to establish a clear end of the structure.
Ex:
IF a < 10 THEN

## PRINT "a is less than 10"

ELSE
PRINT "a is not less than 10"
ENDIF
Block 'IF' can be nested inside another block 'IF' structure.
Ex:

## IF tea = TRUE THEN

PRINT "Tea and cookies will be served"
ELSE
PRINT "Only tea will be served"
ENDIF
ELSE
PRINT "No tea can be served"
ENDIF

Note: Indentation is used to visually separate the IF condition with the nested IF conditions. Otherwise, reading the
nested structures becomes very difficult.
( following block does not have the ELSE )

## c) Block 'IF' with 'ELSEIF' clause

IF <condition> THEN

<statements>

<statements>

<statements>

or

<statements>
...

...

ELSE

<statements>

<statements>
ENDIF
Ex:

ENDIF

## IF tea = TRUE THEN

PRINT "Tea will be served"
ELSEIF coffee = TRUE THEN
PRINT "Coffee will be served"
ELSE
PRINT "Tea and coffee are not available"
ENDIF

Page no: 25

6. Iteration (Loops)
a) Counter loop: FOR ... NEXT
Syntax:

Ex:

## FOR <variable> <start value> TO <end value> STEP <increment value>

<statements>
NEXT <variable>

FOR i 1 TO 10 STEP 1
PRINT "This is line number", i
NEXT i

The STEP clause can be used to change the default increment value of 1.
Ex:

FOR i 1 TO 50 STEP 2
PRINT i
NEXT i

Usually, the start value is less than or equal to the end value of the counter variable. If the start value is greater than the
end value, then the STEP clause must be used and the step value must be a negative number. It is also possible to use a
variable instead of constants for the start and end values.
Ex:

p 1
FOR i 10 TO p STEP -1
PRINT i
NEXT i

## b) Conditional loop: WHILE ... ENDWHILE

Syntax:

WHILE <condition> DO
<statements>
ENDWHILE

Ex:

a 0
WHILE a < 10 DO
PRINT a
a a+1
ENDWHILE

Make sure that you increment then counter yourself, otherwise this loop will become an endless loop.
The WHILE loops runs 'while' ( as long as ) the condition remains TRUE.
The WHILE loops terminate when the condition becomes FALSE.

Page no: 26

## c) Conditional loop: REPEAT ... UNTIL

REPEAT

Syntax:

<statements>
UNTIL <condition>

a 1
REPEAT
PRINT a
UNTIL a > 10

Ex:

The REPEAT ... UNTIL loops run 'until' the condition becomes TRUE.
The REPEAT ... UNTIL loops terminate when the condition becomes TRUE.
The REPEAT ... UNTIL loops run as long as the condition remains FALSE.

7. Procedures
Syntax:

## PROCEDURE <procedure name> (parameter list)

<statements>
ENDPROCEDURE
OR
PROCEDURE <procedure name>
<statements>
ENDPROCEDURE

Ex:

## PROCEDURE Sum (x AS INTEGER, y AS INTEGER)

PRINT "Sum of two number is ", x + y
ENDPROCEDURE
PROCEDURE HelloWorld
Print "Hello to the World"
ENDPROCEDURE

To call a procedure:

<procedure name>

OR

Ex:

Sum

OR

Ex:

## Sum (a, 10)

HelloWorld

Page no: 27

Note: Procedures do not run by themselves. It must be called from the main program or another procedure to run. It is
also possible to write procedures that do not take any parameters (information sent to the procedure). In case one or
more parameters are sent to the procedure, remember the variables that are used to receive the data are local in scope
to the procedure. This means that if the calling program send a variable with the name 'a' and the procedure also has a
parameter variable named 'a' to receive the data, the two variables are different and change in one will not reflect in the
other.

8. Functions
FUNCTION <function name> (parameter list) AS <data type>
<statements>
ENDFUNCTION

Syntax:

OR
FUNCTION <function name> AS <data type>
<statement>
ENDFUNCTION
Ex:

## FUNCTION Sum (x AS INTEGER, y AS INTEGER) AS INTEGER

RETURN x + y
ENDFUNCTION
FUNCTION AMPM (hours AS INTEGER) AS STRING
IF hours > 1 AND hours < 12 THEN
RETURN "AM"
ELSEIF hours > 12 AND hours < 24 THEN
RETURN "PM"
ENDIF
ENDFUNCTION

To call a function:
Ex:

## <variable> = <function name> (parameter list)

p Sum (a, b)
p a + Sum (d, f)
IF sum (a, b) > 10 THEN PRINT "sum is greater"

Note: The only difference between a procedure and a function is that a function must return a value to the calling
routine.

Page no: 28

\$UUD\V
Pseudocode is a way to describe how to accomplish tasks using basic steps like those a
computer might perform. In this weeks lab, you saw how a form of pseudocode can be
used to program the Scribbler robot. The Scribbler control panel has a point and click
interface, but in the rest of the course you will write your own simple pseudocode to
express computations. The advantage of pseudocode over plain English (as you saw in
case of scribbler) is that it has a precise meaning. The exact syntax is not too important
what counts is expressing a computation clearly and precisely.
You can use the handout below in all your homework and exams (even "closed-book"
exams).

Variables
In pseudocode you will need to use variables to store data. You can think of these as
little boxes that hold a number, and you are allowed to look at whats in the box or
replace its contents with something else. We call whatever is inside the box the value
of the variable.
An array is a shorthand way of naming a bunch of variables. If A is an array of
length n, you can imagine it as n boxes lined up in a row. Then we write A[i] to refer
to the ith box. Heres a picture of what A might look like in memory:
A =

40.20

62.71

52.54

22.05

You can use arrays in pseudocode instructions the same way you use variables:
x A

## Sets x to the second value in the array A (here, 62.71)

A 2

Sets the third value in the array A to the value 2 (replacing 52.54)

Sometimes you will use a variable to specify which element of the array you mean:
y A[i]

## Sets y to the ith array value

Arrays can be multidimensional. While a one-dimensional array is like a list, a twodimensional array is like a grid. If A is a two-dimensional array, A[i][j] refers to the
value in row i, column j of the grid.

Page no: 29

Instructions

## A pseudocode program is written as a series of instructions that the computer executes

one at a time. These are several kinds of instructions:

Arithmetic Instructions
Arithmetic instructions affect values stored in variables, named pieces of memory.
These instructions take the form variable arithmetic expression. For example:
x5

yx

ij+1

## Sets i to the value j + 1; leaves j unchanged

There are only a few arithmetic operations allowed, these are addition, subtraction,
multiplication, and division. Note that exponentiation, logarithms, and more complex
operations are not basic.

Simple actions
Sometimes you can use a single instruction to specify a behavior. E.g.:
Move forward for 1s
Read the price from the next jar
Place the box back on the shelf.

Use instructions like these for actions that are secondary to your programs purpose.

Conditionals

Conditional (branch) instructions perform one set of actions only if some specified
condition is true and another set only if the condition is false. They take this form:
IF

## true/false condition THEN

First list of instructions

ELSE
Second list of instructions
ENDIF

Page no: 30

(You can omit the ELSE branch if you dont want to take any special action when
the condition is FALSE.)
Here is a simple example:
IF x is odd THEN
xx1
count count + 1
ELSE
xx2
ENDIF

This conditional checks whether x is odd. If so, it subtracts one from x and adds one
to count; otherwise, it divides x by two.
Its important to understand that the true/false condition is checked only once, at the
beginning of the conditional. If x is odd, the computer subtracts one from x, making
it even. However, the computer does not go on to divide x by two.

Loops
Loops perform a set of actions some number of times. One format performs an long
as a specified condition remains TRUE. It takes the form:
. These look like this:
WHILE condition

DO

List of instructions
ENDWHILE

Another flavor of loop makes the computer perform some instructions repeatedly as
long as a specified condition remains FALSE:
REPEAT
List of instructions
UNTIL condition

A third format of loop adds a variable that counts the passes through the loop:
FOR i a TO b STEP c
List of instructions
NEXT i

On the first pass, the variable i is set to a. At the start of each successive pass, i is
increased by c, until on the final pass it has the value b. In all, the computer

Page no: 31

performs the list of instructions b a + c times. You could use such a loop to add the
integers from 1 to 100 (inclusive) as follows:
Ex:

sum 0
FOR i 1 TO 100 STEP 1
sum sum + i
NEXT i

Ex:

## WHILE current < max

current current + 1
Other instructions
ENDWHILE

At the beginning of WHILE loop, the computer checks whether the condition is true. If
so, it executes the list of instructions. Then it checks again whether the condition is
true; if so, it executes the instructions again. This process is repeated until the
computer checks the condition and it is false. Heres an example:
What happens if current max the first time the computer evaluates the WHILE
instruction? In this case, the operations inside the loop are not executed at all.

## Input and Output

Sometimes you want to get values from outside the program. For example:
INPUT price

## Sets the variable price to a value from outside the program

Similarly, your program can present its results using instructions like these:

PRINT minimum

## Display the value of the variable minimum

program does to help humans understand it. A comment begins with two slashes //
and continues until the end of the line, for example:
fah ( 9 / 5 ) * cel + 32

## // convert celsius to fahrenheit

skip over them entirely. They do help make the meaning clearer to human readers.

Caveat

32

Pseudocode looks deceptively like English, and that is its advantage: it should be
differences between pseudocode and plain English: pseudocode is meant to be
executed verbatim, and not interpreted with human common sense. For example, a
conditional statement is executed only once, whereas a loop is repeated.
Understanding pseudocode
Just as a child first learns to understand language, and then to speak, and finally to write,
you also should first learn to understand pseudocode written by others and only then
In order to understand pseudocode, you dont just read it as you would a poem or a novel.
You work through it. You take a blank sheet of paper, designate some space on the paper
for the variables, arrays etc. Then you execute the pseudocode line by line on this data.
After you do this a few times, you begin to understand what it does.
Example 1: Vote counting machine
We illustrate this with the following program that counts votes for two candidates.
are given to us in an array A, and that there are n votes total. Lets also assume for
simplicity that each vote is stored as either 1 or 2.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16

17
18
19

BEGIN
v1 0

## // set initial vote counts to zero

// v1 holds the tally for candidate 1

v2 0
// v2 holds the tally for candidate 2
A [ 1 : i ] AS INTEGER // array declaration
FOR i 1 TO n STEP 1
INPUT A [ i ] // ask the user to enter "1" or "2"
NEXT i
FOR i 1 TO n STEP 1
// See who the next vote is for
IF A [ i ] = 1 THEN
// If its for candidate 1, then increment his tally
v1 v1 + 1
ELSE
// Otherwise its for candidate 2, so increment his tally
v2 v2 + 1
ENDIF
NEXT i
PRINT Totals:, v1, v2
END

33

Lets work through an example with this pseudocode and see exactly what happens when
we execute it. Suppose n = 5 and we input the following data in an array A:
A

We have our two variables to keep a tally v1 and v2, and our loop variable i. We will
initialize v1 and v2 to 0, and the first time through the loop we have i = 1, which we will
depict as pointing to the first spot in A. Thus at the beginning of the first loop the picture
looks like this:
A

2
i

v1

v2

A

2
i

v1

1
v2

A

2
i

v1

2
v2

34

i
v1

2
v2

A

2
i

v1

3
v2

## We check if A[ i ] = 1, and in this case it is not, so we increment v2.

A

2
i

v1

3
v2

Then we increment i and repeat two more times. At the end, our pseudocode will give us
something that looks like:
A

2
i

v1

5
v2

## Example 2: Sorting a list of numbers

This program sorts a list of n numbers using the selection sort method discussed in
lecture. Notice how comments make the program easier to understand.
1

BEGIN

// Input n and the list of numbers, which are stored in the array A

35

2
3
4
5
6
7
8
9
10
11

n AS INTEGER
A [ 1 : n ] AS INTEGER
// declaring the array A with n elements
INPUT n, A, , A[n]
FOR i 1 to n-1 STEP 1

// Search from position i to position n in the array; find the minimum value,
// and record its position in best.
best i
FOR j i + 1 TO n STEP 1
IF A [ j ] < A [ best ] THEN

15

best j
ENDIF
NEXT j
// Swap the minimum value ( A [ best ] ) with the i' th value

16

tmp A [ best ]

17

A [ best ] A [ i ]

18

A [ i ] tmp

12
13
14

19

20
20
21

NEXT i
PRINT A, , A[n]
END

## Suggestions for writing your own pseudocode

Unfortunately there is no one way to convert an idea of an algorithm into a pseudocode.
(Think about it, this would in essence be an algorithm for writing algorithms!) But to get
you pointed in the right direction, here are several general guidelines that will help you in
Lets think again about Example 1: the vote counting machine. Remember our goal:
count all the votes and then print out the number of votes for each candidate. Lets think
Points to consider when thinking about the algorithm:
1. Imagine giving your program to a 7-year old who can understand English and do
elementary arithmetic but doesnt have much common sense or experience. He or
she should be able to understand exactly what to do given your pseudocode.

36

2. Your program should work for arbitrarily long input, in this case arbitrarily many
votes. Thus, although saying Just count the votes might make sense for 10
votes, if you are given 10,000,000,000 votes then its not as obvious what Just
3. Remember that the instructions are executed step by step. Whoever is running
your program is not allowed to look at the program as a whole to guess what
you actually meant it to do.
With these points in mind, lets think about how to count votes. Say we are given the
votes in a big pile. One way to count would be:
Idea A:
Take the first vote, see who its for. If its for candidate 1 then mark a tally for 1, or if
its for 2 then mark a tally for 2. Then keep repeating this for the rest of the votes until
you go through the entire pile.
This is the idea (or the algorithm). Now we need to turn it into pseudocode.
Points to consider when you are trying to turn an idea into pseudocode:
1. What kind of information is recorded in the process of doing the task? This
information will have to be stored in variables when you write the pseudocode.
2. Where do you make decisions about selecting one of two actions to do? These
will usually become conditional statements in the pseudocode.
3. Where do you repeat things? These will become loops in the pseudocode.
OK now lets look at Idea A and try to translate it into pseudocode.
1. What are we keeping track of? The tallies of votes for each candidate. Thus,
these two tallies will become variables in the pseudocode.
2. Where do we make a decision between two actions? When we decide which
candidates tally we should add to. This will become a conditional statement.
3. Where do we repeat? When we are done with one vote we move on to the next
and repeat the same procedure. Going through the pile of votes is like looping
through an array, where each element tells us someones vote.
Now if you go back and look at the pseudocode for Example 1, youll see exactly how
the idea was transformed into pseudocode. Also, remember that theres more than one
way to write pseudocode for the same algorithm, just like theres more than one way to
express the same idea in English.

37

## How fast does your algorithm run?

The central measure of goodness of an algorithm (assuming it does its job correctly!) is
how fast it runs. We want a machine-independent measure and this necessarily implies
we have to sacrifice some precision. In general, the relative speeds of arithmetic
operations (+, * etc.) differ among machines, but we will assume all of them take the
same amount of time. The three central points to remember when discussing running time
are:
1. Even though we call the speed of an algorithm its running time, we wont
actually measure it in seconds or minutes, but in the number of elementary
operations it takes to run. For this class, elementary operations are arithmetic
(addition, subtraction, multiplication, division), assigning a value to a variable,
and condition checks (either in an if statement or in a do statement).
2. The running time in general depends on the size of the input. For example, if we
are sorting an array of n elements, it is natural (and unavoidable) that it will take
longer to sort n = 10,000,000 elements than to sort n = 10 elements.
3. We will usually analyze worst-case running time. That is, how long will this
algorithm run given the worst possible input of size n? Whenever in doubt, we err
on side of overestimation rather than underestimation.
Lets analyze the running time in Example 1.
1
2
3
4

## // Set initial vote counts to zero

FOR i 1 to n STEP 1

5
6
7

## // See who the next vote is for

IF A[ i ] = 1 THEN

8
9
10

11
12

ELSE

13
14
15
16
17
18

## // Otherwise its for candidate 2, so increment his tally

ENDIF
NEXT i

38

It takes 2 steps to initialize the variables. Then we run the loop n times: each time, we
check 1 condition (i.e. who the vote is for) and possibly make 1 assignment (i.e.
incrementing the tally). Thus each time we go through the loop we execute at most 2
steps. Finally it takes 1 step at the end to print the results. Thus adding everything up the
algorithm runs in time 2n + 3.
Example 3: Largest number in an array
This pseudocode finds the largest number in an array:
BEGIN
a, b, largest AS INTEGER
INPUT a, b
nums [ a : b ] AS INTEGER
FOR i a TO b STEP 1
INPUT nums [ i ]
NEXT i
largest a
FOR i a + 1 TO b STEP 1
IF nums [ largest ] < nums [ i ] THEN largest i
NEXT i
PRINT nums [ largest ]
END
Example 4: Reversing the elements of an an array
This pseudocode reverses the elements of an array
BEGIN
a, b, c, d, tmp AS INTEGER
INPUT a, b
nums [ a : b ] AS INTEGER
FOR i a TO b STEP 1
INPUT nums [ i ]
NEXT i
c b DIV 2
// DIV operation produces the quotient
FOR i a TO c STEP 1
d bi+1
tmp nums [ i ]
nums [ i ] nums [ d ]
nums [ d ] tmp
NEXT i
FOR i a TO b STEP 1
PRINT nums [ i ]
NEXT i
END
39