0% found this document useful (0 votes)
2 views

A_Level algorithms pseudocode

The document provides comprehensive notes on algorithm design and problem-solving in computer science, emphasizing computational thinking, decomposition, pattern recognition, abstraction, data modeling, and algorithm design. It covers various programming concepts including variables, constants, logic statements, loops, and nested structures, along with examples of pseudocode and flowcharts. Additionally, it discusses the implementation of algorithms through modules, procedures, and functions.

Uploaded by

Vimbai Haisa
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

A_Level algorithms pseudocode

The document provides comprehensive notes on algorithm design and problem-solving in computer science, emphasizing computational thinking, decomposition, pattern recognition, abstraction, data modeling, and algorithm design. It covers various programming concepts including variables, constants, logic statements, loops, and nested structures, along with examples of pseudocode and flowcharts. Additionally, it discusses the implementation of algorithms through modules, procedures, and functions.

Uploaded by

Vimbai Haisa
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

Computer Science 9618 Notes Algorithm Design and Problem-Solving Subject Teacher: Fahim Siddiq 03336581412

What is computational thinking?


Computational thinking is a problem-solving process where a number of steps are taken in order
to reach a solution.
Computational thinking involves abstraction, decomposition, data modelling, pattern recognition
and algorithm design.

Decomposition
Decomposition means breaking tasks down into smaller parts in order to explain a process more
clearly. Decomposition is another word for step-wise refinement. This led us to structured
programming using procedures and functions with parameters.

Pattern recognition
Pattern recognition means looking for patterns or common solutions to common problems and
exploiting these to complete tasks in a more efficient and effective way.

Abstraction
Abstraction involves filtering out information that is not necessary to solving the problem.
Abstraction gives us the power to deal with complexity. An algorithm is an abstraction of a
process that takes inputs, execute-s a sequence of steps, and produces outputs. An abstract data
type defines an abstract set of values and operations for manipulating those values.

Data modelling
Data modelling involves analyzing and organizing data. Simple data types such as integer,
character and Boolean. The string data type is a composite type: a sequence of characters. When
we have groups of data items, we used one-dimensional (lD) arrays to represent linear lists and
two-dimensional (2D) arrays to represent tables or matrices. We stored data in text files. In
Chapter 10, we used data modelling to design database tables. We can set up abstract data types
to model real-world concepts, such as records, queues or stacks. When a programming language
does not have such data types built-in, we can define our own by building them from existing
data.

Algorithm design
Algorithm design involves developing step-by-step instructions to solve a problem.
Computer Science 9618 Notes Algorithm Design and Problem-Solving Subject Teacher: Fahim Siddiq 03336581412

Expressing Algorithms
Algorithm can be written in three ways.

Structured English Pseudocode Flowchart


A subset of the English A way of using keywords and Shapes linked together to
language that consists of identifiers to describe an represent the sequential
command statements used to algorithm without following steps of an algorithm.
describe an algorithm. the syntax of a particular
programming language.
Computer Science 9618 Notes Algorithm Design and Problem-Solving Subject Teacher: Fahim Siddiq 03336581412

Variable
A storage location in a memory for a data value that has an identifier name. Variable identifiers
should not contain spaces, only letters, digits and _ (the underscore symbol).

1. Assigning a value
Number ← 1

2. Updating a value
Number ← Number + 1
Number ← Number – 1

3. Copying a value
Number2 ← Number1

4. Swapping two values


Temp ← Number1
Number1 ← Number2
Number2 ← Temp

Constant
Constants are like variables in their declarations and the ability to look at the value stored inside
them; however, you cannot change the values while the program is running, the value of a
constant remains (rather unsurprisingly) constant.

Example
Convert a distance in miles and output the equivalent distance in km (1km = 1.6 miles).

Step 1: Write the problem as a series of structured English statements:


INPUT number of miles
Calculate number of km
OUTPUT calculated result as km
Computer Science 9618 Notes Algorithm Design and Problem-Solving Subject Teacher: Fahim Siddiq 03336581412

Step 2: Analyze the data values that are needed.


We need a variable to store the original distance in miles and a variable to store the result
of multiplying the number of miles by 1.61. It is helpful to construct an identifier table to list the
variables.

OUTPUT “ Enter the miles”


INPUT miles
Km ← miles * 1.6
OUTPUT “The equivalent miles are”,Km

Step 3: Provide more detail by drawing a flowchart or writing pseudocode.

