GCSE-Computer-Science-Theory

Notes for the AQA GCSE Computer Science (4512/1) theory examination Content based on BourneToCode

Algorithms

What is an Algorithm?

Algorithms are computational solutions that always finish and return an answer.

For example, if we had a bag filled with scrabble tiles, and we wanted to find out how many letters of each type there were:

Set count for each letter to 0
Until the bag is empty
    Take out a tile
    Look at the letter
    Add one to that letter's count

Sorting Algorithms

Bubble Sort Algorithm

REPEAT
  swapped <- false
  FOR i <- 1 TO indexOfLastUnsortedElement
    IF leftElement > rightElement
      swap(leftElement, rightElement)
      swapped <- true
    ENDIF
UNTIL swapped IS false

Select Sort Algorithm

WHILE (numOfUnsortedElements - 1) != 0
  set the first unsorted element as the minimum
  FOR element IN unsorted elements
    if element < currentMinimum
      element <- currentMinimum
  swap minimum with first unsorted position
ENDWHILE

Insert Sort Algorithm

mark first element as sorted
FOR element in unsorted elements
  'extract' the element
  FOR i = lastSortedIndex TO 0
    IF currentSortedElement > extractedElement THEN
      move sorted element to the right by 1
    ELSE
     insert extracted element
    ENDIF
  ENDFOR
ENDFOR

Examples

Example 1

Write an algorithm using pseudocode or a flowchart that will calculate the amount of fuel a plane needs to make a complete journey. The algorithm must:

Solution

distance <- 0
REPEAT
  distance <- INPUT "How long will the journey be?"
UNTIL distance > 0
litres <- distance * 20
litres <- litres * 1.2
OUTPUT litres 

Example 2

Write an algorithm using pseudocode or a flowchart that will calculate if a person has a healthy weight. The algorithm must:

Solutions

user_mass <- USERINPUT
user_height <- USERINPUT

WHILE user_mass <= 0 AND user_height <= 0
  OUTPUT "height and mass must be >0"
  user_mass <- USERINPUT
  user_height <- USERINPUT
ENDWHILE

bmi <- user_mass / (user_height ^ 2)

IF bmi < 25 AND bmi > 18.5 THEN
  OUTPUT "healthy weight"
ELSEIF bmi <= 18.5 THEN
  OUTPUT "underweight"
ELSE
  OUTPUT "overweight"
ENDIF

Example 3

Write an algorithm using pseudocode or a flowchart that will calculate the resistance of a resistor. The algorithm must:

Solutions

colours <- ["black", "brown", "red", "orange", "yellow", "green", "blue", "violet", "grey", "white"]

REPEAT
  band_1 <- USERINPUT
  band_2 <- USERINPUT
  band_3 <- USERINPUT
UNTIL band_1 IN colours AND band_1 IN colours AND band_1 IN colours

counter = 1
resistance = 0
WHILE counter <= 3
  IF counter = 1 THEN
    FOR band_count <- 0 to 9
      IF colours[band_count] = band_1 THEN
        resistance = resistance + band_count
        BREAK
  ELSEIF counter = 2 THEN
    FOR band_count <- 0 to 9
      IF colours[band_count] = band_2 THEN
        resistance = resistance + (band_count * 10)
        BREAK
  ELSE
    FOR band_count <- 0 to 9
      IF colours[band_count] = band_3 THEN
        resistance = resistance * (10 ^ band_count)
        BREAK
  ENDIF
ENDWHILE

OUTPUT resistance

Example 4

Write an algorithm using pseudocode or a flowchart that will correctly identify a hydrocarbon:

Carbons Prefix
1 Meth
2 Eth
3 Prop
4 But
5 Pent
6 Hex
7 Hept
8 Oct
Ratio Suffix
CnH2n+2 ane
CnH2n ene
CnH2n-2 yne

Solution

prefixes <- ["Meth", "Eth", "Prop", "But", "Pent", "Hex", "Hept", "Oct"]
REPEAT
  carbons <- INPUT "How many carbon atoms do you have?"
UNTIL 1 <= carbons <= 8
hydrogens <- INPUT "How many hydrogen atoms do you have?"
CASE hydrogens OF
  carbons * 2 + 2: name <- prefixes[carbons] + "ane"
  carbons * 2: name <- prefixes[carbons] + "ene"
  carbons * 2 - 2: name <- prefixes[carbons] + "yne"
ENDCASE

OUTPUT name