Computer Science Paper 2: Computational Thinking, Algorithms, and Programming, Study notes of Computer science

A comprehensive overview of computational thinking, algorithms, and programming fundamentals. It covers key concepts such as abstraction, algorithmic thinking, searching and sorting algorithms, and programming constructs like variables, constants, operators, and data types. The document also delves into defensive design considerations, testing techniques, and common errors in programming. It is a valuable resource for high school students learning about computer science.

Typology: Study notes

2024/2025

Uploaded on 02/27/2025

sana-42
sana-42 🇬🇧

2 documents

1 / 15

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Paper 2 – Computer Science
Content of Computational thinking, Algorithms and Programming.
2.1 Algorithms
2.1.1 computational thinking
Principles of computational thinking:
Abstraction
The process of removing unnecessary details and including only the
relevant details.
It is a method of computational thinking that focuses on what is important
in problem solving.
For example, when you save a file:
- where is it stored on the hard disk:
- how is the data actually being represented?
The level of detail is abstracted for the user.
They only need to be able to create, open, move, save and delete files.
They don’t need to be concerned with where or how this all happens.
Interface design:
- what is important when designing the user interface of a sat-nav device?
- what is important to include on the output map displayed?
- what is less important?
Include: location of the car, the road layout and additional icons for
volume, time.
Remove: buildings, tress and countryside.
- icons are a great example of a use of an abstraction.
- they can be used to clearly suggest a function or process in a simple and
efficient manner.
Decomposition
Breaking a complex problem down into smaller more manageable parts.
Dealing with many stages of a problem at once is much more difficult
than breaking a problem down into several smaller problems and solving
each one at a time.
Decomposition on a daily life: you do many tasks on a daily basis:
- getting up in the morning
- brushing your teeth
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff

Partial preview of the text

Download Computer Science Paper 2: Computational Thinking, Algorithms, and Programming and more Study notes Computer science in PDF only on Docsity!

Paper 2 – Computer Science

Content of Computational thinking, Algorithms and Programming.

2.1 Algorithms

2.1.1 computational thinking

Principles of computational thinking:

Abstraction

 The process of removing unnecessary details and including only the

relevant details.

 It is a method of computational thinking that focuses on what is important

in problem solving.

 For example, when you save a file:

- where is it stored on the hard disk:

- how is the data actually being represented?

The level of detail is abstracted for the user.

They only need to be able to create, open, move, save and delete files.

They don’t need to be concerned with where or how this all happens.

 Interface design:

- what is important when designing the user interface of a sat-nav device?

- what is important to include on the output map displayed?

- what is less important?

Include: location of the car, the road layout and additional icons for

volume, time.

Remove: buildings, tress and countryside.

- icons are a great example of a use of an abstraction.

- they can be used to clearly suggest a function or process in a simple and

efficient manner.

Decomposition

 Breaking a complex problem down into smaller more manageable parts.

 Dealing with many stages of a problem at once is much more difficult

than breaking a problem down into several smaller problems and solving

each one at a time.

 Decomposition on a daily life: you do many tasks on a daily basis:

- getting up in the morning

- brushing your teeth

- getting to school

- doing your homework

These are all complex tasks with many steps involved, but you break

them down and execute them without even thinking about it.

 For example, even an everyday problem like crossing the road can be

broken down into sub problems.

Cross a road:

- stop before crossing

- look left and right

- is the road clear

- if not press the crossing button

- wait for lights to turn red

- crossroad when it is safe to do so

Advantages Disadvantages

- Makes problems easier to solve.

- Different people can work on

different parts of a problem at the

same time…reducing development

time.

- programme components developed

in one programme can easily be used

in other programmes.

None

Algorithmic thinking

 Is a way of getting to solution by identifying the individual steps needed.

 By creating a set of rules, an algorithm that us followed precisely leads to

an answer.

 For example, we all learn algorithms for doing multiplication in school.

 If you (or a computer) follow these rules precisely we can get the answer

to any multiplication problem.

 Algorithmic thinking allows solutions to be automated.

 For example, listing all of the films that are showing, then deleting all the

age restricted films and ones with poor ratings. Getting each family

member to vote for their favourite, then picking the file with the most

votes.

2.1.2 Designing, Creating and Refining Algorithms

Structure diagrams:

Create, interpret, correct, complete and refine algorithms using:

Pseudocode

Flowcharts

Reference language/high level performing language.

Identify common errors.

Types of errors:

 Syntax errors are errors which break the grammatical rules of the

programming language.

They stop it from being run/translated.

 Logic errors are errors which produce unexpected output.

On their own they won’t stop the program running.

Trace tables.

 A vital skill for understanding program flow and testing the accuracy of

