Understanding Algorithms and Pseudocode
Understanding Algorithms and Pseudocode
Advantages of algorithms
- not biased towards any programming language
- easy to convert to a program code or flowchart
- easy to determine logic errors
- has finite steps which lead to a solution
Disadvantages
- time consuming to design, i.e. first convert to flowchart, then to program code
- most people find them difficult to learn
a. Descriptions: These are general statements that are followed in order to complete a specific task. They are not governed
by any programming language. An example is as follows:
Enter temperature in oC
Store the value in box C
Calculate the equivalent temperature in oF
Store the value in box F
Print the value of box C and F
End the program.
b. Pseudocodes:
- These are English-like statements, closer to programming language that indicate steps followed in performing a specific
task.
-They are however independent of any programming language.
- An example is as follows:
Enter centigrade temperature, C
If C = 0, then stop.
Set F to 32 + (9C/5)
Print C and F
End
VARIABLES
Definition: A variables is a memory location that can store a value that can change during program execution.
Naming variables: Each programming language has its own way of naming variables. However, the following conventions
are common:
- a variable should not be a reserved word. A reserved word is a word with a specific meaning / function in that
programming language, e.g. Print, else, are reserved words in BASIC
- Variables must start with an alphabetic character, not with digit.
- It is wise to name a variable using the data it stores, e.g. surname (to store surnames), DOB (to store a date of
birth), etc. Thus it must be meaningful to avoid confusion
- Must not be too long
- Must be one word
i. Simple sequence: This is whereby instructions are executed in the order they appear in a program without jumping any
one of them up to the end of the program. Statements are executed one after another in the order they are. It is simple and
avoids confusion. Example:
Enter first number, A
Enter second number, B
C=A+B
Print C
Stop
ii. Selection Structure:
This allows one to choose the route to follow in order to accomplish a specific task. Selection is written using the IF
....THEN...ELSE statement or the CASE statement.
IF...THEN ...ELSE statement: A programming structure that allows the user to choose one from at least two routes of
solving a problem. The following Pseudocodes compares two numbers entered through the keyboard and determines the
bigger one.
Enter first Number, A Enter first Number, A Enter first Number, A
Enter second number, B Enter second number, B
Enter second number, B
IF A>B THEN IF A > B THEN
Print A is bigger Print A is bigger IF A>B THEN Print A is bigger
ELSE ENDIF
IF A<B THEN Print B is bigger
IF A<B THEN IF A < B THEN
Print B is bigger Print B is bigger IF A=B THEN Print Numbers are equal
ELSE ENDIF
END
Print Numbers are equal IF A = B THEN
ENDIF Print Numbers are equal
ENDIF ENDIF
END END
The above 3 Pseudocodes produces the same result.
CASE Statement: This is an alternative to the IF...THEN...ELSE statement and is shorter. For example:
Enter first Number, A
Enter second number, B
Enter operand (+, -, * /)
CASE operand of:
“+”: C = A + B
“-”: C = A-B
“*”: C = A*B
“/”: C = A/B
ENDCASE
Print C
END
Iii. Repetition/Iteration/Looping:
A control structure that repeatedly executes part of a program or the whole program until a certain condition is satisfied.
Iteration is in the following forms: FOR...NEXT loop, REPEAT... UNTIL Loop and the WHILE...ENDWHILE Loop.
a. For...Next Loop: A looping structure that repeatedly executes the loop body for a specified number of times. The syntax
of the For...Next loop is as follows:
A group of statements between the looping structures is called the loop body and is the one that is repeatedly executed.
The For...Next loop is appropriate when the number of repetitions is known well in advance, e.g. five times. An example of
a program that uses the For...Next loop is as follows:
Sum, Average = 0
FOR I = 1 to 5 DO
Enter Number
Sum = Sum + number
NEXT I
Average = Sum/5
Display Sum, Average
End
b. Repeat...Until Structure: This is a looping structure that repeatedly executes the loop body when the condition set is
FALSE until it becomes TRUE. The number of repetitions may not be known in advance and the loop body is executed at
least once. The syntax is as follows:
Repeat
Statement 1
Statement 2 loop body
................
Until {Condition}
For example
Sum, Average, Count = 0
Repeat
Enter Number (999 to end)
Sum = Sum + Number
Count = count + 1
Until Number = 999
Average = Sum / count
Print Sum, count, Average
End
In the above program:
- Count records the number of times the loop body executes.
- 999 is used to stop further data entry through the keyboard and thereby ending the loop. Such a value that stops
further data entry through the keyboard thereby terminating a loop is called a Rogue value or sentinel.
- The condition here is {Number = 999}. The loop exits when the number 999 is entered. If 999 is part of the
number to be entered in this program, then the user has to split it into two numbers, that is 999 = 990 + 9, therefore
can be entered separately as 990 and 9.
- A flag is also used to control the loop. In this case 999 is also a flag.
NB. As for the Repeat...Until loop, the condition is tested after the loop body has been run at least once, even when the
condition is true from start. This is rather misleading.
c. While ... Do Statement: A looping structure in which the loop body is repeatedly executed when the condition set is
TRUE until it becomes FALSE. It is used when the number of repetitions is not known in advance. The condition set is tested
first before execution of the loop body. Therefore the loop body may not be executed at all if the condition set is FALSE from
start. The syntax of the WHILE…END WHILE structure is as follows:
WHILE {condition}
Statement 1
Statement 2 loop body
................
ENDWHILE
The word WEND can be used to replace the word ENDWHILE in some structures and therefore is acceptable. The word Do,
after the condition is optional.
c. Flowcharts
It is a diagram used to give details on how programs and procedures are executed. Flowcharts are drawn using specific
symbols, each with its own meaning, as given below:
Symbol Explanation
Process Symbol - Indicates where some form of processing occur
Decision Represents a selection stage. Often used where a condition is, especially
in repetition and selection structures.
3. Using Iteration
(a) Repeat ... Until Structure
Flowchart (a) above indicates modules named Accept Numbers, Add numbers Multiply Numbers and Display Results.
Flowcharts for individual modules can then be designed as given in diagram (b) above, only the first module is indicated.
Can you do the rest?
d. Structure Diagrams: These are diagrams that show relationships between different modules as given below.
The structure diagram above indicates five sub-programs of the program Process Numbers, namely Initialise, Accept
Numbers, Process Numbers, Display Results and Exit. The module Process Numbers has its own sub-programs, which are
Add Numbers and Multiply Numbers. Modules are appropriate for very large programs. Can you write pseudocode for
individual modules? The program can be written as a continuous single program as indicated on the right side of the
diagram.
DATA STRUCTURES
-it is a collection of different data items that are stored together as a single unit and the operstion allowable on
them.
-such data structures includes arrays, trees ,linked lists,stacks and queues.
• Complex and more difficult to program as software needs to keep track of its size and data item
locations at all times
• Can be slow to implement searches
• A linked list only allows serial access
Arrays are declared by stating the following parameters: array name, array size, dimensions and the
data type. The size of the array is the maximum number of elements that it can store. For example, the
following declares an array that reserves five locations in memory and labels these as ‘Names’: Dim
Names(4) As String
The last option modifies the starting value and the ending value of the indices, but the number of
elements still remains the same.
By default, this implies that the array stores at most 5 elements. On running, the computer creates 5
contiguous memory locations under the name “Names”. Thus Names will contain 5 partitions. Each
memory partition will be accessed using an index, which is the address of the memory space in the
array.
In Visual Basic, the number of elements stored in an array is N + 1, N being the number (subscript)
used when declaring the array.
Array index starts at Zero up to N, N being the number in the declaration of the array.
The index is also called Subscript, and therefore arrays are subscripted data types.
It is not possible to exceed the upper limit or go below the lower limit on the array index values. You will
receive a “Subscript out of range” error message if you try to do so.
In the above declaration, the array index starts from 0 to 4 and are integer values. The memory
locations will be as follows:
The five individual locations are Names (0), Names (1), Names (2), Names (3) and Names (4). Each data
item is called an element of the array. To reference a particular element one must use the appropriate
index.
NB: However, most programming languages differ with Microsoft Visual basic in handling arrays,
especially on the amount of memory allocated. For example, using Java, the following declaration:
Int [4 ]Names;
This array declaration creates exactly 4 memory spaces for the array Names. The indices of the array
range from 0 to 3 which are
Names(3) = “Kapondeni”
Arrays simplify the processing of similar data. An algorithm for getting four names from the user and
storing them in the array Names is shown below:
One-dimensional arrays
A one-dimensional array is a data structure in which the array is declared using a single index and can be
visually represented as a list.
The following diagram shows the visual representation of the array Names(4):
0 Theresa
Lameck
1 Johanne
2 Laurence
Fadzai
3
Two-dimensional arrays
A two-dimensional array is a data structure in which the array is declared using two indices and can be
visually represented as a table.
Indices 0 1 2 3
0 Makombe Tinashe M 4A
Vheremu Alex M 4B
1
Mununi Mary F 3C
2 Chirongera Salpicio M 2C
Mutero Violet F 4C
3
The diagram above shows the visual representation of a 2 dimensional array Names(4,3)- 5 rows and 4
columns:
Initialising an array
Initialising an array is a procedure in which every value in the array is set with starting values – this
starting value would typically be “” for a string array, or 0 for a numeric array.
Initialisation is important to ensure that the array does not contain results from a previous use,
elsewhere in the program.
The following pseudo-co de can be used to search an array to see if an item X exists:
The above algorithm will shift elements of the array up (or left), removing the first element and then
completely removing the last element in the array.
The algorithm first searches the element to delete, and then remove it from the array.
NB:
• If the item is string, it replaces with empty spaces. However, if it is numeric, it replaces with a 0.
• Deleting form an array is often difficult as elements need to be shifted positions after deletion. It
is an effective method for deleting elements.
Inside the module, one then needs to redefine the array as follows:
ReDim Names(5) As
String
This allows the user to create the array when he/she actually needs it, using a ReDim statement:
Dynamic arrays can be re-created at will, each time with a different number of items. When you
recreate a dynamic array, its contents are reset to 0 (or to an empty string) and you lose the data it
contains. If you want to resize an array without losing its contents, use the ReDim Preserve command:
Binary Trees
A binary tree is a data structure, consisting of a root node and zero, one or two sub-tree which are
organised in a hierarchical way. Each node is a parent of at most two nodes.
For example, given the following numbers: 20, 30, 5, 2, 7, 6, 17, 58, 41 Placing
them in the binary tree is as follows:
A. Pre-Order traversal
The order of traversal is:
Procedure traversefrom(p)
Print (data);
If Tree[p].Left<>0 Then
Traversefrom(Left)
EndIf
If Tree(p).Right <> 0 Then
traversefrom(Right)
endif
endProcedure
B. In-Order Traversal
The order of traversal is:
NB: In-order traversal prints items in ascending order or in alphabetical order if they are alphabetic
items.
1. For the current node, check if there is left-sb-tree. If it exists, go to the root node of this sub-
Procedure traversefrom(p) If
Tree[p].Left<>0 Then
Traversefrom(Left)
EndIf
Print (data);
If Tree(p).Right <> 0 Then
traversefrom(Right)
endif
endProcedure
Procedure traversefrom(p) If
Tree[p].Left<>0 Then
Traversefrom(Left)
EndIf
If Tree(p).Right <> 0 Then
traversefrom(Right)
endif
Print (data);
endProcedure
1. The structure could be left the same, but the value of that node set to deleted.
2. The tree could be traversed, the values removed, put into a stack and then put back into a binary
tree.
• The value at the node is not only data, it is part of the structure of the tree
• If the node is simply deleted then the sub-tree leading from it is not navigable
OR:
• The entire sub-tree without its root is read to a list
• The sub-tree is deleted
• The values in the list are read back into the tree (element which was originally on the left will
replace the deleted element (becomes root of that branch))
Binary tree maintain the order of elements. However, if one element is deleted, the order is affected. In
some cases, each node (data) may be assigned pointers (right and left pointer). This is as illustrated
below:
- Left pointer
- Right pointer
- Data item
Let’s take the binary tree below:
B2 BINARIES