Sie sind auf Seite 1von 47


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
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


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.

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
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
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.

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.




Indicates any type of internal operation

inside the Processor or Memory


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

Indicates that the computer is to obtain data
or output results


Used to ask a question that can

be answered in a binary format (Yes/No,


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.


Indicates the starting or ending of the

program, process, or interrupt program.

Flow Lines

Shows direction of flow.

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
Sum 0
INPUT the first testscore
Add first testscore to sum
INPUT the second testscore
Add to sum
INPUT the third testscore
Add to sum
INPUT the Forth testscore
Add to sum
INPUT the fifth testscore
Add to sum
INPUT the sixth testscore
Add to sum
PRINT the sum
16. END
b) The corresponding flowchart is as follows:
Sum 0
Get first testscore
Add First testscore
To sum
Get second testscore
Add second testscore
To sum

Page no: 6

Get third testscore

Add third testscore

to sum
Get Forth testscore

Add forth testscore

to sum
Get Fifth testscore
Add fifth testscore to sum

Get Six testscore

Add sixth testscore to
Output Sum
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:

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

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.
Sum 0
INPUT a value

Page no: 8

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

Sum 0

Get a value

= -1


Sum Sum + Value

Output Sum


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:
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

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
= 0.123 * 102
123000 = 0.123 * 106
0.000123 =0.123 * 10-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.
A single character, could be a digit, letter of the alphabet or symbol, for example:
"a", "@", "$", "3", "x" etc.
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 data items are used as status indicators and may contain only one of two possible
values: True or False.
There are two basic methods of using data items in a program:
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
Add 1 to
If = 13
Page no: 10


Variables and Variable names

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.
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.

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.
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
INPUT the sum
Average sum / 6
PRINT the average
The corresponding flowchart will appear as follows:

Page no: 12

Get the sum

Average sum / 6
Output sum

Example 3: This is the pseudo-code required to input three numbers from the keyboard
and output the result.
sum, number1, number2, number3 AS INTEGER
INPUT number1, number2, number3
sum number1 + number2 + number3
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.
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
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:






In pseudocode form we get

IF condition is true THEN
do task A
do Task-B
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




From the above structure, we have the following

Page no: 14

IF condition is true THEN

do Task-A
IF condition is true THEN do Task-A
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
IF a = b THEN PRINT "a = b"
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
ans, number1, number2, AS INTEGER
PRINT choose one of the following
PRINT m for multiply
PRINT a for add
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
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
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
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
pay-rate is normal
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
p-rate normal

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
Tax 100000 * 0.30
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
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.
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
PRINT entry invalid
Page no: 17

This can be expressed in a case statement as follows:

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
Instead of using the word otherwise, one can use else.
Repetition or Iteration Structure
A third structure causes the certain steps to be repeated.




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
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.
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.
PRINT Type in the letter chosen or Q to finish
PRINT A: Athletics
PRINT S: Swimming
PRINT F: Football
PRINT B: Badminton
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
badminton badminton + 1
UNTIL letter = "Q"
PRINT Athletics scored, athletics, votes
PRINT Swimming scored, swimming, votes
PRINT Football scored, football, votes
PRINT Badminton scored, badminton, votes
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
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
Example 12: Write a program that will output the square root of any number input until
the number input is zero.
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
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
NEXT variable
Example 13.
FOR n 1 TO 4 STEP 1
PRINT Loop, 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
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:
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
average sum / count
PRINT The sum of the numbers is , sum
PRINT Average of the numbers is , average
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.
Design an algorithm and the corresponding flowchart for finding the sum of N numbers.
Pseudocode Program
Sum 0
FOR i 1 TO n STEP 5
INPUT value
sum sum + value
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

The corresponding flowchart will be as follows:

sum 0

Input a number




Output sum

sum sum + number


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
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


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.


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).




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
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
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.






Cooking utensils


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.


Chapter 3

Algorithmic Problem Solving

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
area and radius instead of A and r in general, for the sake of readability.)
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.


Characteristics Of An Algorithm 35

3. An algorithm must be effective.

Again, this goes without saying.
correct answer to the problem.

An algorithm must provide the

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
At the same time, you compare num with min, if num is smaller than min,
let min be num instead.
Similarly, you compare num with max, if num is larger than max, let max
be num instead.
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.


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.

Terminator box
Process box

sum count 0
min ?
max ?


Decision box

end of
increment count
sum sum + num

ave sum/count

