Key+Points+Paper+2 +CS+Revision
Key+Points+Paper+2 +CS+Revision
ORGANISERS
COMP 2 REVISION: COMPUTATIONAL THINKING, ALGORITHMS &
PROGRAMMING
1
Revise on the following PLC’s for Paper 2
2.1 Algorithms 1 Computational thinking: Decomposition/Abstraction/Computational Thinking
2.1 Algorithms 2 Standard searching algorithms: Binary and Linear
2.1 Algorithms 3 Standard sorting algorithms: Merge /Bubble/
2.1 Algorithms 4 How to produce algorithms using: Pseudocode/Flowcharts/Trace table
2.1 Algorithms 5 Interpret, correct or complete algorithms
2.2 Programming techniques 6 The use of variables, constants, operators, inputs, outputs and assignments
2.2 Programming techniques 7 The use of the three basic programming constructs: Sequence/Selection/Iteration
2.2 Programming techniques 8 The use of the three basic programming constructs: Iteration (count and condition controlled loops)
2.2 Programming techniques 9 The use of basic file handling operations: (Open, Read, Write, Close) / Arrays
2.2 Programming techniques 10 The use of SQL to search for data
2.2 Programming techniques 11 How to use sub programs (functions and procedures) to produce structured code
2.2 Programming techniques 12 The use of data types: Integer, Real, Boolean, Character & String, Casting
2.4 Computational logic 13 Simple logic diagrams using the operations AND, OR and NOT
2.4 Computational logic 14 Truth tables
Characteristics and purpose of different levels of programming language, including low level
15
2.5 Translators and facilities of languages languages/high level languages
Common tools and facilities available in an integrated development environment (IDE): Editors, Error
16
2.5 Translators and facilities of languages diagnostics, Run-time environment, Translators
2.5 Translators and facilities of languages 17 Translators : Compiler and Interpreter
3
2.1 – Algorithms - Computational Thinking KEY TERMS
What is Computational thinking?
Algorithm: Steps to provide a solution to a
problem, usually represented in flowcharts
3
Flowcharts
or pseudocode
The thought processes involved in Displays an algorithm in diagram form using
symbols and arrows to show to flow of
formulating a problem and its Decompose: Breaking down a large
problem into smaller sub-problems information
solution(s), so that a computer,
human or machine can effectively Abstraction: Representing 'real world' Pseudocode
carry out problems in a computer using variables and A structured use of English used to define the
symbols and removing unnecessary steps needed to solve a problem.
1 elements from the problem
computationally?
Structure diagram – a hierarchical diagram temp = “” 3
that shows how a problem is broken down
into sub-sections/sub-tasks IF temp > than 20c THEN
To effectively solve problems you need Open windows AND heaters OFF
to…. Sequence: Completing steps in the order ELSE
which they must happen Close windows AND
heaters ON
• Decompose
• Selection: Where a choice is made in a
Abstract TIME = “”
program depending on a condition or
• Algorithmic thinking IF TIME = 18.00 THEN
outcome
• Create algorithms Sprinklers on
INPUT/OUTPUT
PROCESS
7
2.1 – Sorting Algorithms – Bubble, Insertion & merge
8
9
11
Answer
12
2.2 Programming Techniques
Syntax Error
Constant An error in the rules/grammar of the language Eg missing colon / spelling mistake
Value STORED IN A MEMORY LOCATION that
never changes WITHIN A PROGRAM Logic Error
The program is written to do something other than what the programmer intended
Variable Eg Resetting only the first 9 elements in an array instead of all 10.
Value STORED IN MEMORY LOCATION that
Run Time Error:
can change WITHIN IN A PROGRAM More difficult to spot as it can run a program without reporting an error. E.g. runs but
Doesn’t give an output. Or the program hangs or Becomes inactive
Selection
• An IF statement is a type of selection statement
• The next statement to be executed depends on
whether the condition being tested is TRUE or FALSE
hoursPerNight = int(input("How many hours a night
do
you sleep?"))
if hoursPerNight < 8 then
Execute this if TRUE
print("That’s not enough!")
else Execute this if FALSE
print("That’s plenty!")
endif
Developing algorithms using pseudocode
Unit 6 Algorithms
default:
Execute if no
print("Please select a valid choice") cases match
endswitch
Python operations
Command Name Example Output MOD: will return the remainder
for the division. (Some programming
+ Addition 4+5 9
languages will use the % symbol for
- Subtraction 8-5 3 MOD).
* Multiplication 4*5 20
/ Division 19/3 6.3333
% Remainder 19%3 1
** Exponent 2**4 16
30
31
Writing to a file
Write
file = open("test1.txt", "w") Move to a
inside the
file.
file.write("Welcome\n") new line.
Does a Procedure or a Function return a value ? Underline which you think is correct
34
Procedures and Functions
Operator Operator
procedure hello(name)
print(“Hello “ + ) fill in the missing code == *
Endprocedure
!= /
function doubler(number) What is this part of the function
return double * 2 called
End function < -
> MOD
>= DIV
month:= getRandBetween(1,12)
number = random(1,10)
Syntax
• The exact structure of a random number generator will
vary between programming languages.
Devise your own scoring system that will award players with
points based on their dice rolls.
Example
roll1 = random(1,6)
roll2 = random(1,6)
Maintainability : Updating code so that it is compatible Anticipating Misuse: Defensive program design will consider and anticipate
with current requirements misuse. Misuse may be in the form of a brute force attack on the program.
E.G Many programs and systems only allow a user to enter a password three
Comments: To help describe the code and structures or four times before it locks out the system.
in a program using #
Contingency Planning: Once a programmer has anticipated the misuse they can
Indentation: used to show the programs structure e.g. then plan for the these issues. For example: Limiting the number of logon
where selection or iteration or functions are used . attempts. Ensuring the code is robust in validating the data entered
51
2.3 Robust Programs Test data – When testing your programs it is important to use of
data to test and to create a test plan.
Iterative testing: This happens overtime and is repeated
throughout the development of the program
52
53
2.4 Computational logic Truth Tables: Display all possible
outcomes for that gate
Logic Gate: A building block of a digital circuit. They
perform logical functions in a circuit and use binary IN OUT
Not Gate 0 1
1 0
Inverts the input (0 becomes 1 and 1
becomes 0
A B Out
0 0 0
OR Gate
0 1 1
Wait for either inputs to be 1 for output to 1 0 1
be 1 1 1 1
A OR B
A B Out
AND Gate 0 0 0
Both inputs to be 1 for output to be 1 0 1 0
A AND B 1 0 0
1 1 154
2.4 Computational logic TRANSISTOR: A tiny switch that is activated by the
electronic signals it receives. The digits 1 and 0
used in binary reflect the on and off states of this.
High level
Language
Low level
Language
Machine Code
Hardware
2.5 Translators and Facilities
Code Editor – edits program text, you can type our your source Executable Code: Files which contain
code here instructions in machine code. These
Syntax Checks – Highlights syntax errors instructions are carried out in the computer
hardware
Runtime Environment – allows programs to be run one line at a
time – helps test programs and locate errors. Programming Standards
Code should follow agreed conventions (EG Lowercase for
variable names, schemes to be followed).
Translator – Compiles or interprets the code
Language code is written in.
Libraries – Provides functions not included in core part of Functions used to tidy up repeated code.
programming languages (eg Random) Comments explain the code clearly.
Correct use of indentation.
Debugger – Helps to detect errors. Suggests what type of error it Useful identifiers (File names & Variable names)
is and what line it is on.
Code should follow agreed conventions
60
Compilers
• Compilers
• Translate entire source code all in one go into Machine Code
• Optimise code
• Used at the end of development (ready for shipping)
• Error Reports created along with Object Code
Interpreters
• Interpreters
• Translate and execute source code
• Line by Line, Statement by Statement
• Source code is checked for syntax – if correct, code is executed. If incorrect
interpreting is stopped.
• Used for development (aide debugging)
Integrated Development Environment (IDE)
• Integrated Development Environment (IDE)
1. Editor (for writing the code such as auto-correct, auto-indent)
2. Error Diagnostics (such as de-bug facilities to detect errors.)
3. Run-Time Environment (you can execute the program one step at a time)
4. Translators (This compiles or interprets the code).
5. Break point (Common debugging tool they stop the program on certain lines.
6. Code completion (assist programmers by intelligently identifying and inserting
common code components).
Advantage and disadvantage of Binary Search
Advantage and disadvantage of Linear Search
Advantage and disadvantage of Bubble Sort
Advantage and disadvantage of Insertion Sort
Advantage and disadvantage of Merge Sort