Algorithms
Algorithms
Algorithms
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
input/output
Decision
Connector
Predefined Process
Terminal
Flow Lines
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.
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.
a) Algorithm
1. BEGIN
2.
Sum 0
3.
INPUT the first testscore
4.
Add first testscore to sum
5.
INPUT the second testscore
6.
Add to sum
7.
INPUT the third testscore
8.
Add to sum
9.
INPUT the Forth testscore
10.
Add to sum
11.
INPUT the fifth testscore
12.
Add to sum
13.
INPUT the sixth testscore
14.
Add to sum
15.
PRINT the sum
16. END
b) The corresponding flowchart is as follows:
Start
Sum 0
Get first testscore
Add First testscore
To sum
Get second testscore
Add second testscore
To sum
1
Page no: 6
1
Get third testscore
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
Add 1 to
If = 13
Print PLEASE INPUT TODAYS DATE
Page no: 10
b)
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
Task-A
Task-B
Condition
False
True
Task-A
Page no: 14
Page no: 15
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
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
Condition
False
True
Task
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 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
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
Input a number
i>n
True
False
Output sum
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
32
Chapter 3
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
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
34
Chapter 3
3.3
Characteristics Of An Algorithm 35
36
Chapter 3
Algorithm version 2:
sum count 0
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
3.5
Control Structures 37
3.6
Summary
38
Chapter 3
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.
2.
How would you sort 10 numbers in increasing order? Do you know any
of the basic sorting techniques?
3.
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.
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
DATA TYPE
PURPOSE
INTEGER
REAL
BOOLEAN
CHARACTER
Any single character. Can't be used in arithmetic expressions. ("A", "1", "%"
"G", "-", "?", "5", "*", etc.)
STRING
DATE
2. Declaring variables
Syntax:
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
Input i
Input name
Input part_no, price, quantity
( Three values will be asked from the user and put into
these variables respectively )
PRINT
PRINT
PRINT
PRINT
i
"This is a message"
i+p
"The result is", i / 2
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
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
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 )
IF <condition> THEN
<statements>
<statements>
or
<statements>
...
...
ELSE
<statements>
<statements>
ENDIF
Ex:
ENDIF
Page no: 25
6. Iteration (Loops)
a) Counter loop: FOR ... NEXT
Syntax:
Ex:
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
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
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:
Ex:
To call a procedure:
<procedure name>
OR
Ex:
Sum
OR
Ex:
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:
To call a function:
Ex:
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[2]
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]
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
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
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
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:
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.
Similarly, your program can present its results using instructions like these:
PRINT minimum
Comments
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
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.
Caveat
32
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.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
BEGIN
v1 0
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
2
i
v1
1
v2
2
i
v1
2
v2
34
i
v1
2
v2
2
i
v1
3
v2
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
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[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
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[1], , A[n]
END
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
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.
37
5
6
7
8
9
10
11
12
ELSE
13
14
15
16
17
18
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