min num


max num





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.


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
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.



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.


Chapter 3

Algorithmic Problem Solving

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.

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.


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


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.


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.


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


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
How to read the symbols
<> : 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



Whole numbers, negative or positive. (123, -123, 0, 23434, -8 etc.)


Floating point numbers, negative or positive. (-143, 0, 45.67, -12.8987 etc.)


Logical. Only TRUE or FALSE.


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


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 in any format. (12/12/2009, 01-03-2007 etc.)

2. Declaring variables

<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

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

( total gets the result of price * quantity )

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

Syntax: INPUT <variable>

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>


"This is a message"
"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 )


( 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
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.
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.
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




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.
IF a < 10 THEN

PRINT "a is less than 10"

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


IF cookies = TRUE THEN
PRINT "Tea and cookies will be served"
PRINT "Only tea will be served"
PRINT "No tea can be served"

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

Syntax: IF <condition> THEN



ELSEIF <condition> THEN

ELSEIF <condition> THEN





ELSEIF <condition> THEN






PRINT "Tea will be served"
PRINT "Coffee will be served"
PRINT "Tea and coffee are not available"

Page no: 25

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


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

NEXT <variable>

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

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

FOR i 1 TO 50 STEP 2

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.

p 1
FOR i 10 TO p STEP -1

b) Conditional loop: WHILE ... ENDWHILE


WHILE <condition> DO


a 0
WHILE a < 10 DO
a a+1

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



UNTIL <condition>

a 1
UNTIL a > 10


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

PROCEDURE <procedure name> (parameter list)

PROCEDURE <procedure name>



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

To call a procedure:

<procedure name>






<procedure name> (parameter list)

Sum (a, 10)


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

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


FUNCTION <function name> AS <data type>


RETURN x + y
IF hours > 1 AND hours < 12 THEN
ELSEIF hours > 12 AND hours < 24 THEN

To call a function:

<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

Page no: 28

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"

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 =





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

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

A[3] 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


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:

Sets the value stored in variable x to the value 5


Sets y to the value stored in x; leaves x unchanged


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.


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:

true/false condition THEN

First list of instructions

Second list of instructions

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
count count + 1

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 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


List of instructions

Another flavor of loop makes the computer perform some instructions repeatedly as
long as a specified condition remains FALSE:
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

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:

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


WHILE current < max

current current + 1
Other instructions

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 The lowest price was:

Display a fixed message

PRINT minimum

Display the value of the variable minimum

Comments are not actually instructions; instead, they provide hints about what the
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

Remember, comments dont change the meaning of your program, since computers
skip over them entirely. They do help make the meaning clearer to human readers.



Pseudocode looks deceptively like English, and that is its advantage: it should be
understandable by your average lay-person. However, please be aware of the
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
attempt to write your own.
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.
Votesfor candidate 1 or candidate 2are read one by one. Lets assume that the votes
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.


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"
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
// Otherwise its for candidate 2, so increment his tally
v2 v2 + 1
PRINT Totals:, v1, v2


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:

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:




Now we look to see if A[ i ] = 1. In this case it is indeed, so we increment v1.





Now we loop around and increment i, which gives us the picture





We check whether A[ i ] = 1, which it is, so we increment v1 again.




Then we increment i again to get





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





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




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.


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



A [ 1 : n ] AS INTEGER
// declaring the array A with n elements
INPUT n, A[1], , 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


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


tmp A [ best ]


A [ best ] A [ i ]


A [ i ] tmp




PRINT A[1], , A[n]

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
writing your own pseudocode.
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
about how to write the pseudocode for this task.
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.


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
count the votes means.
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.


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
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.

// Set initial vote counts to zero

votes_for_candidate_1 0
votes_for_candidate_2 0
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

votes_for_candidate_1 votes_for_candidate_1 + 1




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

votes_for_candidate_2 votes_for_candidate_2 + 1
PRINT Totals:, votes_for_candidate_1, votes_for_candidate_2


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:
a, b, largest AS INTEGER
INPUT a, b
nums [ a : b ] AS INTEGER
FOR i a TO b STEP 1
INPUT nums [ i ]
largest a
FOR i a + 1 TO b STEP 1
IF nums [ largest ] < nums [ i ] THEN largest i
PRINT nums [ largest ]
Example 4: Reversing the elements of an an array
This pseudocode reverses the elements of an array
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 ]
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
FOR i a TO b STEP 1
PRINT nums [ i ]