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

Algorithm Questions

There are two main methods for writing algorithms: flowcharts and pseudocode. Flowcharts use symbols and diagrams to visually represent the logic and steps of an algorithm. Pseudocode uses common programming keywords and syntax to describe an algorithm without a specific programming language. Both methods allow algorithms to be understood by programmers regardless of the language. Effective algorithms are independent of any language, succinct, accurate, and easy to understand.

Uploaded by

Chunky King
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
31 views

Algorithm Questions

There are two main methods for writing algorithms: flowcharts and pseudocode. Flowcharts use symbols and diagrams to visually represent the logic and steps of an algorithm. Pseudocode uses common programming keywords and syntax to describe an algorithm without a specific programming language. Both methods allow algorithms to be understood by programmers regardless of the language. Effective algorithms are independent of any language, succinct, accurate, and easy to understand.

Uploaded by

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

Writing Algorithms

What is an Algorithm?
An algorithm is simply a set of steps/instructions needed to complete a task. A recipe is an example of an
algorithm. For instance you could define an algorithm to make a cup of tea. You start by filling the kettle,
then place a tea bag in the cup and so on. When the word algorithm is used in computing, it defines the way
in which a software task can be completed or the way a problem can be solved.
Writing algorithms is all about problem solving. After being given a problem to solve, programmers never
begin programming straight away, they will first do the following:
 Think about the important parts of the problem (Abstraction),
 Break the problem down into smaller chunks (Decomposition),
 Think of the steps needed to solve each chunk (Algorithmic Thinking),
 Write down the steps – which in turn becomes the algorithm (Algorithmic Thinking).
Writing Algorithms
When writing algorithms, we could…
…write an algorithm in a specific language. However, doing this results in the following:
 Too time consuming
 Pointless – could just code
 Can’t be taken by a programmer using a different language
 Too complex – need knowledge of syntax (code specific to a language)
…write an algorithm in everyday language. However, doing this results in the following:
 Too time consuming
 Open to interpretation – thus resulting in different outcomes
As a result, effective Algorithms should be independent of any language…

There are therefore methods used to write


algorithms so that they are succinct, accurate and easy to understand so that a programmer of any language
could understand the steps required to solve a task. These methods are:

1
 Flowcharts
 Pseudocode
Examples of each method of algorithm writing:

Flowchart

Pseudocode

Flowcharts
Flowcharts provide a visual representation of the logic of a program. They make use of a limited range of
symbols to describe processes and arrows to show the order of instructions / program flow.
Flowchart Example:
A central heating system will try to keep the temperature between 2
values (19 and 21)
If the temperature falls below 19 it will turn the heating system on
If the temperature rises above 21 it will switch the heating system off.
Flowcharts and their symbols
Representation Explanation Symbol

1
Start and Stop (Terminal) All flow charts begin with a Start
Symbols Symbol and at the end of the flow
chart (or at various end points of
the chart) we place a Stop
Symbol.

There are drawn as a rectangle


with curved ends
Process Symbols Most of the time a flow chart will
demonstrate the sequence of
instructions to be carried out.

Simple processes (like “Add 1 to


x” or “append x to List”) are
shown using a standard rectangle.
Input / Output Symbols At times your program will most
certainly ask the user for inputs
and output values too.

Inputs and Outputs (like “Name?”


or “…display age”) are shown
using a parallelogram.
Decision Symbols At times your program will be
programmed to make a decision
based on certain conditions.

Decisions (like “IF X = 3” or


“WHILE Y > 3”) are shown
using a diamond.

4 things you need to be able to do with flowcharts for your exam


 Understand them
 Correct them if they have errors,
 Complete them if they are incomplete
 Produce them after being given a problem to solve

Tips on understanding / reading flowcharts:

 Start from top and follow the arrows


 Remember what the symbols mean
 Read each symbol description
 Logically work out what the algorithms is doing.

1
Tips on correcting flowcharts:
Read the flowchart and look for decisions which don’t make sense or iterations (loops) that you cannot enter
of break out of.

Tips on completing flowcharts:


 Start from top and follow the arrows
 Remember what the symbols mean
 Read each symbol description
 Logically work out what the algorithms is doing.