Logic Statements
A number-guessing game follows different steps depending on certain conditions. Here is a
description of the algorithm.
• The player inputs a number to guess the secret number stored.
• If the guess was correct, output a congratulations message.
• If the number input was larger than the secret number, output message “secret number is
smaller”.
• If the number input was smaller than the secret number, output message “secret number
is greater”.
We can re-write the number-guessing game steps as an algorithm in pseudocode:
Computer Science 9618 Notes Algorithm Design and Problem-Solving Subject Teacher: Fahim Siddiq 03336581412

SET value for secret number


INPUT Guess
IF Guess = SecretNumber
THEN
OUTPUT "Well done. You have guessed the secret number"
ELSE IF Guess > SecretNumber
THEN
OUTPUT "secret number is smaller"
ELSE
OUTPUT "secret number is greater"
ENDIF
ENDIF

More complex conditions can be formed by using the logical operators AND, OR and NOT. For
example, the number-guessing game might allow the player multiple guesses; if the player has
not guessed the secret number after 10 guesses, a different message is output.

IF Guess = SecretNumber
THEN
OUTPUT "Well done. You have guessed the secret number"
ELSE IF Guess > SecretNumber AND NumberofGuesses = 10
THEN
OUTPUT "You still have not guessed the secret number"
ELSE IF Guess < SecretNumber
THEN
OUTPUT "secret number is smaller"
ELSE
OUTPUT "secret number is greater"
ENDIF
ENDIF
ENDIF

Nested IF Statement
Conditional statements within conditional statements.

IF name = "Queen" And age >= 18 Then


Print "Hello your Majesty, may one serve you drink?"
End If
IF name = "Queen" And age < 18 Then
Print "I'm sorry your Majesty, you are too young to buy a drink"
End If
IF name <> "Queen" And age >= 18 Then '<> means not equal (so does !=)
Computer Science 9618 Notes Algorithm Design and Problem-Solving Subject Teacher: Fahim Siddiq 03336581412

Print "Hello mate, can I serve you drink?"


End If
IF name <> "Queen" And age < 18 Then
Print “Get out of my pub, you are too young to buy drink"
End If

This seems awfully cumbersome and we will now look a more elegant way of solving this, using
Nested IF's. First of all, nested means placing one thing inside another, so we are going to place
an IF inside another.

IF name = "Queen" Then


IF age < 18 Then
Print "I'm sorry your Majesty, you are too young to buy drink"
Else
Print "Hello your Majesty, may one serve you drink?"
End IF
Else
IF age >= 18 Then
Print "Hello mate, can I serve you drink?"
Else
Print "Get out of my pub, you are too young to buy drink"
End IF
End IF

Case Statement
Create a program where someone types in the name of an animal and it outputs the sound the
animal makes. The animals it should handle are:

• Pig - Oink
• Cow - Moo
• Bear - Grr
• Sheep - Baa
• Tiger - Grr
• everything else - Meow
Try and complete this task by only using 5 case statements.
Computer Science 9618 Notes Algorithm Design and Problem-Solving Subject Teacher: Fahim Siddiq 03336581412

Declare animal: String


Input animal
Case animal Of
"Pig”: Print "Oink"
"Cow": Print "Moo"
"Bear", "Tiger": Print "Grr"
"Sheep": Print "Baa"
Otherwise: Print "Meow"
End Case

Loops/Iterations

Repetition using REPEAT...UNTIL


The problem to be solved: Take 10 numbers as input and output the largest number.

BiggestSoFar ← -100
Counter ← 1
REPEAT
INPUT NextNumber
Counter ← Counter + 1
IF NextNumber > BiggestSoFar
THEN
BiggestSoFar ← NextNumber
ENDIF
UNTIL Counter = 10
OUTPUT BiggestSoFar

Repetition using FOR...NEXT


The problem to be solved: Take 10 numbers as input and output the largest number.
Computer Science 9618 Notes Algorithm Design and Problem-Solving Subject Teacher: Fahim Siddiq 03336581412

BiggestSoFar ← -100
FOR Counter ← 1 TO 10
INPUT NextNumber
IF NextNumber > BiggestSoFar
THEN
BiggestSoFar ← NextNumber
ENDIF
NEXT Counter
OUTPUT BiggestSoFar

Repetition using While…Do...END While

BiggestSoFar ← -100
Counter ← 1
While Counter <> 10
Do
INPUT NextNumber
Counter ← Counter + 1
IF NextNumber > BiggestSoFar
THEN
BiggestSoFar ← NextNumber
ENDIF
END While
OUTPUT BiggestSoFar

