Revision Guide - Paper 1
Revision Guide - Paper 1
Science (8525)
Paper 1: Computational Thinking
And Programming Skills
Revision Booklet
Topic 1: Fundamentals of Algorithms
Algorithms – A set of steps which can be followed to complete a certain task
A computer program may implement one or more algorithms.
A computer program is not an algorithm
Decomposition - Breaking down large problems into a set of smaller sub problems.
Each sub-problem accomplishes a clear, identifiable task.
Sub-programs may be further broken down if needed.
Advantages:
▪ Smaller problems are easier to solve
▪ Each part can be solved independently
▪ Each part can be tested independently
▪ The parts are combined to produce the full problem.
▪ Allows each smaller problem to be examined in more detail
Advantages:
▪ Allows the creation of a general idea of how to solve the problem.
▪ Provides focus on what actually needs to be done.
▪ Provides a simple view of the problem
Flowcharts
▪ Created to represent an algorithm. Start or End Process
▪ Show the data that is input, and output.
▪ Show processes that take place.
▪ Show any decisions and repetitions that Input or YES
take place. Decision
Output
▪ Lines show flow through the chart.
▪ Shapes represent different functions Subprogram NO
Stage X Y Output
Trace Tables – a method of recording the 1 3 1
values used within an algorithm at each stage of X = 3 2 2
processing to help in troubleshooting Y = 1
3 2
▪ Tests algorithms for logic errors which while X > 0
Y = Y + 1 4 3
occur when the algorithm is executed.
X = X - 1 5 1
▪ Simulates the steps of algorithm.
print(Y) 6 4
▪ Each stage is executed individually allowing
7 0
inputs, outputs, variables, and processes to
8 4
be checked for the correct value at each
stage.
▪ A great way to spot errors
Efficiency
There is often more than one algorithm which will solve a given problem
We can compare the efficiency to help us compare algorithms and work out which is the best
The less time an algorithm takes to complete its task, the more efficient it is
Steps which improve efficiency:
• Using repetition (loops) to reduce the amount of code?
• Using arrays instead of declaring many individual variables?
• Using selection statements which only make comparisons until a solution is reached?
Searching Algorithms
Linear Search
1) Check the first value
2) If it is desired value
a) Stop
3) Otherwise check the second value
4) Keep Going until all elements have been checked or the value is found
Binary Search
1) Put the list in order.
2) Take the middle value.
3) Compare it to the desired value.
a) If it is the desired value.
i) Stop.
b) If it is larger than the desired value.
i) Take the list to the left of the middle value.
c) If it is smaller than the desired value.
i) Take the list to the right of the middle value.
4) Repeat step 3 with the new list.
Linear Search Binary Search
Pros• Works with unsorted lists • More efficient
• Not affected by changes to the • Efficient for large lists
list
• Works well for small lists
Cons • Slower • Does not work with unsorted
• Inefficient for large lists lists
Sorting Algorithms
Bubble Sort
1) Take the first element and second element
2) Compare the two
a) If element 1 > element 2
i) Swap them over
b) Otherwise
i) Do nothing
c) Move to the next pair in the list
d) If there are no more elements return to step (1)
e) Otherwise, return to step (2)
3) Repeat until you have worked through the whole list without making any changes
Merge Sort
1) Split the list into individual elements.
2) Merge the elements together in pairs, putting the smallest element first.
3) Merge two pairs together, putting the smallest first.
4) Keep merging until all pairs are in order.
Variables - a box in which data may be stored. The value can be changed as needed whilst the
program is running
▪ Different types e.g. string, decimal, etc.
▪ Allows the program to store data such as an input for later use
Selection
▪ Allows the program to make a ← 1 a ← 1
decisions IF (a MOD 2) = 0 THEN IF (a MOD 2) = 0 THEN
▪ Uses conditions to change the OUTPUT 'even' OUTPUT 'even'
flow of the program ENDIF ELSE
OUTPUT 'odd'
▪ Selections may be nested one
ENDIF
inside another
▪ IF statements perform
comparisons sequentially and so the order is important
▪ The ELSE statement allows us to use multiple
IF GameWon THEN
conditions. … Instructions here …
IF Score > HighScore THEN
Nested Selection and Iteration – using more … Instructions here …
than one iteration or selection, one within the other. ENDIF
… Instructions here …
▪ This provides more flexibility and choices. ENDIF
▪ We can nest statements many layers deep.
▪ This allows for more complex decisions to be WHILE NotSolved
… Instructions here ...
made.
FOR i ← 1 TO 5
… Instructions here …
Choosing Names ENDFOR
▪ Names of variables, constants and sub programs … Instructions here …
ENDWHILE
should be meaningful.
▪ They should give an indication as to the purpose
and function.
▪ This makes it much easier to understand the code
Operators – Allow us to work with data, to change it and compare it
Arithmetic Operators
+ Addition
- Subtraction
* Multiplication
/ Division
MOD Modulus (the remainder from a division, e.g. 12 MOD 5 gives 2)
DIV Quotient (integer division, e.g. 21 DIV 5 gives 4)
^ Exponentiation (to the power of, e.g. 3^3 gives 27)
Comparison Operators
== Equal to
!= Not equal to
< Less than
<= Less than or equal to
> Greater than
>= Greater than or equal to
Boolean Operators
AND - two conditions must be met for the statement to be true
OR - at least one condition must be met for the statement to be true
NOT – inverts the result, e.g. NOT(A AND B) will only be false when both A and B are true
Data Structures – defines how data flows via inputs, processes and outputs
RECORD Car
make : String Records – a method for storing related data
model : String Allows different types of related data to be stored together.
reg : String Several records can be held together in an array.
price : Real Each field in a record can contain different types of data.
noOfDoors : Integer
ENDRECORD
Parameters
Allow us to provide (pass) data to the subroutine for processing.
Subroutines can accept more than one parameter.
They are passed using the syntax SUBROUTINE
subroutineName(prarmeter1, pameter2)
The subroutine must be passed the same number of parameters as it
was created to expect.
Providing too many or not enough parameters will cause errors.
Returning Values
The subroutine will often need to return an output back to the main program.
This allows the result of processing carried out in the subroutine to be used in the main program.
This is done using the Output ‘return value’ code in a subroutine.
This is important because any variables used within the subroutine are not available to the main
program.
Local Variables
Variables created within a subroutine are called Local Variables.
They only exist whilst the subroutine is running.
They are only accessible to that subroutine and not to other subroutines or the main program.
This makes the code easier to debug, as the variable will only exist within the subroutine.
This improves efficiency since the variable does not exist when the subroutine is not running, freeing
up memory.
Global Variables
Global variables can be used anywhere within the code.
Because they can be altered anywhere in the program they are harder
to troubleshoot.
Range Check – makes sure that the data is within a certain range, this is usually used for numbers
and dates. For example, a child’s date of birth must be between the current date, and the current
date minus 18 years.
Length Check – makes sure the data is the correct length, and not too long or short. For example,
the first line of an address must be at least one character long, but not more than 50 characters.
Presence Check - makes sure a value has actually been entered. For example, an email address
must be entered to sign up for a newsletter.
Format Check – makes sure the data is in the correct format. For example, a date must be in the
format DD/MM/YYYY
Type Check – Makes sure the data is of the correct data type. For example, a quantity must be an
integer.
Below is an example of a validation subroutine to have the user input a number between a lower
and upper value provided as parameters.
Test Data
Different types of data will need to be entered into the program to test it
is working correctly.
This data should cover a range of different data which the program might
have to deal.
It should cover possible data, which is normal and allowed, and impossible, which is not allowed.
Normal Data – is typical data that the program
is likely to encounter and should be able to Example Test Data for Subroutine Which
process without error. Validates Input is Between 1 and 100
Boundary Data – is data at the limit of what the Normal Data: 10, 50, 75
program will and will accept. Boundary Data: 0, 1, 100, 101
Erroneous Data – is data which is not valid. Erroneous Data: 200, -500, John, Four
Types of Error
A program with a syntax error will not run. A program with a logic error will run but it will not
perform as expected.
Syntax Errors
When the code does not follow the syntax rules of the programming language used. This stops the
program from running.
Examples:
• Misspellings or typos
• Using a variable before it has been declared
• Missing or incorrect use of brackets
Logic Errors
The program runs but does not do what it should.
Examples:
• Incorrectly using logical or Boolean operators
• Creating infinite loops
• Incorrectly using brackets in calculations
Using the same variable name at different points for different purposes