…then use your knowledge of the algorithm so far and of the flow chart symbols and complete the
flowchart.

Pseudocode
Algorithms can be written in ‘generic code’ (which doesn’t follow any particular syntax) as well as a
flowchart. This ‘generic code’ is known as pseudocode. Pseudocode has keywords such as IF, ELSE and
FOR and so mimics a programming language and therefore the logic is easy to follow and easy to turn into
code. Although there are no STRICT STANDARDS for pseudocode there are some generally accepted
keywords that you should be aware of.
Pseudocode Example:
 A central heating system will try to keep the temperature between 2
values (19 and 21)
 If the temperature falls below 19 it will turn the heating system on
 If the temperature rises above 21 it will switch the heating system off.

1
Pseudocode and commonly used keywords
Representation Explanation Symbol

Start and End “Key Words” Pseudocode begin with a START


and ends with END

The algorithm goes in between.

Process “Key Words” Most of the time pseudocode will


outline the logical sequence of
instructions to be carried out.

Simple processes will often use


the key words shown below (like
“CALCULATE X*2” or
“INCREMENT X by 1)”
You don’t have to always use
these words, for example the
logic statements such as “Add 1
to x” or “append x to List”) are
fine too.

Input / Output “Key Words” At times your program will most


certainly ask the user for inputs
and output values too.

Inputs and Outputs (like “Name?”


or “…display age”) are indicated
using the following words.
Usually a programmer will
choose one and stick with it
throughout their algorithm.

Variable Assignment “Key At times your program will


Words” assign values to variables.
In pseudocode, this is done using
the following key words.

1
Decision/Selection “Key Words” At times your program will be
programmed to make a decision
based on certain conditions.

Decisions (like “IF X = 3, THEN


…”) are shown using,
unsurprisingly, the following key
words.

Loops / Iterations “Key Words” Programs will often loop in


places while certain conditions
occur (condition controlled) or
for a set number of times (count
controlled).

Loops use the following key


words:

Pseudocode and their key words / statements in action…


Notice:
Use of capitalised pseudocode key words/statements
Indentation

4 things you need to be able to do with pseudocode for your exam


 Understand them
 Correct them if they have errors,
 Complete them if they are incomplete
 Produce them after being given a problem to solve

Tips on understanding / reading pseudocode:


 Start from top and read each line in turn
 Remember what the key words mean
 Logically work out what the algorithms is doing.

1
Tips on correcting pseudocode:
Read the pseudocode and look for decisions which don’t make sense or iterations (loops) that you cannot
enter of break out of.

Tips on completing pseudocode:


 Start from top and understand each line in turn
 Remember what the keywords mean
 Logically work out what the algorithms is doing
…then use your knowledge of the algorithm so far and of the pseudocode key words and complete the
algorithm.
Tips on writing algorithms
 Read the task to be solved
 Read it again
 Highlight the important information
 Begin at the START of the problem
 Think logically in terms of ‘1 step at a time’ and always think ‘IF this were to happen, what should
be the result?’

Questions (The question zone you choose must either match your target grade or be higher!)

Question Zone 1-3


1. Define the term ‘Algorithm’. [2]
A written set of instructions needed to complete a task.

2. Describe the difference between pseudocode and flowcharts. [2]


Flowcharts are visual representation for an algorithm
Pseudocode is Generic Code which algorithms can be written with.

3. Write an algorithm in pseudocode, which asks for the user to input 2 numbers and then outputs their
sum. [3]
Start
Input Number1
Input Number2

1
Then
Output Number1+Number2=Number3
Print Number3
End

Question Zone 4-6


1. Write an algorithm in pseudocode which asks for the user to input 2 numbers and then outputs their
sum. [3]
2. Create a flowchart which shows the logic required to ask the user to input a word, check its length
and output one of two messages depending on its length. If it is longer than 8 characters, “It’s a long
word” should be outputted, otherwise “It’s a short word” should be outputted. [6]

Is it 8
characters. Display “Long Word”

Display “Short
Word”

3. “Effective algorithms are independent of any language”. Explain the meaning of this statement [5]
A good algorithm can be written in any language the writer prefers.

