see revision website links for questions and help ... · a logical way of getting from the problem...
TRANSCRIPT
Support for Paper 2 – See Revision website links for questions and help powerpoints on Technical Topics
Computational Thinking:
1. Abstraction: Looking at a set of information or text and removing unwanted information to leave only relevant information or the information asked for.
2. Decomposition:
Breaking a difficult or complex problem into sub problems and then into smaller parts to be solved individually.
3. Algorithm:
A logical way of getting from the problem to the solution either using a flowchart, set of instructions or pseudocode.
Algorithm symbols:
Start/stop Input/output Process/calculation
Decision – is …..? Sub routine –
a sub program within main program
Sequence, Selection + iteration
Sequence: A set of instructions or commands where there is only one possible route from start to finish.
Selection
There are multiple ways from start to end.
This usually involves decision boxes in an algorithm or
using IF THEN ELSE when programing
Iteration
This would include a loop which will allow you to repeat a task eg counters
Searching Algorithm methods
Binary Search:
Only looks for items in an ordered list eg 7 21 36 46 62 71
Method (Find the value 46 in the list):
1. Find the middle value eg 36
2. Compare 46 to middle value - it is smaller so remove all values lower than 36.
3. Values left are 46 62 71
4. Repeat Steps 1, 2 and 3 ( eg middle value is now 62 – compare – it is higher so remove all higher values
5. You are now left with 46.
Searching Algorithm methods
Linear Search:
Mainly looks for items in an unordered list eg 7 35 18 62 46 71
Method (Find the value 71 in the list):
1. Look at the 1st item – 7 - stop if it is the value you are looking for.
2. It is not so look at next item – 35 - stop if it is the value you are looking for.
3. Repeat step 2 until you find the value you’re looking for.
It is simpler than a binary search, but not as efficient and can be much slower if the value is towards the end of a list.
Only useful in small lists!!
Sorting Algorithm methods - Bubble Sort:
• This compares 2 values or a pair of numbers at a time.
• Used to sort an unordered list of items.
• It is very simple but can take a long time.
Method:
Sort these numbers in numerical order –
2 1 6 3 5 4
Sorting Algorithm methods - Merge Sort:
Method:
• This uses a method of splitting a large list in half each time using sub lists.
• Compare 2 values and rearrange theme in size or alphabetical order.
• Merges back together and then check if in order ?
• Repeat process again until list is sorted
• It is more efficient than a bubble sort for large lists.
Sort these numbers in numerical order –
7 2 1 6 3 5 4 8
Sorting Algorithm methods – Insertion Sort: Method:
• This takes an item(2nd in list) and compares against others in the list and just simply places it in the correct place.
• Repeats this action with the next value in the list(3rd value).
• Repeats until all values are in the correct order.
• It is a very quick method on small lists,
but would not be as efficient
as a merge sort on large lists.
Data Types + Casting In programming we need to store data as different types. You need to know these types:
integer – whole numbers
real – numbers with a decimal part
Boolean – 2 values – T/F, Y/N, 1/0
character – 1 letter, number, symbol -
string – text – Gh3r%
What is Casting:
When you convert between data types - We have used this when saving to file changing the integer value to a string
eg asking for age: age=int(input(“what is your age”)) THEN BECOMES str( age)
Then when we may need to save to file we would change the data type:
File.write ……………..(name+ gender + str(age))
Operators:
These are simply special characters to perform calculations or functions in maths.
The 4 standard ones are +, - , * and / .
Equally as important and used in Pseudocode questions are :
^ or ** Powers eg 2 ^ 4 = 16 ( 2⁴) (Exponentiation)
DIV eg 20 DIV 6 = 3 ( 6 goes into 20 3 times) (Quotient)
MOD eg 11 MOD 5 = 1 ( Remainder is 1 – 5 goes into 11 twice rem 1)
Comparison Operators:
These are used to compare values on the left and right of the expression.
== is equal to eg 5 == 5 is True
!= or <> is not equal to
< is less than
> is greater than
<= is less than or equal to
>= is greater than or equal to
these are used to produce a True or False outcome eg Boolean result
SQL: (Structured query language)
• This is used in tables or databases of information.
• It is mainly used for searching for information
• Can also create a table and update information.
Example 1 : Looking for all the hotel names and ratings:
SELECT hotel_name, hotel_rating FROM hotels (table)
Part 2 – Filter the results:
WHERE price < = 80 AND rating > = 4
( price is less than or equal to 80 and Rating is 4 or more)
Part 3 – Sort results - Price highest first
ORDER by price DESC
Arrays :
An array is a data structure that allows you to hold several variables, all of the same type, with one name.
Each item is called an element with a position in the list called an index.
The 1st item in the list has position 0
Eg five different resorts with great beaches, you could write the pseudocode statement
resort [“Margate”, “Swansea”, “Brighton”, “St Ives”, “Cromer”]
so resort [ 0 ] = Margate
resort [4] = Cromer
2D Arrays :
Suppose you wanted to hold in a program, 5 marks for each of 3 students
Marks
Student1 12 14 8 7 17Student2 6 8 5 3 13Student3 16 15 9 10 18
These marks could be put into a 2D array
The position of each element or item is usually written [ a ] [ b ] where a is the row b is the column
You can set individual items in the array like this: mark [2] [4] =18 eg Row 3, column 5
example 2: What is the value of mark[1][0]? = 6 eg Row 2, column 1
Questions
What is the value of mark [0] [0]?
What is the value of mark [0] [2]?
What is the value of mark [1] [3]?
String manipulation:
Remember strings are a data type made of mixed characters.
Concatenation – strings can be joined together
eg fullname = firstname + surname
Useful types of manipulation:
.upper – changes characters to uppercase
.lower - changes characters to lowercase
.length – counts the number of characters in a string
x[ i ] – Finds the position of i from string x
town.substring (0,3) - this finds the 1st 3 characters of the town.
File handling:
This deals with how a program can access and change data in an external file.
newfile = open Read(“newfile.txt”) - opens the file in read mode
newfile = open Write(“newfile.txt”) - write data to the file
newfile.close() - closes the file
variable.writeLine( name + str(age) + …..) - writes variables to file
first_line = newfile. readLine() - reads the 1st line of the file
endoffile() - used to stop reading all lines of file
TestingTesting:
This is needed to make sure that a program is fully working, bug free and meets the client’s needs.
2 main types:
Iterative testing – When the program goes through the system life cycle a few times to meet the customers needs egRequirements/Design/implementation/Testing
Final testing – usually when the program has gone through iterative testing and has one final run through the system life cycle. If all requirements are met the program is ready for the customer.
Constants and variables
Data values can be stored as a constant or a variable
• Constant values are assigned and can’t be changed • If you attempt to change a constant it may return an error when being compiled.
Eg CONST pressure as INT = 30
A variable is a value that can be changed eg Temperature, name, weight
Variables can either be classed as LOCAL or GLOBAL.
• Local variables are only ever contained inside the function or procedure
• Global variables can be used anywhere else in the program
Switch – Case statements
This is used when you want to use the same variable for different actions for different values.
Voting/Election program• 3 people – John, Sue, Alan• All start with 0 votes• A variable called vote is used • If John, Sue or Alan is entered the totalof votes goes up by 1 for that person• It saves using a different variable for the 3 different options
Validation and Sanitisation
This is used to make sure that when a user inputs data they are not allowed to enter unwanted or invalid data.
Programmers will try to predict how users may misuse a program and try to prevent it from crashing.
Input Sanitisation – Remove any unwanted characters in a program
Input Validation – Checks if data meets certain criteria eg @ included in email address and ends with .com, co.uk
White list – All data that the program should accept
Black list – All data that the program should reject
Functions
• A function will always return a value eg Area
• It will also need at least one parameter eg pi and radius
Example of a function:
FUNCTION area_circle ( pi, radius)
area=pi*radius*radius
return area
END FUNCTION
Procedures
• A procedure is a set of instructions stored under one name eg hello()
• to repeat these set of instructions you only need to type in hello() at a different place in the program
• A procedure doesn’t have to take a parameter but could!
• It doesn’t return a value
Example of a Procedure: A parameter or argument could be added here
PROCEDURE hello() variable added here
print(“ Hello Year 11”)
print(“this is a procedure”)
END PROCEDURE
Examples of Pseudocode
Iteration example:
FOR k = 1 to 100
Normal program text in here
NEXT K
Pseudocode:
Try to use key phrases like the following:
password = INPUT(“Enter your password”)
IF length of password <=6 THEN
print(“Password is too short”)
ELSE
print(“Password is valid”)
ENDIF
While loop example:
WHILE total < cost
coin = INPUT(“Enter coin value)total = total + coin
ENDWHILE
Repeat loop example:
REPEAT
coin = INPUT(“Enter coin value)total = total + coin
UNTIL total > cost
Examples of Pseudocode
Iteration example: FOR Loop
FOR k = 1 to 100
Normal program text in here
NEXT K
Pseudocode:
Try to use key phrases like the following:
password = INPUT(“Enter your password”)
IF length of password <=6 THEN
print(“Password is too short”)
ELSE
print(“Password is valid”)
ENDIF
While loop example:
WHILE total < cost
coin = INPUT(“Enter coin value)total = total + coin
ENDWHILE
Repeat loop example:
REPEAT
coin = INPUT(“Enter coin value)total = total + coin
UNTIL total > cost
Types of Languages and Translators:
Machine code is an instruction that can run inside a CPU - binary or hex
Assembly code refers to mnemonic instructions eg keyterms that do instructions
eg ADD, SUB, STORE
Code could be either high level or low level language.
Examples of high level language – Python, Java
Examples of low level language – Assembly code
Translators:
A translator is a piece of software that converts programming code to machine code.
There are 3 types of translator:
Assembler
Compiler
Interpreter
Assembler – converts assembly language to machine code
Compiler – converts HLL source code into machine code in one operation and stores it in an exe file
Interpreter – converts HLL source code into machine code one line at a time and runs it on the CPU.
More detail on Translators:
Compiler: Will return a list of errors
Can take a long time to compile the program into one file before converting it
Interpreter: Needed every time you want to run the program
Program would stop every time it finds the 1st error
Programs run slowly since the code is being translated one line at a time.
IDE(Integrated Development Environment):
This provides programmers with handy tools to help when programming.
An example would be pyscripter
Features:
Line numbering
Auto colour coding eg print, input, variables
Auto correct of variables or commands , Auto indents
Tools :
GUI – easier navigation
Code editor – shows source code
Run-time environment - Runs the code without compiling – shows the outputs
Debugger – Tests the code for errors
Translator – compiler or interpreter to run the source code
Logic Gates(Binary Logic):
The 3 main logic operations a CPU can perform are:
a) The AND operation ‘Logic Gates’
b) The OR operation
c) The NOT operation
These can be combined to perform complex calculations in the CPU.
Logic gates are created from a number of transistor switches – very small
Truth tables: AND gate
Each gate produces an output from different inputs. A truth table can show these combinations of results.
"If both A and B are True then the output is also True, otherwise it is False”.
a) The AND operation Boolean expression:
(The shorthand for this) is Q = A AND B
Truth tables: OR gate
This gate needs 2 inputs to produce 1 output -"If either or both A, B are True then the output is also True”.
a) The OR operation Boolean expression:
(The shorthand for this) is Q = A OR B
Truth tables: NOT gate
This operation produces an output that is opposite to the input.
If the input is 1 (Logic True) then the output is 0 (Logic False).
If the input is 0 (Logic False) then the output is 1 (Logic True)
a) The NOT operation Boolean expression:
(The shorthand for this) is Q = NOT A
More complex Logic circuits
In this example we have an AND gate with inputs A and B where the result Q is then taken as one of the inputs to an OR gate
Boolean expression: P = (A AND B) OR C
Units of data
Binary – computers use 1’s(on) and 0’s(off) to represent flow of electricity.
Sizes: Bit(b) single binary digit 1 or 0 -
Nibble 4 bits - 2⁴ = 16 different values
Byte(B) 8 bits - 2⁸ = 256 different values
Kilobyte(kB) 1000 bytes
Megabyte(MB) 1000 kB
Gigabyte(GB) 1000 MB
Terabyte(TB) 1000 GB
Petabyte(PB) 1000 TB
Binary representationDenary to Binary - egs of 4bit numbers
0 = 0 4= 100 8 =1000 12 =11001 = 1 5 = 101 9 =1001 13=11012 = 10 6 =110 10=1010 14 =11103 = 11 7=111 11=1011 15=1111
8 bit number could be: 0011 0101
128 64 32 16 8 4 2 1
0 0 1 1 0 1 0 1
32 + 16 + 4 + 1 = 53
Adding Binary using column addition – (right to left)
1 0 0 0 1 1 0 1
+ 0 1 0 0 1 0 0 0
______________________________________1 1 0 1 0 1 0 1
Rules are: 0 + 0 = 0, 1 + 0 = 1, 0 + 1 = 1
and 1 + 1 = 1 0 so it is 0 carry 1 to next column on left
If you end up with a value greater than 8 bit eg 9 bit you get an overflow error – can cause software to crash.
Binary shifts – aka Logical Shift
If you are asked to do a binary or logical shift it will either be:
Left or Right
Each shift to the left multiplies the value by 2
Each shift to the right divides the value by 2
example a logical shift of 2 places to the left would:
Increase the number by 2² = 4 times
Hexadecimal numbersHexadecimal – uses a combination of numbers and letters to represent a number. Base 16
The numbers 1-15 become 1-9 followed by A - F
Reasons why:• Simpler to remember large numbers in hex
• Less chances of input errors
• Easier to convert between binary and hex than with denary.
Hex to Binary:
0 = 0 4= 100 8 =1000 C =11001 = 1 5 = 101 9 =1001 D=11012 = 10 6 =110 A=1010 E =11103 = 11 7=111 B=1011 F=1111
Hexadecimal numbers to denaryHex values:
Hex increases by a power of 16 eg: 4096 256 16 1
So Hex number 87 to denary is:8 7(8x16) (7x1)= 128 + 7 = 135
Hex number 6A to denary is:6 A(6x16) (10x1)= 96 + 10= 106
Binary to Hex
Convert 11001001 to hexadecimal
Split the number into 4 bit numbers – nibbles 1100 1001
so 8 4 2 1 8 4 2 1
1 1 0 0 1 0 0 1
= 12 = C =9
so answer is C9 in hexadecimal
Hex to Binary
Convert 6B to binary
Find the number in denary 6 = 6 and B =11
so 8 4 2 1 8 4 2 1
0 1 1 0 1 0 1 1
check =6 check =11
so answer is 0 1 1 0 1 0 1 1 in binary
Error Checking
There are 2 types of errors –
The compiler or interpreter will not run the program if it finds one of these errors
Syntax –
an error due to a grammar mistake or doesn’t follow (python) command language
Logical –
an error which stops the program running properly – inaccurate formulae or inequality
Error Checking
Check digits:
These are used to make sure that data has been entered and read correctly.
They are digits at the end of numbers eg ISBNs on books.
Eg ISBN 978 1 78294 602 1
The check digit is 1
ASCII
What is meant by a character set?All of the characters recognised/represented by a computer system. Every character on the keyboard is represented by a binary value
What does ASCII stand for?American Standard Code for Information Interchange
What is ASCII used for?Developed for representing the English alphabet. Characters include 0-9, upper case, lowercase a-z, punctuation and space.
How many characters can ASCII represent?128 characters – standard ASCII256 characters – Using 8 bits – extended ASCII
UNICODE
What is a problem with ASCII?
It doesn’t have enough characters to deal with other languages and extra symbols like accents .
A different character set was invented called UNICODE
This uses 16 and even 32 bit character sets so it can cover languages that have completely different letters to english.
ImagesStored as pixels. Colours relate to number of bits.
Black and white – 2 colours – 1 bit – 0 –white1- black
2 bit images have 4 colours 00, 01, 10, 11
4 bit = 16 colours (2⁴)
More bits - greater colour range or depth
Most devices have 24 bit, 8bits for red, green ,blue
Resolution – number of pixels per inch – more pixels greater quality of images.Larger file size
Metadata – information stored about the image – file format, height, width, colour depth, resolution. Also date and time information
Without this information the image cannot be displayed.
SoundSound is recorded by a microphone in Analogue format (constantly changing value)
Needs to be changed to Digital for a computer to use(made up of bits)
An analogue to digital converter is used to record sound – this is called sampling! See page 74 of
revision book
Sample intervals – the gaps between each of the points where the analogue recording is sampled eg
5milliseconds.
Sampling frequency – How many samples you take in a second eg 44KHz – 44, 000 samples a second
Sample size – number of bits available for each sample.
Bit rate = sampling frequency x sample size
Increase the frequency – better quality sound –will pick up the quieter sounds even if they happen
at the same time as the louder ones.
Obviously this increases the bit rate, number of bits stored and makes the file size large.
Types of compression Data compression is when we make files smaller to save space but try not to lose too much quality with the new compressed file.
Reasons: 1.Less storage, 2. streaming and downloading is quicker, 3. web pages load quicker4. email attachments can be sent - limit on file sizes.
2 types of compression:
Lossy – permanently removes data so file is smaller – quality will be less eg mp3, jpeg. The difference in quality is only slight though.
Lossless – temporarily removes data while compressed - eg zip file. When extracted can be turned back to original eg png image.