AS AND A LEVEL
COMPUTER SCIENCE
TOPIC 9: ALGORITHM
DESIGN AND PROBLEM
SOLVING
9.1 Computational
Thinking Skills
Objectives
(1) Show an understanding of abstraction
Need for and benefits of using abstraction
Describe the purpose of abstraction
Produce an abstract model of a system by only including essential details.
(2) Describe and use decomposition
Break down problems into sub problems leading to the concept of a
program module (procedure / function)
Abstraction
What is abstraction?
Abstraction is a key component in computational thinking involving the process of
simplifying complex systems by focusing on the most essential details and
ignoring less relevant information.
Need for and benefits of abstraction
- Abstraction helps reduce complexity by focusing on the core elements of a
problem making it easier to understand and solve.
- By ignoring unnecessary details, it becomes easier to develop solutions and
reduce the time spent on problem solving.
- Abstract models can be reused in different context or problems, saving time and
resources.
- Abstraction provides a common language that allows different stakeholders e.g
developers and clients to discuss and understand the system at a higher level.
- The program is smaller in size and takes less space and works efficiently and is
easy to modify.
The purpose of abstraction
- The primary purpose of abstraction is to create a simplified model of a system or
problem.
- This model captures the essential features without being bogged down by the
complexity of the real world system.
- Abstraction enables the creation of solutions that are general and adaptable to
different situations.
Examples of abstraction
How to produce an Abstract model?
- An abstract model is a simplified representation of a system that includes only the
essential details necessary for understanding or solving a problem.
Steps to produce an abstract model
1. Identify the essential features by focusing on the most critical elements of the
system or problem that directly impact the solution.
2. Ignore irrelevant details by filtering out any information that does not contribute to
solving the problem or understanding the problem.
3. Create a simplified representation through a model or diagram that represents the
essential features in a clear and concise manner.
For example
In designing a traffic management system, an abstract model might focus on traffic flow
and signal timings while ignoring the specific details of individual vehicles.
Example 1
Decomposition
What is decomposition?
- Decomposition is the process of breaking down a complex problem or task into
smaller sub problems or smaller parts making it easier to explain, understand and
solve the problem leading to the concept of program modules.
Benefits of decomposition
- smaller sub problems are easier to understand, manage and solve than a single
complex problem.
- Smaller problems are easier to program / test / maintain
- Sub-problems can be given to different teams / programmers with different
expertise hence can be solved separately hence speeding up the overall
development process.
- Decomposition leads to modular solutions, where individual components can be
developed, tested and reused independently.
Steps to decompose a problem
(a) Identify the main problem and understand the overall objective or problem that
needs to be solved.
(b) Break down the problem into smaller, more specific tasks or components.
(c) Solve each sub problem individually ensuring that each solution contributes to the
overall solution.
The concept of a Program Module
- A program module is a self- contained unit of code tat performs a specific task
within a larger program. Modules can be procedures (which perform a task) or
functions (which return a value)
Example 1
Example 2
Example 3
9.2 Algorithms
Objectives
(1) Show understanding that an algorithm is a solution to a problem expressed as a sequence of
defined steps.
(2) Use suitable identifier names for the representation of data used by a problem and represent these
using an identifier table
(3) Write pseudocode statements for:
Declaration and initialization of constants
Declaration of variables
The assignment of values to variables
Expressions involving any arithmetic or logical operators input from the keyboard and output to the
console
(4) Write pseudocode that contains input, process and output
(5) Write pseudocode using the three basic constructs of sequence, selection and iteration (repetition)
(6) Document a simple algorithm using a structured English description, a flowchart or pseudocode.
(7) Write pseudocode from:
A structured English description
A flowchart
(8) Draw a flowchart from:
A structured English description
Pseudocode
(9) Describe and use the process of stepwise refinement to express an algorithm to a level of detail
from which the task may be programmed.
(10)Use logic statements to define parts of an algorithm solution.
What is an algorithm?
- An algorithm is a step by step procedure or formula for solving a problem.
- It is expressed as a sequence of clearly defined steps that lead to the desired
outcome.
There several types of algorithms which are:
1. Structured English
2. Flowcharts
3. Pseudocode
4. Program code
(1) Structured English
- it is a written description of an algorithm using structured English, which
combines natural language with programming logic.
- For example, a program that finds the average score of a student.
1. Enter the name of the student
2. Enter the marks of the student
3. Calculate the average by dividing the total marks by the number of subjects
4. Output the average mark.
(2) Flowchart
- It is a graphical or diagrammatic representation of an algorithm using standard
symbols shown in the table below.
START/STOP or BEGIN/END
STOP START
INPUT/OUTPUT or READ/PRINT
INPUT/
OUTPUT
SELECTION or DECISION
Decision
PROCESS
Process
SUBROUTINE
Sub routine
(3) Pseudocode
- A pseudocode is an outline of a program, written as a series of instruction using
simple English statements.
- Pseudocode uses keywords commonly found in programing languages and
mathematical notation.
- An example of pseudocode is shown below
(4) Program code
- This is the actual program code for the algorithm written using programming
langauegs e.g Python, Java, Visual Basic
VARIABLES AND CONSTANTS
- When writing computer programs, there is need to store the data.
- The data can be the values inputted, the result of a calculation or any for any other
reason.
- These data can be stored in variables and constants
What are variables and constants?
- A variable or a constant is a space or location in the memory that is given an
identifier or name and store a value.
How are variables and constants used in programming?
- Variables and constants are used to store items of data and the data stored can
be accessed by the identifier or name of the data store.
- The value of a variable may change during the execution of a program but on the
other hand the value of a constant remains the same during the execution of a program.
- Variables can be used to store results of calculations, counting, totaling or storing
values entered by the user.
- Constants store values that do not change such as Pi in mathematical formulae.
Why are variables and constants used in programming?
- Constants – the value cannot be changed accidentally during the execution of the
program. The value only needs to be changed once if circumstances change.
- Variables – stores a value that can change during the execution of the program. A
variable can be used without knowing its value.
Variable assignment
- Putting data into a variable is done using an assignment statement.
- Variable assignment is a process so is represented using the process block
in a flowchart.
- An assignment statement consists of the identifier (variable name) on the
left of the arrow and the value or expression on the right side of the arrow as
shown below:
Identifier Value
StudentMark
<- 50
- Note: it is important to give meaningful variable identifiers so the whole
program can make sense.
DATA TYPES
- Data used in programming can be of different types.
- It is important to tell the program what type of data it needs to store.
- The table below shows some common data types used in programming.
Data type Description Example data Python
String Text – characters, numbers “hello world” str
and symbols “123”
The data is always inside “Computer Science!”
speech marks (“ “)
Integer This is a whole number 20 int
Note once a number starts 4567
with 0, it becomes a string, it is
no longer an integer. 567
Real Decimal numbers. A number 1.5 float
134.67
with a fractional part that can
be positive or negative.
Boolean Can only have two values, TRUE bool
TRUE or FALSE FALSE
Yes or No.
Character One character or number or “h” char
symbol. Always inside speech “3”
“$”
marks (“ “)
Characters combine to form a
string.
IDENTIFIERS AND IDENTIFIER TABLES
- Identifiers are names given to variables, constants, functions and other elements
in a program to uniquely identify them.
- It is important that the identifiers are descriptive and meaningful.
Guidelines for choosing suitable identifier names
(a) Use names that clearly describe the data or purpose e.g totalMarks instead of
x
(b) Follow a consistent naming convention (e.g camelCase or snake_case)
(c) Avoid reserved words, do not use keywords reserved by the programming
language as identifiers.
- An identifier table is used to document the variables and constants in the
algorithm, providing such details such as the name, data type and purpose.
Example 1
Example 2
Example 3
Example 4
Example 4
DECLARATION OF VARIABLES AND CONSTANTS
- It is also important to declare variable datatype before data is
inputted so that the appropriate data type can be inputted. This is to ensure
the computer or program knows the type of data it is dealing with.
Pseudocode
- Declaration is done at the start of the program before data is
entered.
- Constants are declared by assigning the value to the constant at the
start of the program.
ARITHMETIC LOGICAL AND BOOLEAN OPERATORS
Mathematical operators table
Operator Action Example
+ Adds values together 10 + 2 = 12
- Subtract values 10 – 3 = 7
* Multiplies values 10 * 2 = 20
/ Divides the first number by the second 10 / 2 = 5
number
5 / 2 = 2.5
^ Raise to the power 2^3=8
DIV Gives the whole number after the first number DIV (4 , 2) = 2
is divided by the second and ignores the
decimals DIV (5 , 2) = 2
MOD Gives the remainder after the first number is MOD ( 5 , 2) = 1
divided by the second number.
MOD ( 12 , 9) = 3
Comparison Operators
Operator Description Example
= or == Equals to 10 = 10? TRUE
10 = 2? FALSE
<> or != Not equal to 10 <> 10? FALSE
10 <>2? TRUE
< Less than 10 < 11? TRUE
10 > 11? FALSE
<= Less than or equal to 10 < = 10 ? TRUE
10 < = 11? TRUE
> Greater than 10 > 2? TRUE
10 > 11? FALSE
>= Greater than or equal to 10 > = 2? TRUE
10 >= 10? TRUE
Boolean Operators
Boolean Operator Description Example
AND If both conditions are true, the IF 1 = 1 AND 2 = 2
result is true. This will return TRUE as both conditions
If one or both of the conditions is are true.
false, the result is false
IF 1 = 1 AND 1 > 2
This will return FALSE as the right side
is false.
OR If one or both conditions are true, IF 2 = 3 OR 5 < 6
the result is true. This will return TRUE as the right side is
If both conditions are false, the true.
result is false.
IF 2 = 3 OR 5 > 6
This will return FALSE as both
conditions are false.
NOT Reverses the condition. If the IF NOT (1 = 1)
condition is True it becomes The brackets equal to TRUE but the
False NOT makes it FALSE
INPUT AND OUTPUT
INPUT
- The user might need to input some data into the program.
- the INPUT or READ function is used to input data.
- When the data is inputted, the value is stored in a variable.
- Ensure the variable has an appropriate name that clearly states what is
being stored.
Flowchart Pseudocode
Python code
- It is also important to declare variable datatype before data is inputted so
that the appropriate data type can be inputted.
Pseudocode
Python code
OUTPUT
- The program might need to output information to the user, whether displayed on
the screen or printed out on paper.
- The OUPUT or PRINT function is used. In python the print function is used.
Flowchart Pseudocode
Python
- Sometimes you might want to output more than one piece of data then you can
join them using a concatenation symbol ( + or , ).
- Concatenation means joining together so it joins multiple pieces of data together.
Flowchart pseudocode
Python
PROGRAMMING CONSTRUCTS
- There are 3 basic programming constructs used in programming which are:
Sequence, selection and iteration.
(1) Sequence
- A sequence is a series of statements that are executed once, in the order they are
written.
- The ordering of steps is very important and an incorrect order can lead to incorrect
results or extra steps that are not required by the task.
Example 1
Write a program that allows the user to input the length, width and height of a cube and
calculates the volume of the cube and outputs the volume.
Structured English
1. Enter the length, width and height of the cube
2. Calculate the volume using the formula V = L x W x H
3. Output the volume.
Flowchart
Pseudocode Python
(2) Selection
- Selection is the second programming construct
- In a selection, a condition is checked and this determines which code is run.
- There are two forms of selection, IF statements and CASE statements.
IF STATEMENT
- The structure of the IF statement is shown below
YES /
TRUE
NO /
FALSE
Python
Example 1
A program allows the user to input two numbers and outputs the bigger number.
Pseudocode
Flowchart
Python
- In python, the print function prints strings, so there is need to change the data type of the
variables Num1 and Num2 to strings in - order to perform the concatenation.
Example 2
A program allows a user to guess a number. If the number guessed is 16, then the
user guessed the correct number.
Pseudocode
Flowchart
Python code
- There is also an ELSEIF (elif) statement.
- This allows for a second condition to be used within the same IF statement.
If the first condition is False, then a second condition can be checked.
CASE STATEMENTS
- A case statement is used when there are several choices to be made.
- An example of a CASE statement is shown below:
- In python the “elif” statement is used.
(3) ITERATION