Questions Zone 7-9


1. “Effective algorithms are independent of any language”. Explain the meaning of this statement [5]
2. Create a flowchart which shows the logic required to repeatedly ask the user to enter a password
until it matches the string “pa$$word”. [5]

1
_____________________________________________________
_____________________________________________________
_____________________________________________________
Does it match
String pa$$word? Display “Correct
_____________________________________________________
Password”

_____________________________________________________
_____________________________________________________
_____________________________________________________
_____________________________________________________
3. Discuss the two methods of writing algorithms and explain which you think is the most effective for
_____________________________________________________
an experienced software designer. [4]
_____________________________________________________
Pseudocode and Flowcharts are the 2 methods for writing an algorithm. In my opinion flowcharts are
_____________________________________________________
more effective for an experienced software designer since it’s much easier and a better representation
since it’s a visual one. It’s easy to make, not to complex and just easy to understand/work with it.
_____________________________________________________
_____________________________________________________
_____________________________________________________
_____________________________________________________
_____________________________________________________
_____________________________________________________
_____________________________________________________
_____________________________________________________
_____________________________________________________
_____________________________________________________
_____________________________________________________
_____________________________________________________
_____________________________________________________
_____________________________________________________
_____________________________________________________
_____________________________________________________ 1
_____________________________________________________
_____________________________________________________
_____________________________________________________
_____________________________________________________
_____________________________________________________
_____________________________________________________
_____________________________________________________
_____________________________________________________
_____________________________________________________
Checklist:
 Date and title, clearly presented
Keywords / Key Terms:
Algorithm: A set of steps/instructions needed to complete a software task.
_____________________________________________________
 Spelling & grammar checked
 Question numbers in the margin
Flowchart: A graphical representation of an algorithm.
Pseudocode: A representation of an algorithm in which the algorithm
_____________________________________________________
 Handwriting neat & legible
 Punctuation / Capital letters
resembles a simplified, generic programming language.

_____________________________________________________
_____________________________________________________
_____________________________________________________
_____________________________________________________
_____________________________________________________
_____________________________________________________
_____________________________________________________
_____________________________________________________
_____________________________________________________
_____________________________________________________
_____________________________________________________
_____________________________________________________
_____________________________________________________
_____________________________________________________
_____________________________________________________
_____________________________________________________ 1
State/Identify/Give/Name: Simply label a diagram, fill out a table or write a few words
Describe: Describing is ‘saying what you see’ (E.G.: A computer will have a CPU, Primary and Secondary storage etc)
Explain: Explaining is ‘saying WHY/HOW something is like that’. (E.G.: A computer will have a CPU so that it can process all of the data the
computer needs to perform a range of tasks. Primary and Secondary storage is needed because…)
Discuss: Discussing is ‘looking at two sides of an issue, weighing up the two views and giving a conclusion’. Often these require a mini essay
answer. (E.G.: New technology could be seen as being bad for the environment because…, but on the other hand, new technology has led to…
In conclusion I believe that…)
Describe/Explain/Discuss using examples: Finally, if you are asked to give examples in any of these types of questions – YOU MUST GIVE
EXAMPLES!

Stick answer sheet here

1
Reflections: Score: / Percentage: % Grade: Progress: On / Above / Below

What Went Well?:  My answers effectively incorporated technical terminology.


 I demonstrated a good level of understanding.  My responses were well structured / organised.
 I responded to the command words effectively.  My revision strategy was effective as I showed depth of understanding in my
answers.
 My answers were detailed / were written in depth.
 My answers contained enough points / examples / explanations to achieve the
 My work was well presented / legible.
marks available.

Even Better If…:  I must incorporate key terminology into my answers.


 I must better organise my answers to improve its clarity.
 My answers need to be more accurate.
 I need to improve my revision strategy, as I did not demonstrate a depth of
 I must respond correctly to the command words. understanding in my answers.
 My answers need more detail / greater depth.  My answers didn’t contain enough points / examples / explanations to
 I must take greater care over my work / write neatly. achieve the marks available.

Further thoughts:
__________________________________________________________________________
__________________________________________________________________________
__________________________________________________________________________
__________________________________________________________________________

You might also like