Paper 2 Fundamental Problem-solving and Programming Skills Unit 12 Algorithm Design and Problem Solving
An Algorithm sequence of steps that can be carried out to perform a task.
Consider the following as an example:-
1 Take a spanner and loosen the wheel nuts.
2 Position a jack in an appropriate place.
3 Raise the car.
4 Take off the wheel nuts and the wheel.
5 Lift replacement w heel into position .
6 Replace wheel nuts and tighten by hand.
7 Lower the car.
8 Fully tighten wheel nuts.
These directions may seem pretty straightforward but not followed correctly in sequence may result in the process becoming very
difficult maybe even impossible.
In computer Science all Algorithms make use of the following basic constructs when writing algorithms:-
- Assignment
- A value is given a name (identifier) or the value is changed.
- Sequence
- The steps are performed on after the other.
- Selection
- Some steps are performed only under certain conditions. Otherwise different (or no steps are performed).
- Repetition
- Some steps are performed a number of times. A.K.A iteration or looping.
- Input, Processing and Output
- Programs involve data and that is why we need input and output statements.
Algorithms can be expressed in the following ways:-
- Structured English
Subset of English language that consists of commands to describe an algorithm.
- Pseudocode
Makes use of keywords and identifiers without following any particular syntax of a programming language.
- Flowchart
Makes of shapes that are linked together to represent the sequence in an algorithm.
Hassan Zulfiqar Haider For more study material please visit:-
A-Levels/IGCSE/O-Levels Computer Science https://sites.google.com/site/csvault042/home
03234140902
hassanzulfiqarhaider@gmail.com
Paper 2 Fundamental Problem-solving and Programming Skills Unit 12 Algorithm Design and Problem Solving
Hassan Zulfiqar Haider For more study material please visit:-
A-Levels/IGCSE/O-Levels Computer Science https://sites.google.com/site/csvault042/home
03234140902
hassanzulfiqarhaider@gmail.com
Paper 2 Fundamental Problem-solving and Programming Skills Unit 12 Algorithm Design and Problem Solving
Variables:-
- It is the space allocated in the main memory to hold some data.
- And of course, it’s contents are suppose to change during execution.
- Unlike constants.
- The size of variable (bytes) depends on its type.
- A variable is referred to by its name (identifier).
Operation Pseudocode Description
Assigning a Value INPUT number Number is assigned with 5 (assuming that is what the user put in) Local Variables:-
Guesses ß 1 The value of 1 is assigned to (ß) to Guesses Variables that are used within a single module or subroutine (to
be discussed later).
Value Updating Guesses ß Guesses + 1 Guesses will be added to 1 and then stored in Guesses overwriting the Global Variable:-
INPUT num old value with the new one. Variables that are used by all modules or subroutines.
Total<—Total + num
Copying a Value Total ß Sum The contents of Sum will be copied to Total.
Swapping Two We will need a The value of A is temporarily stored Temp. (say A was 5 now 5 is Temp)
Values temporary variable…say The value of B (say 6) is now copied to A (overwriting 5).
Temp The Value from Temp is then copied to B (5 overwrites 6).
Temp ß A Therefore the value of A(was 5 now is 6) and B(was 6 now is 5) are
AßB swapped.
BßTemp
a,b = b,a
Correlating Flowcharts and Pseudocodes:-
Consider a program that takes miles as an input and returns the equivalent kilometres.
Hassan Zulfiqar Haider For more study material please visit:-
A-Levels/IGCSE/O-Levels Computer Science https://sites.google.com/site/csvault042/home
03234140902
hassanzulfiqarhaider@gmail.com
Paper 2 Fundamental Problem-solving and Programming Skills Unit 12 Algorithm Design and Problem Solving
Consider the following psuedocode where a element is input from the user, compared from elements in the array. If the element is found the the index position of where the element is
present is returned otherwise a “not found” message is returned. The psuedocode can be represented flowchart as well…(notice it has a pre-condition loop)
Hassan Zulfiqar Haider For more study material please visit:-
A-Levels/IGCSE/O-Levels Computer Science https://sites.google.com/site/csvault042/home
03234140902
hassanzulfiqarhaider@gmail.com
Paper 2 Fundamental Problem-solving and Programming Skills Unit 12 Algorithm Design and Problem Solving
Also consider the following example of a post condition loop represented using both pseudo code and flowchart
Hassan Zulfiqar Haider For more study material please visit:-
A-Levels/IGCSE/O-Levels Computer Science https://sites.google.com/site/csvault042/home
03234140902
hassanzulfiqarhaider@gmail.com
Paper 2 Fundamental Problem-solving and Programming Skills Unit 12 Algorithm Design and Problem Solving
Also Consider the following Psuedocode to Flowchart Translation of a FOR…LOOP
PROCEDURE CheckWeight
count <— 0
index <— 1
FOR INDEX 1 TO 100
IF BarWeight[index] > MaxIndex
THEN count <— count+1
END IF
NEXT
END FOR
IF MaxIndex>Threshold
THEN CALL ServiceCheck
ELSE
PRINT “ShippingBox OK - Maximum Weight Exceeded 3 TImes.”
END IF
Hassan Zulfiqar Haider For more study material please visit:-
A-Levels/IGCSE/O-Levels Computer Science https://sites.google.com/site/csvault042/home
03234140902
hassanzulfiqarhaider@gmail.com
Paper 2 Fundamental Problem-solving and Programming Skills Unit 12 Algorithm Design and Problem Solving
CASE...OF...END CASE in Flowchart
INPUT PlayerName
CALL ReadPlayerTotal()
INPUT PlayerGameGrade
CASE OF PlayerGameGrade
‘B’ : PointsTotalßPointsTotal+1
‘C’ : PointsTotalßPointsTotal+3
‘D’ : PointsTotalßPointsTotal+5
END CASE
OUTPUT PointsTotal
CALL SavePlayerTotal
IF PointsTotal>11
THEN
OUTPUT “ELIMINATED”
Hassan Zulfiqar Haider For more study material please visit:-
A-Levels/IGCSE/O-Levels Computer Science https://sites.google.com/site/csvault042/home
03234140902
hassanzulfiqarhaider@gmail.com
Paper 2 Fundamental Problem-solving and Programming Skills Unit 12 Algorithm Design and Problem Solving
Logic Statements:-
You should understand the difference between Simple Logic, Complex Logic and Nested IF Statements.
PRINT “enter a positive number”
*This is a simple condition
INPUT number
In which case it is only determining is the number is positive.
If number > 0 *
**This is a nested if statement as it is part of another if
if number > 10 **
statement (i.e. number>0).
then
***This is a complex condition as it is taking in account of
print “this number is greater than 10”
two conditions.
else if number>=5 and number<=10 ***
(number>=5 and number<=10) and since there is an “and”
then
print “this number is between 5 and 10 (inclusive)” operator in between the two condition, both sub-statements
else have to be true for this statement to be true. This is also
print “this number is less then 5” makes use of an “else if” to test a second condition.
else
print “this number is not positive”
Case…OF…OTHERWISE…ENDCASE
input choice *
case of choice ** A variable “choice” will hold some value * which is to
1: print “Have fun” *** be investigated ** and only one statement is to be
2: print “good luck” executed based on the contents of choice. ***Of the
3: print “good night” input is among 1..4 then the corresponding statement
4: print “good bye” will execute. ****However, if anything else in the
Otherwise: “choice” then the default condition will execute.
Print “you have not selected a valid option”. ****
end case
Loops:-
We will be solving the following problem using different types of loops:-
“take 10 numbers as input and output the largest number”
FOR…TO…NEXT…ENDFOR REPEAT…UNTIL WHILE…DO…ENDWHILE
INPUT BiggestSoFar INPUT BiggestSoFar INPUT BiggestSoFar
FOR Counter ß 1 to 9 REPEAT WHILE Counter < 10
INPUT NextNumber INPUT NextNumber INPUT NextNumber
IF NextNumber>BiggestSoFar IF NextNumber>BiggestSoFar IF NextNumber>BiggestSoFar
THEN THEN THEN
BiggestSoFar ßNextNumber BiggestSoFar ßNextNumber BiggestSoFar ßNextNumber
END IF END IF END IF
NEXT Counter ß Counter + 1 Counter ß Counter + 1
END FOR UNTIL Counter = 10 END WHILE
PRINT BiggestSoFar PRINT BiggestSoFar PRINT BiggestSoFar
For Loop is an Unconditional Loop Repeat Until loop is a Post While Loop is a Pre Conditional Loop.
which means that is will run the Conditional Loop. There are no There are no defined number of steps
defined number of time no matter defined number of steps the loops and the loops terminates when the
what. i.e. 9 times (counter 1 to 9). terminates when the condition tested condition tested before each step
The statements between FOR and after each step becomes true. In this becomes false. i.e Counter<9. In which
ENDFOR are to be repeated 9 times. case that condition is Counter = 9. In case if we start counter at 0 this
which case if we start counter at 0 this program will run 10 times before
program will run 10 times before exiting loop.
exiting loop.
Hassan Zulfiqar Haider For more study material please visit:-
A-Levels/IGCSE/O-Levels Computer Science https://sites.google.com/site/csvault042/home
03234140902
hassanzulfiqarhaider@gmail.com
Paper 2 Fundamental Problem-solving and Programming Skills Unit 12 Algorithm Design and Problem Solving
FOR LOOP can also be used in reverse order for example
FOR count 9 DOWNTO 0 FOR count 9 to 0 STEP -1
PRINT count
NEXT
END FOR
The above algo will print values from 9 to 0 in reverse order.
A rogue value is a value used to terminate a sequence of values. The rogue value is of the same data type but outside the range of
normal expected values.
This algorithm works even if the sequence consists of only one
non-zero input. However, it will not work if the only input is 0.
If the rogue value is entered the
Repeat Until statement will still
execute its statements whereas the
While Loop will not as it tests its
condition before entering the
loops statements.
Step Wise Refinement refers to the process of splitting a problem into smaller sub problems. These sub problems are then divided
into even smaller sub problems until each problem represents just one element of the final program.
Sub problems can be referred to as “Sub Routines”.
Subroutine:-
A subroutine is a self-contained section of the program code that performs a specific task as part of the main program.
- Procedure:-
- A procedure is a subroutine that performs a specific task without returning a value to the part of the program
from which it was called.
- Function:-
- A function is a subroutine that performs a specific task and returns a value to the part of the program from which
it was called.
PROCEDURE SetValues
INPUT Symbol FUNCTION AdjustNumberOfSpaces RETURNS INTEGER
MaxNumberOfSymbolsßValidateMaxNumberOfSymbols NumberOfspaces ß NumberOfSpaces-1
RETURN NumberOfSpaces
NumberOfSpaces ß (MaxNumberOfSymbols - 1)/2
END FUNCTION
NumberOfSymbols ß 1
END PROCEDURE
As you can see one of the subroutines defined above are a “procedure” whereas the other is a “function. The procedure performs
required instructions without returning any value. Whereas, returns a value after preforming a decrement operation on a variable
“NumberOfSpaces”.
Hassan Zulfiqar Haider For more study material please visit:-
A-Levels/IGCSE/O-Levels Computer Science https://sites.google.com/site/csvault042/home
03234140902
hassanzulfiqarhaider@gmail.com
Paper 2 Fundamental Problem-solving and Programming Skills Unit 12 Algorithm Design and Problem Solving
The problem to be solved: Take as input a chosen symbol and an odd number. Output a pyramid shape made up entirely of the chosen symbol,
with the number of symbols in the final row matching the number input. For example the two input values A and 9 result in the following output:
PYRAMID0 OUTPUT(AdjustNumberOfSpaces)
1PROCEDURE SetValues
CALL SetValues1 INPUT Symbol
REPEAT MaxNumberOfSymbolsß*ValidateMaxNumberOfSymbols1.1
CALL OutputSpaces2 NumberOfSpaces ß (MaxNumberOfSymbols - 1)/2
CALL OutputSymbols3 NumberOfSymbols ß 1
END PROCEDURE
*NumberOfSpacesßAdjustNumberOfSpaces4
*NumberOfSymbolsßAdjustNumberOfSymbols5 2PROCEDURE OutputSpaces
UNTIL NumberOfSymbols > MaxNumberOfSymbols FOR count 1 TO NumberOfSpaces
OUTPUT Space //without moving to the next line
1.1FUNCTIONValidateMaxNumberOfSymbols RETURNS INTEGER END FOR
REPEAT END PROCEDURE
INPUT MaxNumberOfSymbols 3PROCEDURE OutputSymbols
UNTIL MaxNumberOfSymbols MOD 2 = 1 FOR count 1 to NumberOfSymbols
RETURN MaxNumberOfSymbols OUTPUT symbol //without moving to next line
END PROCEDURE END FOR
OUTPUT Newline
0 Main Program (Zero Level Program) END PROCEDURE
4FUNCTION AdjustNumberOfSpaces RETURNS INTEGER
1 Procedure for Setting Values (Sub Routine) NumberOfspaces ß NumberOfSpaces-1
RETURN NumberOfSpaces
1.1 Procedure for Inputting max number of END FUNCTION
symbols (Sub Routine) 5FUNCTION AdjustNumberOfSymbols RETURNS INTEGER
NumberOfSymbols ß NumberOfSymbols+2
2 Procedure for Outputting spaces (Sub RETURN NumberOfSymbols
Routine) END FUNCTION
3 Procedure for Outputting Symbols (Sub
*Function Call VS Procedure Call:-
Routine) When making a Procedure Call you will specifically be using the
CALL keyword. For Example CALL ProcedureXYZ (flowchart version below)
4 Procedure for Adjusting Spaces for Next Row
(Sub Routine)
5 Procedure for Adjusting Symbols for Next
Row (Sub Routine)
Whereas a function call DOES NOT make use of CALL keyword. Instead a function
Sequence of Program Control:- call is part of a an algorithm expression*. For example
0 à 1à1.1à1à0 NumberOfSpacesßAdjustNumberOfSpaces
Loop on 2à0à3à0à4à0à5à0 Here AdjustNumberOfSpaces is a function and it will simply return an integer
value which will be saved in the variable NumberOfSpaces
Further Practice
Hassan Zulfiqar Haider For more study material please visit:-
A-Levels/IGCSE/O-Levels Computer Science https://sites.google.com/site/csvault042/home
03234140902
hassanzulfiqarhaider@gmail.com