Rogue value
A value used to terminate a sequence of values.
Computer Science 9618 Notes Algorithm Design and Problem-Solving Subject Teacher: Fahim Siddiq 03336581412

Repetition using a rogue value


The problem to be solved: A sequence of non-zero numbers is terminated by 0. Take this
sequence as input and output the largest number.

BiggestSoFar ← -100
REPEAT
INPUT NextNumber
IF NextNumber > BiggestSoFar
THEN
BiggestSoFar ← NextNumber
ENDIF
UNTIL NextNumber = 0
OUTPUT BiggestSoFar

This algorithm works even if the sequence consists of only one non-zero input. However, it will
not work if the only input is 0. In that case, we don’t want to perform the statements within the
loop at all. We can use an alternative construct, the WHILE...ENDWHILE loop.

INPUT NextNumber
BiggestSoFar ← NextNumber
WHILE NextNumber <> 0 DO // sequence terminator not encountered
INPUT NextNumber
IF NextNumber > BiggestSoFar
THEN
BiggestSoFar ← NextNumber
ENDIF
ENDWHILE
OUTPUT BiggestSoFar

Implementing the number-guessing game with a loop


Consider the number-guessing game again, this time allowing repeated guesses.
1. The player repeatedly inputs a number to guess the secret number stored.
2. If the guess is correct, the number of guesses made is output and the game stops.
3. If the number input is larger than the secret number, the player is given the message
to input a smaller number.
4. If the number input is smaller than the secret number, the player is given the message to
input a larger number.
Computer Science 9618 Notes Algorithm Design and Problem-Solving Subject Teacher: Fahim Siddiq 03336581412

Pseudocode for the number-guessing game with a post-condition loop

SecretNumber ← Random()
NumberOfGuesses ← 0
REPEAT
INPUT Guess
NumberOfGuesses ← NumberOfGuesses + 1
IF Guess > SecretNumber
THEN
// the player is given the message to input a smaller number
ENDIF
IF Guess < SecretNumber
THEN
// the player is given the message to input a larger number
ENDIF
UNTIL Guess = SecretNumber
OUTPUT NumberOfGuesses

Pseudocode for the number-guessing game with a pre-condition loop

SecretNumber ← Random()
INPUT Guess
NumberOfGuesses ← 1
WHILE Guess <> SecretNumber DO
IF Guess > SecretNumber
THEN
// the player is given the message to input a smaller number
ENDIF
IF Guess < SecretNumber
THEN
// the player is given the message to input a larger number
ENDIF
INPUT Guess
NumberOfGuesses ← NumberOfGuesses + 1
ENDWHILE
OUTPUT NumberOfGuesses
Computer Science 9618 Notes Algorithm Design and Problem-Solving Subject Teacher: Fahim Siddiq 03336581412

Calculating running totals and averages


The problem to be solved: Take 10 numbers as input and output the sum of these numbers and
the average.

RunningTotal ← 0
FOR Counter ← 1 TO 10
INPUT NextNumber
RunningTotal ← RunningTotal + NextNumber
NEXT Counter
OUTPUT RunningTotal
Average ← RunningTotal / 10
OUTPUT Average

Nested loop: loop containing another loop.

The problem to be solved: Take as input two numbers and a symbol. Output a grid made up
entirely of the chosen symbol, with the number of rows matching the first number input and the
number of columns matching the second number input. For example, the three input values 3, 7
and &, result in the output:
&&&&&&&
&&&&&&&
&&&&&&&

INPUT NumberOfRows
INPUT NumberOfColumns
INPUT Symbol
FOR RowCounter ← 1 TO NumberOfRows
FOR ColumnCounter ← 1 TO NumberOfColumns
OUTPUT Symbol // without moving to next line
NEXT ColumnCounter
OUTPUT Newline // move to the next line
NEXT RowCounter

Stepwise Refinement: Breaking down the steps of an outline solution into smaller and
smaller steps.
Computer Science 9618 Notes Algorithm Design and Problem-Solving Subject Teacher: Fahim Siddiq 03336581412

Modules: Another method of developing a solution is to decompose the problem into sub-
tasks. Each sub-task can be considered as a ‘module’ that is refined separately. Modules are
procedures and functions.

Procedure: A sequence of steps that is given an identifier and can be called to perform a sub-
task.

Function: a sequence of steps that is given an identifier and returns a single value; function call
is part of an expression.

Local variable: A variable that is accessible only within the module in which it is declared.
Global variable: A variable that is accessible from all modules.

You might also like