Mojza Computer Notes P2
Mojza Computer Notes P2
COMPUTER
SCIENCE NOTES
Paper 2: Programming and
Algorithm
2210 & 0478
BY TEAM MOJZA
CONTENTS
Problem-Solving
Pg 11 Unit 8 Pseudocode
Pg 26 Unit 9 Databases
1
MOJZA
- Analysis
➜ Abstraction: Removal of unnecessary details and identification of the key elements
of the problem
➜ Decomposition: Breaking down a complex problem into smaller, easier-to-solve
parts
➜ Identification of the problem and requirements
- Design
➜ Pseudocode
➜ Flowcharts
➜ Structure diagrams
➜ How the problem is to be solved, what tasks are required for it and how they work
with each other, and the order of those tasks is found out in this stage
- Coding
➜ Program code is written using a programming language
➜ Iterative testing of the separate modules of the program is carried out, ensuring they
work as they are meant to
➜ Most amendments to the code are carried out in this stage
- Testing
➜ The completed program is tested with test data to spot any errors
2
MOJZA
Computer systems
➜ Made up of subsystems, which are made up of further sub-systems, and so on
➜ Made up of software, data, hardware, communications, and people
➜ The division can shown using top-down design
➜ Top-down design is the breaking down of a system into smaller sub-systems, and
those subsystems into further sub-systems, until each sub-system performs a single
action
Decomposition of a problem
➜ A problem can be decomposed by the identification of its component parts
➜ These include the inputs, processes, outputs, and storage required for the solution
- Inputs
➜ The data that is to be entered into the system for processing while it is active
- Processes
➜ The tasks that need to be performed on the data input and and/or the data
previously stored in the system
- Outputs
➜ The data that needs to be displayed or printed for the users of the system
➜ The results of the various processes carried out in the system using the input data
and/or the previously stored data
- Storage
➜ Data that needs to be stored in an appropriate medium in the system so that they
can be used throughout the program as needed
3
MOJZA
- Structure diagrams
➜ Hierarchical diagrams that show the decomposition of a system
➜ Display top-down design diagrammatically
➜ Show the solution to a problem in a hierarchical way, dividing it into subsystems and
dividing those subsystems into further sub-systems, until each sub-system performs a
single action
4
MOJZA
- Flowcharts
➜ Diagrammatically show the steps required, and the order in which they are to be
carried out to complete a task
➜ The steps, along with the order, are called an algorithm
➜ Have standard flowchart symbols for different processes
5
MOJZA
Standard methods of solution
➜ Linear search
➜ Bubble sort
➜ Totalling
➜ Counting
➜ Finding maximum, minimum and average values
- Linear search
➜ Used to search for a certain value/variable, etc.
➜ Checks all the elements of the data structure being searched
Example
OUTPUT “Please enter the name to find”
INPUT Name
Found ← FALSE
Counter ← 1
REPEAT
IF Name = StudentName[Counter]
THEN
Found ← TRUE
ELSE
Counter ← Counter + 1
ENDIF
UNTIL Found OR Counter > ClassSize
IF Found
THEN
OUTPUT Name, “ found at position ”, Counter, “ in the list”
ELSE
OUTPUT “Name not found”
ENDIF
//pseudocode to find the name stored in Name
6
MOJZA
- Bubble sort
➜ Used to arrange elements in a data structure in ascending or descending order
➜ Always has a variable to temporarily store data in between swaps
➜ Compares each element with the next consecutive one, and swaps if needed
Example:
DECLARE StudentAge : ARRAY [1:10] OF INTEGER
Last ← 10
FOR Counter ← 1 TO 10
OUTPUT “Enter student’s age”
INPUT Age
StudentAge[Counter] ← Age
NEXT Counter
REPEAT
Swap ← FALSE
FOR Counter ← 1 TO Last - 1
IF StudentAge[Counter] > StudentAge[Counter + 1]
THEN
Temp ← StudentAge[Counter]
StudentAge[Counter] ← StudentAge[Counter +1]
StudentAge[Counter + 1] ← Temp
ENDIF
NEXT Counter
Last ← Last - 1
UNTIL (NOT Swap) OR Last = 1
//pseudocode to arrange the students’ ages in StudentAge in
//ascending order
- Totalling
➜ The process of keeping a running total of values in a program
Examples:
TotalCost ← TotalCost + FurnitureCost
TotalBaskets ← TotalBaskets + BananaBaskets
7
MOJZA
- Counting
➜ Number of times an action is performed in a program
Examples:
Counter ← Counter + 1
NumberInStock ← NumberInStock - 1
Validation
➜ The checking that the data entered is reasonable/sensible
➜ There are several kinds of validation checks
8
MOJZA
//This may not be needed as the data type
//of Age can be declared INTEGER
Verification
➜ The checking if data has been accurately copied from one medium to another
➜ Does not check for any ranges, boundaries, etc.
➜ Only checks if the data is identical to the original source
Visual Manual check done by Data is displayed on the screen and user is asked to
the user to see if the confirm their accuracy
data entered is correct
9
MOJZA
Test Data
➜ Data that has been specifically identified for testing a program
➜ Used to work through a program to find any errors and ensure that it is working like it
is meant to
Normal test data: Data that will be accepted by the program
Abnormal/Erroneous test data: Data that will be rejected by the program
Boundary test data: The largest/smallest accepted values, and their corresponding
largest/smallest rejected values
Extreme data: The largest and smallest accepted values
10
MOJZA
UNIT 8: Pseudocode
(NOTE: meta-variables, i.e: symbols in the pseudocode that should be substituted with
other symbols, are enclosed in angled brackets <>)
(NOTE: All the examples are in PSEUDOCODE. None of the programming languages
are used in these notes.)
- Variable declaration
Examples:
DECLARE MyName : STRING
DECLARE Flag : BOOLEAN
- Constant declaration
➜ It is recommended to use constants if it makes the program more readable, and
easier to update if the value of the constant changes
➜ Only literals must be used as the value of a constant; a variable, another constant, or
an expression must never be assigned to a constant
Example:
CONSTANT StudentNo ← 30
CONSTANT Message ← “You have completed this level”
11
MOJZA
- Identifiers
➜ Names given to variables, constants, procedures, data structures, and functions
➜ Must be meaningful, and not arbitrary
➜ Are not case sensitive; for example, StudentNumber and Studentnumber must
not be treated as different variables
➜ Keywords should never be used as identifier names, like Repeat or Procedure
➜ Should never start with a number e.g: 4Digit
➜ Should not include accented letters and other characters, including the underscore
(_)
➜ Can only contain uppercase (A-Z) and lowercase (a-z) alphabets, and the digits 0-9
➜ Must start with a capital letter
Data types
- Sequence
➜ The order in which statements in a program are executed
➜ Sequence is very important, as an incorrect order can lead to errors
- Selection
➜ Performed when different actions need to be performed based on the values entered
into the algorithm
➜ There are two types of selection statements:
↳ IF statements
↳ CASE statements
➜ Also called conditional statements
12
MOJZA
- IF statements
➜ IF…THEN…ELSE…ENDIF
➜ THEN path is followed if the set condition evaluates to TRUE
➜ ELSE path is followed if the set condition evaluates to FALSE
➜ May have more than two conditions
➜ May or may not have an ELSE clause
Format Example
13
MOJZA
- CASE statements
➜ CASE OF…OTHERWISE…ENDCASE
➜ Allow one of the several branches of the code to be executed, depending on the
value of a variable
➜ OTHERWISE is used as the last case as the path to be followed if all preceding cases
evaluate to FALSE
Format Example
- Iteration
➜ When part of the code needs to be repeated either a set number of times or until a
set condition evaluates to TRUE or FALSE
➜ There are three types of iteration loops
➜ count-controlled loops
➜ pre-condition loops
➜ post-condition loops
14
MOJZA
- Count-controlled loops
➜ FOR…TO…NEXT…
➜ Used when there is a known, set number of repetitions
➜ More efficient than the other loops as the counter variable does not need to be
managed/the counter variable increments by itself
Format Example
- Pre-condition loops
➜ WHILE…DO…ENDWHILE
➜ Used when there is no known number of repetitions
➜ Tests the condition prior to the execution of the statements for each repetition
➜ If it evaluates as TRUE, the statements are executed; otherwise, the loop is
terminated
➜ May not be executed even once if the condition evaluates as FALSE when it is tested
for the first time
➜ The condition must be an expression that evaluates to a Boolean
Format Example
15
MOJZA
- Post-condition loops
➜ REPEAT…UNTIL
➜ Used when there is no known number of repetitions
➜ Tests the condition after the execution of the statements for each repetition
➜ If the condition is evaluated as FALSE, the statements are executed for the next
repetition; otherwise, the loop is terminated
➜ Is always repeated at least once
Format Example
REPEAT REPEAT
<statements> INPUT Number
UNTIL UNTIL Number < 0 OR Number >100
//This loop repeats until the number input is smaller
//than 0 or larger than 100
- String handling
➜ Length : Finding the number of characters in a string
➜ Substring : Extracting a part of a string
➜ Uppercase : Converting all the letters in the string to uppercase
➜ Lowercase : Converting all the letters in the string to lowercase
➜ The first character of a string can be in position one or zero; however, it is generally
one
16
MOJZA
The second
parameter is the
position of the
start character
The third
parameter is the
length of the
substring
- Arithmetic operators
Operator Function
+ Add
- Subtract
* Multiply
/ Divide
( ) Group
17
MOJZA
- Logical operators
Operator Comparison
= Equal to
- Boolean operators
Operator Meaning
AND Both
NOT Not
OR Either
- Procedures
➜ A standard subroutine that does not return a value
➜ Can have up to two parameters in our syllabus
➜ A parameter is a value sent to the subroutine
➜ The variables declared and used within it are local variables; their scope covers only
the procedure
➜ Value is not returned
Format
PROCEDURE <identifier>
<statements>
ENDPROCEDURE
CALL <identifier>
//calling the procedure
18
MOJZA
Example
PROCEDURE Welcome
OUTPUT “Welcome back to the game, player!”
OUTPUT “Click SKIP to skip the tutorial”
ENDPROCEDURE
CALL Welcome
Format
Example
- Functions
➜ A standard subroutine that always returns a value
➜ Can have up to two parameters in our syllabus
➜ The variables declared and used within it are local variables; their scope covers only
the function
➜ Since functions return a value when they are called; a function call is not a complete
programming statement
➜ It is called with an expression
19
MOJZA
- Functions without parameters:
Format
<expression> <identifier>
//calling the function
Example
OUTPUT One()
//calling the function
Format
Example
20
MOJZA
- Local and global variables
➜ Local variables are variables that are declared for use in a specific part of a program
➜ Their scope is restricted to that part of the program
➜ Examples of local variables include those which have been declared in a procedure
and/or function
➜ Global variables are variables that are declared for use all over a program
➜ Their scope is not restricted to any specific part of the program
- Library routines
➜ Standard subroutines that are available for immediate use
➜ Some examples of library routines are MOD, DIV, ROUND, and RANDOM
➜ Identifiers are of integer data type in MOD and DIV
21
MOJZA
- Comments
➜ Are important for making program code more understandable
➜ Normally, comments are on a separate line before, and at the same level of
indentation as, the code they refer to
➜ Occasionally, however, a short comment that refers to a single line may be at the end
of the line to which it refers
➜ In pseudocode, comments are preceded by two forward slashes //
➜ The comment continues until the end of the line
➜ For multi-line comments, each line is preceded by //
Example:
// This procedure swaps
// values of X and Y
PROCEDURE SWAP (X : INTEGER, Y : INTEGER)
Temp ← X // temporarily store X
X ← Y
Y ← Temp
ENDPROCEDURE
Arrays
➜ Fixed-length structures of elements of the same data types
➜ The elements are accessible by consecutive index numbers
➜ The lower bound (i.e: the index of the first element) can be one or zero
➜ However, it is generally one
➜ The upper bound (i.e: the index of the last element) of the array can be any integer
22
MOJZA
- 1-D arrays
- Declaration:
Format
DECLARE <identifier> : ARRAY [<l>:<u>] OF <data type>
Example
DECLARE Name : ARRAY [1:10] OF STRING
- Input/Output values:
Input
Name [2] ← “Bruce Wayne”
Output
OUTPUT Name[2]
- Initialisation/assignment of values:
FOR Counter ← 1 TO 10
Name[Counter] ← “”
NEXT Counter
- 2-D arrays
- Declaration:
Format
DECLARE <identifier> : ARRAY [<l1>:<u1>, <l2>:<u2>] OF <data
type>
l1 is the lower bound of the number of rows in the array, and u1 is its upper bound
l2 is the lower bound of the number of columns in the array, and u2 is its upper
bound
Example
DECLARE StudentMark : ARRAY [1:ClassSize, 1:SubjectNo] OF REAL
23
MOJZA
- Input/Output values:
Input
StudentMark [3,5] ← 45.0
Output
OUTPUT StudentMark [3,5]
- Initialisation/assignment of values:
FOR RowCounter ← 1 TO ClassSize
FOR ColumnCounter ← 1 TO SubjectNo
StudentMark [RowCounter : ColumnCounter] ← 0.00
NEXT ColumnCounter
NEXT RowCounter
File Handling
➜ Before reading from or writing to a file, explicitly opening it and stating the mode of
operation is a good practice
➜ There are two modes of operation:
↳ READ : for data to be read from a file
↳ WRITE : for data to be written to a file
➜ A file can only be opened in one mode at a time
24
MOJZA
READ
Format
OPENFILE <File identifier> FOR READ
READFILE <File identifier>, <Variable>
CLOSEFILE <File identifier>
Example
OPENFILE MyFile.txt FOR READ
READFILE MyFile.txt, LineOfText
CLOSEFILE MyFile.txt
WRITE
Format
OPENFILE <File identifier> FOR WRITE
WRITEFILE <File identifier>, <variable>
CLOSEFILE <File identifier>
Example
OPENFILE MyFile.txt FOR WRITE
WRITEFILE MyFile.txt, LineOfText
CLOSEFILE MyFile.txt
25
MOJZA
Unit 9: Databases
Databases:
➜ A database is a structured collection of data that allows people to extract information
in a way that meets their needs.
➜ Data can include text, images, numbers.
➜ Single-table database contains only one table
➜ Can store information about people, products, events, timings and more
TABLE
Record 1 Field 1 Field 2 Field 3 Field 4 Field 5
Column
➜ The table has a fixed name, such as TICKETS (this table is for bus tickets)
➜ Each record will be different with fields
➜ Each field will have different data such as FirstName, LastName, TicketNo, BusNo,
and more. There will be no spaces in between
26
MOJZA
➜ Example of a table:
TICKETS
TicketNo FirstName LastName BusNo Price Destination
➜ Databases will also use validation checks when data is being entered
➜ Validation checks of presence check, range check, type check, length check can be
done
➜ Each field will have a specific data type and will only accept the data given in that
specific data type
➜ There are 6 basic data types used in databases:
27
MOJZA
TICKETS
SQL
➜ SQL stands for Standard Query Language
➜ It is for writing scripts to obtain useful information from a database and display it
Operator Description
= Equal to
28
MOJZA
Example 1:
The following SQL query displays the first name of all people going to New York
or Florida in alphabetical order, their last names, and their ticket number from
the table TICKETS.
This returns:
Note that the order in which the script has asked for the information (in this case, first
name, last name, and then ticket number) is very important, and so, it must be
displayed according to it
29
MOJZA
Example 2:
The following SQL query displays the prices of tickets, in descending order, for
buses that are either going to New York, Queens or Las Vegas, their bus
numbers, and their respective destinations.
This returns:
30
MOJZA
➜ There are 6 different logic gates given in 2023, 2024 and 2025 syllabus:
Truth Tables:
➜ Used to trace the output from a logic gate or circuit
➜ Each logic gate is currently restricted to two inputs (except NOT, which has only
one)
➜ Each input will give a different output, based on the logic gate
31
MOJZA
Logic gates:
1 0
2) AND gate 0 0 0
➜ Output is 1 when BOTH inputs are 1
0 1 0
➜ X = A AND B (logic notation)
1 0 0
1 1 1
3) OR gate 0 0 0
➜ When either or both input/s is 1, the output 0 1 1
will be 1
➜ X = A OR B 1 0 1
1 1 1
32
MOJZA
5) NOR gate 0 0 1
➜ Opposite of OR gate 0 1 0
➜ Output will be 1 when 1 is NOT an input
➜ X = A NOR B 1 0 0
1 1 0
1 1 0
33
MOJZA
:
A B C
0 0 0
0 0 1
0 1 0
0 1 1
1 0 0
1 0 1
1 1 0
1 1 1
- A problem statement
➜ Form a logic expression from the problem statement
➜ Draw the logic circuit
34
MOJZA
Example:
A gas fire has a safety circuit made up of logic gates. It generates an alarm (X = 1) in
response to certain conditions.
Solution:
35
MOJZA
- A logic expression:
➜ Identify the innermost pair of brackets, and work your way out of it to draw the logic
circuit properly
Example:
Solution:
- A truth table:
➜ Identify the rows where the output is 1
➜ Form a logic expression
➜ Draw the logic circuit
36
MOJZA
Example:
A B C X
0 0 0 0
0 0 1 0
0 1 0 1
0 1 1 0
1 0 0 0
1 0 1 0
1 1 0 0
1 1 1 1
Solution:
➜ Identify the rows where the output is 1
➜ Write each row’s expression:
➜ Row 3: (NOT A AND B) AND (NOT C)
➜ Row 8: (A AND B) AND C
37
MOJZA
Example:
A gas fire has a safety circuit made up of logic gates. It generates an alarm (X = 1) in
response to certain conditions.
38
MOJZA
Solution:
X is 1 if : (NOT G AND C) OR (C AND L)
Working Space
G C L X
NOT G (P) P AND C C AND L (Q OR R)
(Q) (R)
0 0 0 1 0 0 0
0 0 1 1 0 0 0
0 1 0 1 1 0 1
0 1 1 1 1 1 1
1 0 0 0 0 0 0
1 0 1 0 0 0 0
1 1 0 0 0 0 0
1 1 1 0 0 1 1
You must show your working in the provided working space, and you can name each
column an alphabet (like ‘P AND C’ was named ‘Q’ above) for your ease.
- A logic expression:
➜ Make the logic expression for the problem statement
➜ Make a truth table with three inputs and complete it
Example:
((A AND B) NOR (NOT A)) AND (B NAND C)
39
MOJZA
Solution:
Working Space
A B C X (F AND
A AND B NOT A D NOR E B NAND G)
(D) (E) (F) C (G)
0 0 0 0 1 0 1 0
0 0 1 0 1 0 1 0
0 1 0 0 1 0 1 0
0 1 1 0 1 0 0 0
1 0 0 0 0 1 1 1
1 0 1 0 0 1 1 1
1 1 0 1 0 0 1 0
1 1 1 1 0 0 0 0
- A logic circuit:
➜ In the working space, name each part of the circuit with an alphabet for your ease
➜ Make the truth table and complete it
Example:
40
MOJZA
Solution:
Working Space
A B C X (F AND
A AND B NOT A D NOR E B NAND G)
(D) (E) (F) C (G)
0 0 0 0 1 0 1 0
0 0 1 0 1 0 1 0
0 1 0 0 1 0 1 0
0 1 1 0 1 0 0 0
1 0 0 0 0 1 1 1
1 0 1 0 0 1 1 1
1 1 0 1 0 0 1 0
1 1 1 1 0 0 0 0
- A problem statement
➜ Identify the conditions
➜ Write their logical equivalents
41
MOJZA
Example:
A gas fire has a safety circuit made up of logic gates. It generates an alarm (X = 1) in
response to certain conditions.
Solution:
➜ Gas pressure is correct: NOT G (because G = 0)
Carbon monoxide level is too high : C (because C = 1)
42
MOJZA
- A logic circuit:
➜ Separate the logic circuit into each of its sections, going from left to right
➜ Combine them, using brackets as needed, and form the logic expression
Example:
Solution:
➜ The first two sections are:
A NOR B
B AND C
43
MOJZA
- A truth table:
➜ Identify the rows which have 1 as an output
➜ Write their logic expressions
➜ Combine with OR
Example:
A B C X
0 0 0 0
0 0 1 0
0 1 0 0
0 1 1 1
1 0 0 0
1 0 1 1
1 1 0 0
1 1 1 1
Solution:
➜ Rows with 1 as an output are highlighted
➜ Row 4:
((NOT A) AND B) AND C
➜ Row 6:
(A AND (NOT B)) AND C
➜ Row 8:
(A AND B) AND C
44
MOJZA
45