Professional Documents
Culture Documents
1.7 Pseudocode
Pseudocode refers to English like statements which are used to outline an algorithm.
Pseudocode does not have syntax but it must be written clearly and concisely so
that it is easy to read and understand
2. Programming Concepts
2.1 Storage of values
When a programmer wants to write a program, he must first decide how he is
going to store values. The 3 ways of storage of values for a program are:
VARIABLE, CONSTANT and ARRAY
2.1.1 Variable
A variable is a storage space in computer memory whose value can change as the
program is running. For example Count=Count+1
A variable has a variable name and a datatype. A variable name is also called an
IDENTIFIER.
2.1.2 Constant
A constant is a storage space in computer memory for values whose value does not
change as the program is running. Example Constant PI=3.142
2.1.3 Array
An array is a data structure for storage of values which has many spaces under one
name or identifier and datatype. Values stored in an array must be of the same
datatype
2.1.4 Datatype
A datatype specifies the type of data that can be stored in a variable, constant or an
array. Examples are:
Integer – for whole numbers such as 20, 15 or 50
Double or Real or Decimal – for storing numbers with decimal points such as 3.5,
0.23, 25.89
Boolean – true or false
Char – A single character such as a “b” or “z”
String - a string of many characters such as “uMalcom Sambo” or “ibizo lami ngu
Ryan Winterboer”
2.2.3 OUTPUT
After processing, the program gives an output. For example for a program that adds
2 numbers the Output is Sum. In Pseudocode the key word OUTPUT or PRINT are
used to input data into the program.
Example 1: (Pseudocode)
START
Sum Num1+Num2
END
Sub Main()
Dim Num1 As Integer
Dim Num2 As Integer
Dim Sum As Integer
Console.WriteLine("Please enter the first number")
Num1 = Console.ReadLine
Console.WriteLine("Please enter the second number")
Num2 = Console.ReadLine
Sum = Num1 + Num2
Console.WriteLine("The sum of the 2 numbers is" & Sum)
Console.ReadKey()
End Sub
End Module
Example 2
Example 2
Question: Write a program which subtracts 2 numbers
Module Module1
Sub Main()
Dim Num1 As Integer
Dim Num2 As Integer
Dim Difference As Integer
Console.WriteLine("Enter the first number")
Num1 = Console.ReadLine
Console.WriteLine("Please enter the second number")
Num2 = Console.ReadLine
Difference = Num1 - Num2
Console.WriteLine("The difference of the 2 numbers is " & Difference)
Console.ReadKey()
End Sub
Example 4: Write a program that asks the user to input their name and age and
output it
Module Module1
Sub Main()
Dim FirstName As String
Dim Surname As String
Dim Age As Integer
Console.writeline(“Enter the first Name”)
FirstName=Console.Readline
Console.Writeline(“Please enter the Surname”)
Surname=Console.readline
Console.Writeline(“Please enter the Age”)
Surname=Console.readline
Console.Writeline(“You said your first name is: “&FirstName)
Console.Writeline(“You said your Surname is: “&Surname)
Console.Writeline(“You said your Age is: “&Age)
Console.Readkey()
End Sub
End Module
5.0 Efficiency:
An algorithm‟s efficiency can be judged in terms of:
• Speed: How quick the algorithm produces the required output.
• Memory requirements: How many lines of code the algorithm contains.
Correctness:
Although an algorithm is expected to produce the correct outputs, correctness can still be
measured in terms of:
Accuracy:
How many decimal places produce output with greater accuracy (e.g. more decimal places)
Range:
Will the algorithm work with the complete range of inputs? Or can it only deal with positive
numbers, whole numbers, numbers below 1 million, etc.
Reliability:
Will the algorithm always produce correct output within the range that it is designed to work?
Or are there values which it will not accept (e.g. zero).
Appropriateness: Appropriateness can be measured in terms of:
Length: If the problem is simple then a short algorithm would normally be expected.
Speed: if the output needs to be generated quickly, then the algorithm must be able to
generate
output quick enough.
Memory requirements: An algorithm controlling a washing machine must not require a lot
of memory!
6.0 Programming Constructs (Control Structures)
Control structures are used to control the program for it to perform what the
programmer wants.
Think of a Car: A car has many controls available to make it do anything that the
driver wants it to do. For example, when the driver wants the car to increase speed,
he steps on the accelerator pedal, when he wants the car to stop he steps on the
brake pedal. When he wants the car to turn left he turns the steering wheel leftwards.
In a similar manner, programmers need to be able to control their programs for
example: To execute instructions in a certain order or sequence, to check a condition
and select the path that corresponds to whether the condition is TRUE or FALSE
When writing a program, we use 3 control structures:
Sequence
Selection
Iteration
1. SEQUENCE
Programming statements are executed one after another in the order in which they
are written. The order of the statements is important.
2. SELECTION
This is when a program needs to make a decision and select a path depending on a
given condition. Conditional statements are used for selection and these are:
1. IF…THEN…ELSE…ENDIF
2. CASE…OF…OTHERWISE…ENDCASE
Example 1: Question: Write a program that prompts the user to input the name
and computer science mark for a student and output the grade:
90-100 A*
80-89 A
70-79 B
60-69 C
50-59 D
30-49 E
0-29 U
//USING IF…THEN…ELSE…ENDIF
START
DECLARE StudentName : STRING
DECLARE CompSciMark : INTEGER
INPUT “Please enter your computer science mark”, CompSciMark
INPUT “Please enter your name”, StudentName
IF CompSciMark <=100 AND CompSciMark >=90 THEN
OUTPUT “A*”
ELSEIF CompSciMark <=89 AND CompSciMark >=80 THEN
OUTPUT “A”
ELSEIF CompSciMark <=79 AND CompSciMark >=70 THEN
OUTPUT “B”
ELSEIF CompSciMark <=69 AND CompsciMark >=60 THEN
OUTPUT “C”
ELSEIF CompSciMark <=59 AND CompSciMark >=50 THEN
OUTPUT “D”
ELSEIF CompSciMark <=49 AND CompSciMark >=40 THEN
OUTPUT “E”
ELSEIF CompSciMark <=29 AND CompSciMark >=0 THEN
OUTPUT “U”
ELSE
OUTPUT “You have entered an invalid mark”
ENDIF
END
//USING CASE…OF…OTHERWISE…ENDCASE
START
DECLARE StudentName : STRING
DECLARE CompSciMark : INTEGER
INPUT “Please enter your name”, StudentName
INPUT “Please enter your computer science mark”, CompSciMark
CASE CompSciMark OF:
90 TO 100 :
OUTPUT “A*”
80 TO 89 :
OUTPUT “A”
70 TO 79 :
OUTPUT “B”
60 TO 69 :
OUTPUT “C”
50 TO 59 :
OUTPUT “D”
40 TO 49 :
OUTPUT “E”
0 TO 29:
OUTPUT “U”
OTHERWISE
OUTPUT “You have entered an invalid mark”
ENDCASE
END
Write a program that prompts the user to input their name and age. The program
must output the following messages depending on the age of the person:
Greater than 140 – “You no longer exist”
80 to 140 – “you are too old to drive”
16 to 80 – “You are allowed to drive “
Great than 0 to 15 – “You are too young to drive. You are a baby”
Less than 1 – “Error. You have entered an invalid age”
3. ITERATION/LOOPING/REPETITION
It is a construct which is used when an action has to be repeated more than
once. When an action needs to be repeated more than once we use looping.
There are 3 types of loops:
1. FOR…TO…NEXT
2. REPEAT…UNTIL
3. WHILE…DO…ENDWHILE
FOR…TO…NEXT Loop
It is used when the number of iterations/repetitions is known. It is a count controlled
loop – meaning it can move the counting variable on its own, from the lower limit to
the upper limit. “It can count on its own”.
E.g: write a program that displays the name “Sam Rundle” 10 times.
START
DECLARE Count : INTEGER
Count0
FOR Count1 TO 10
OUTPUT “Sam Rundle”
NEXT Count
END
Write a program that prompts the user to input their name and age and outputs them
10 times
START
DECLARE StudentName : STRING
DECLARE StudentAge : INTEGER
DECLARE Count : INTEGER
Count0
FOR Count1 TO 10
OUTPUT “The student name is “,StudentName
OUTPUT “The student age is”, StudentAge
NEXT Count
END
Example 2: Write a program that prompts the user to input 10 numbers and
calculates and outputs the total.
START
DELCARE Number : INTEGER
DECLARE Total : INTEGER
DECLARE Count : INTEGER
Count0
Total0
FOR Count1 TO 10
INPUT “Enter a number: “, Number
TotalTotal+Number
NEXT Count
OUTPUT “The total is : “,Total
END
Example 3:
Write a program that prompts the user to input 10 numbers and stores them in
an ARRAY, and add the average them.
START
DECLARE Numbers[1:10] : INTEGER
DECLARE Sum, Count : INTEGER
DECLARE Average : REAL
Sum0
Average 0
Count0
FOR Count1 TO 10
INPUT “Enter a number”, Numbers (Count)
SumSum+Numbers(Count)
NEXT Count
FOR Count1 TO 10
OUTPUT “The numbers are:”, Numbers(Count)
NEXT Count
AverageSum/10
REPEAT…UNTIL
Repeats an action while the condition is FALSE until it becomes TRUE. It is a
conditional loop, meaning it *CHECKS A CONDITION*.
It tests the condition at the bottom (Bottom Tested loop/Post Condition loop) –
meaning, the program will enter the loop at least once before a condition is tested.
EXAMPLE 1:
Write a program that prompts the user to input a number and add it to the total until
the total is greater than 200. No need to store the numbers, just store the total.
START
DECLARE Number, Total : INTEGER
Total 0
REPEAT
INPUT “Please enter a number”, Number
TotalTotal+Number
UNTIL Total>200
OUTPUT “The total of the numbers is: “,Total
END
Example 2:
Write a program that prompts the user to input a PIN until they input a correct
PIN = 1234
START
CONSTANT Pin : STRING=”1234”
DECLARE PinGuess : STRING
REPEAT
INPUT “enter the pin: “, PinGuess
UNTIL PinGuess=Pin
OUTPUT “You have finally entered the correct pin”
END
Example 3:
Write a program that prompts the user to input a PIN until they input a correct
PIN = 1234. The program must allow a person 5 chances only
START
DECLARE Pin : STRING
DECLARE PinGuess : STRING
DECLARE Count : INTEGER
Count0
Pin”1234”
REPEAT
INPUT “ enter the pin: “, PinGuess
CountCount+1
UNTIL PinGuess=Pin OR Count=5
IF PinGuess=Pin THEN
OUTPUT “You have finally entered the correct pin”
ELSE
OUTPUT “You exhausted your 5 chances”
END IF
END
WHILE…DO…ENDWHILE
Example 1: Write a program that prompts the user to input a number and adds it to
the total until the total is greater than 200. No need to store the numbers, just store
the total.
START
DECLARE Number, Total : INTEGER
Total 0
WHILE Total<=200
INPUT “Please enter a number”, Number
TotalTotal+Number
ENDWHILE
OUTPUT “The total of the numbers is: “,Total
END
Array
An array is a data structure for storage of values which has many spaces and can
store multiple values under the same Identifier and datatype.
Example 1: Write a program that prompts the user to input 10 student names and
stores them in an array and then displays them.
START
DECLARE StudentNames[1:10] : STRING
DECLARE Count : INTEGER
Count0
FOR Count1 TO 10
INPUT “Enter a name of the student”,StudentName(Count)
NEXT Count
FOR Count1 TO 10
OUTPUT “The name of the student is : “ , StudentName(Count)
NEXT Count
END
Mr A Ncube , the geography teacher wants to grade and split his class into two sets
depending on the percentage marks. If a student scores 70 and above they are in
set 1 if they score less than 70 than they are put in set 2. The number of students
is 100. Show how many are in set 1 and how many are in set 2
START
DECLARE Set1Count, Set2Count, Index : INTEGER
DECLARE StnGeoMark : REAL
Index0
Set1Count0
Set2Count0
Write a program that prompts the user to enter 20 numbers that are between -90 and
90.The program must calculate and output the sum and average of the positive and
negative numbers
START
DECLARE Number, NegativeNumCount, PositiveNumCount, NegativeSum,
PositiveSum, Index : INTEGER
DECLARE NegativeAverage, PositiveAverage : REAL
NegativeNumCount0
PositiveNumCount0
Index0
NegativeSum0
PositiveSum0
FOR Index1 TO 20
REPEAT
INPUT “Please Enter a Number”,Number
IF Number<0 AND Number>=-90 THEN
NegativeNumCount+=1
NegativeSum=NegativeSum+Number
ELSEIF
Number>=0 AND Number =<90 THEN
PositiveNumCount+=1
PositiveSum=PositiveSum+Number
ELSE
OUTPUT “Invalid Number re-enter”
ENDIF
UNTIL Number>=-90 AND Number=<90
NEXT Index
NegativeAverage=NegativeSum/NegativeNumCount
PositiveAverage=PositiveSum/PositiveNumCount
OUTPUT “The Negative Sum is”,NegativeSum
OUTPUT “The Positive Sum is”,PositiveSum
OUTPUT “The Negative Average is”,NegativeAverage
OUTPUT”The Positive Average is “,PositiveAverage
END