an algorithm for logic is called “Tracing Execution”.

 Involves examining a printed extract of program code and running

thorough the program.

 Take each line at a time and write out in a trace table the current state of

each variable.

 Noting down any output the program produces.

 Each variable present in the program should have its own column in the

trace table.

 A new row should be added under any column if the state of a variable

changes.

 Trace tables are an excellent way to track down logic errors in a problem.

Flowchart symbols Arrows Arrows connect boxes and show the direction you should

follow. Some boxes might have multiple arrows coming in or going out of them. Rectangle General instructions, processes and calculations go in rectangular boxes Sub programs Parallelogra m Anything that’s put into or taken out of the algorithm goes in a parallelogram box Diamond Decisions, often a yes or no question are put in diamond boxes. The beginning and the end of the algorithm are put in boxes with rounded corners- they’re sometimes called terminals 2.1.3 Searching and Sorting Algorithms Standard Searching algorithms: Binary Search  A binary search looks for items in an ordered list.

  1. Find the middle item in the ordered list
  2. If this is the item you’re looking for, then stop the search – you’ve found it.
  3. If not, compare the item you’re looking for to the middle item. If it comes before the middle item, get rid of the second half of the list. If it comes after the middle item, get rid of the first half of the list.
  4. You’ll be left with a list that is half the size of the original size. Repeat steps 1) - 3) on this smaller list to get an even smaller one. Keep going until you find the item you’re looking for.  For example: Item we are searching for: “Rice Crispies” 0 1 2 3 4 5 6 7 Coco Pops Cornflakes Crunchy nut clusters Fruit n Fibre Rice Crispies Special K Sugar Puffs Weetabix Calculate Mid-Point: Mid-Point = (left + right) DIV 2 (0 + 7) = 7 7 DIV 2 = 3 which is Fruit N Fibre The item at our “mid-point” is not the item we are looking for. Rice Krispies is greater than Fruit n Fibre.

 A linear search is much simpler than a binary search but not as efficient. A linear search can be used on any type of list, it doesn’t have to be ordered. Due to it being inefficient, a linear search is often only used on small lists.  Once the list has been ordered, a binary search is much more efficient than a linear search. In general, a binary search takes fewer steps to find the item you’re looking for, which makes it more suitable for large lists of items. Standard Sorting Algorithms: Bubble Sort  A bubble sort compares pairs of items.  The bubble sort algorithm is used to sort an unordered list of items.

  1. Look at the first two items in the list
  2. If they’re in the right order, you don’t have to do anything. If they’re in the wrong order, swap them.
  3. Move on to the next pair of items (the 2nd^ and 3rd^ entries) and repeat step 2)
  4. Repeat step 3) until you get to the end of the list – this is called one pass. The last item will now be in the correct place, so don’t include it in the next pass.
  5. Repeat steps 1 to 4 until there are no swaps in a pass.  For example: use the bubble sort algorithm to write these numbers in a ascending order. First passs 66 21 38 15 89 49 Compare 66 and 21 - swap them 21 66 38 15 89 49 Compare 66 and 38 - swap them 21 38 66 15 89 49 Compare 66 and 15 - swap them 21 38 15 66 89 49 Compare 66 and 89 - no swap 21 38 15 66 89 49 Compare 89 and 49 - swap them 21 38 15 66 49 89 End of first pass
  • after the second pass the order of the numbers will be 21, 15, 38, 49, 66, 89.
  • after the third pass the order of the numbers will be 15, 21, 38, 49, 66, 89.
  • there are no swaps in the fourth pass, so the list has been sorted 15, 21, 38, 49, 66, 89. Advantages Disadvantages
    • it’s a simple algorithm that can be easily implemented on a computer.
    • it’s an efficient way to check if a list is already in order. For a list of n items you
      • it’s an inefficient way to sort a list – for a list of n items the worst-case scenario would involve you doing n(n-1) ÷ 2 comparisons.
      • Due to being inefficient, the bubble sort

only have to do one pass of n-1 comparisons to check if the list is ordered or not.

  • doesn’t use very much memory as all the sorting is done using the original list. algorithm does not cope well with a very large list of items Merge sort  A merge sort splits the list apart then merges it back together.  The merge sort algorithm is an example of a divide and conquer algorithm and takes advantage of two facts:
  • Small lists are easier to sort then large lists
  • It is easier to merge two ordered lists than two unordered lists.
  1. Split the list in half (the smaller lists are called sub-lists) – the second sub-list should start at the middle item
  2. Keep repeating step 1) on each sub-list until all the lists only contain one item.
  3. Merge pairs of sub-lists so that each sub-list has twice as many items. Each time you merge sub-lists, sort the items into the right order.
  4. Repeat step 3) until you’ve merged all the sub-lists together. For example: F P A L T D K H Insertion sort 2.2 Programming Fundamentals

The Common Arithmetic Operators

  • addition 7 + 2 9
  • subtraction 7 - 2 5
  • Multiplication 7 * 2 14 / Division 7 / 2 3. ^ Exponentiation 7 ^ 2 49 DIV Quotient 7 DIV 2 3 MOD Remainder 7 MOD 2 1  The Common Comparison Operators = = 7 = = 7 Equals to ! = 7! = 5 Not equals to < 5 < 7 Less than < = 5 < =5 Less than or equal to

7 > 5 Greater than = 7 > = 7 Greater than or equals to The common Boolean operators AND, OR and NOT 2.2.2 Data Types  Programming languages have Five Main Data Types Data Type Code Characteristics Examples Integer int A whole number, positive or negative. Only used for data that requires calculations.

Real (Float) real A number with a decimal part. Only used for data that requires calculations.

Boolean bool Can only take one of two values, usually TRUE or FALSE, often called a “FLAG”. Used to track if something has happened or not. True/False, Yes/NO, 1/ Character char A single letter, “A”, “k”, “5”, “-“

number, symbol String string Used to represent text, it is a collection of characters. Used for all data that is not calculated. “FsTmQ2”, “$money$”  For example: Give the appropriate data type for each of the categories in this registration form. Initial of first name: N Surname: Chapman Age (in whole years): 27 Height (in metres): 1. Male of Female: Female  Initial of first name should be stored as a character.  Surname should be stored as a string.  Age should be stored as an integer.  Height should be stored as a real data type.  Male of Female should be stored as Boolean.  Changing a data type from one type to another is known as casting.

  • Casting is necessary so that sub-problems receive data in a format they are expecting.
  • Casting allows numbers to be manipulated as strings.
  • Casting allows inputs that are always strings to become numbers X = str (x) casts the variable x to a string 2.2.3 Additional Programming Techniques The use of basic string manipulation:  String Manipulation is the act of manipulating, extracting or changing the characters in a string variable. The use of basic file handling operation:  Open:  Read:  Write:  Close:

-Programs that read and write to files need to handle many types of exceptions, including: The File/folder not being found. The disk being out of space. The data in the file being corrupt. The end of the file being reached.

  • Robust programs will handle all these situations by checking files and data before attempting to use them for further processing. Authentication:  Data used by systems should be secure.  This can be achieved with:
  • Username and password to access systems.
  • Recovering a password requiring clicking on a link within the email that is sent to the registered address.
  • Encryption of data files.  Online bots can submit data automatically to online forms  This can be protected against using software such as reCAPTCHA that verifies the user is a human.  Programmers should also be aware of the potential for SQL injection hacks and other methods used by hackers Input Validation:  Input Validation – checking if data meets certain criteria before passing it into the program E.g. checking than an email address contains an @ symbol and has a suitable ending.  Type Check: “Is the input in the correct data type?” e.g. Integer, Real, String  Range Check:  “Is the input within the correct range?” e.g. Between 1 and 10  Presence Check “Has mandatory / required data been entered” e.g. Reject blank inputs  Format Check “Is the input in the correct format?” e.g. dd/mm/yyyy, postcode  Length Check “Does the input have the correct (or min/max) number of characters?” e.g. Password, Car licence plate  By using input validation techniques a programmer can make their program:
  • More robust
  • More user friendly
  • Prevent further errors occurring later in the algorithm Maintainability  Use of sub-programs: -Can make it easier for other programmers to see how different parts of a program work, which can help them understand the overall program faster.  Naming conventions:
  • User procedures and / or functions to: Structure the code Eliminate duplicating code
  • Use descriptive variable names and explain their purpose with a comment when declared.  Indentation:
  • Can be used to separate different statements in a program.
  • This allows other programmers to see the flow of the programs more clearly and pick out the different features.  Commenting:
  • Explain what the key features of a program do
  • Well written and clear comments are fundamental for helping other programmers understand your programs.
  • Explain sections of code. Typically, selections, iterations and procedures.
  • Visually divide sections of a program.
  • Explain unusual approach’s that were necessary 2.3.2 Testing The purpose of testing: Four main reasons why a program should be tested include:  To ensure there are no errors (bugs) in the code.  To check that the program has an acceptable performance and usability.  To ensure that unauthorised access is prevented.  To check the program meets the requirements. Types of testing: Iterative Testing – the program is tested while it is being developed. Often a programmer will test a module, fix any errors they find and then test it again. They will repeat the process until the module is working correctly. Final (terminal) Testing – the program is tested at the end of the development process. The whole program is tested at the same time to check that it is working correctly. Identify Syntax and Logic errors: