algorithms and pseudocode

23
Algorithms and Pseudocode

Upload: hilary-lee

Post on 18-Jan-2018

279 views

Category:

Documents


3 download

DESCRIPTION

Activity 1 Quickly sketch a flow chart for the following problem. Create a program that takes in two numbers and outputs their sum.

TRANSCRIPT

Page 1: Algorithms and Pseudocode

Algorithms and Pseudocode

Page 2: Algorithms and Pseudocode

Activity 1Quickly sketch a flow chart for the following problem.

Create a program that takes in two numbers and outputs their sum.

Page 3: Algorithms and Pseudocode

Solution

Start

End

Number 1?

Store in variable “Num1”

Number 2?

Store in variable “Num2”

Answer = Num1+Num2

Display Answer

Page 4: Algorithms and Pseudocode

Algorithms and PseudocodeLearning Objectives:(a) understand algorithms (written in pseudocode or flow diagram), explain what they do, and correct or complete them(b) produce algorithms in pseudocode or flow diagrams to solve problems.

Hands up if you can explain the following:

What is an algorithm?

Page 5: Algorithms and Pseudocode

Algorithms and PseudocodeRemembering Algorithms

• In general, an 'algorithm' is the name given to a set of steps needed to complete a task.

• Think of an algorithm as a recipe.

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.

In computer terms, an algorithm describes the set of steps needed to carry out a software task or solve a problem

Learning Objectives:(a) understand algorithms (written in pseudocode or flow diagram), explain what they do, and correct or complete them(b) produce algorithms in pseudocode or flow diagrams to solve problems.

Page 6: Algorithms and Pseudocode

Algorithms and PseudocodeWriting AlgorithmsLearning

Objectives:(a) understand algorithms (written in pseudocode or flow diagram), explain what they do, and correct or complete them(b) produce algorithms in pseudocode or flow diagrams to solve problems.

Algorithms are

independent of any language

Problem

Algorithm

Page 7: Algorithms and Pseudocode

Algorithms and PseudocodeLearning Objectives:(a) understand algorithms (written in pseudocode or flow diagram), explain what they do, and correct or complete them(b) produce algorithms in pseudocode or flow diagrams to solve problems.

Hands up if you can explain the following:

What is an algorithm?

Page 8: Algorithms and Pseudocode

Algorithms and PseudocodeWriting AlgorithmsLearning

Objectives:(a) understand algorithms (written in pseudocode or flow diagram), explain what they do, and correct or complete them(b) produce algorithms in pseudocode or flow diagrams to solve problems.

Writing an algorithm in a specific language:

-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)

Writing an algorithm in everyday language (standard English):

-Too time consuming-Inaccurate as it could be open to interpretation

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:

Flowcharts and Pseudocode

Algorithms are independent of any

language

Page 9: Algorithms and Pseudocode

Algorithms and PseudocodeWriting AlgorithmsLearning

Objectives:(a) understand algorithms (written in pseudocode or flow diagram), explain what they do, and correct or complete them(b) produce algorithms in pseudocode or flow diagrams to solve problems.

1. Flowchart2. Pseudocode

Problem: Calculate the Sales Tax (VAT) of an item and then using it to work out the final price

Examples of each method of algorithm

writing

Page 10: Algorithms and Pseudocode

Algorithms and PseudocodePseudocodeLearning

Objectives:(a) understand algorithms (written in pseudocode or flow diagram), explain what they do, and correct or complete them(b) produce algorithms in pseudocode or flow diagrams to solve problems.

Today we will focus on 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.

STARTIF Temp < 19 THEN

Heating OnELSEIF Temp > 21 THEN

Heating OffEND

• Represents an algorithm using adapted everyday language and common keywords from programming languages

• Instructions are easy to read and understand, but look similar to statements found in programming languages and so can be more readily turned into code.

• Psedo-code is NOT a strict list of commands, there are no universal standards, however, using certain keywords (as shown on the next few slides) is an expectation – this way any programmer should be able to take your algorithm an understand it.

Page 11: Algorithms and Pseudocode

Algorithms and PseudocodeLearning Objectives:(a) understand algorithms (written in pseudocode or flow diagram), explain what they do, and correct or complete them(b) produce algorithms in pseudocode or flow diagrams to solve problems.

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

The algorithm goes in between.

START……………………………..……………………………..……………………………..

END

Pseudocode and their

statements

Page 12: Algorithms and Pseudocode

Algorithms and PseudocodeLearning Objectives:(a) understand algorithms (written in pseudocode or flow diagram), explain what they do, and correct or complete them(b) produce algorithms in pseudocode or flow diagrams to solve problems.

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..Compute

CalculateDetermine

Increment; ++ or +=Decrement; -- or -=

Pseudocode and their

statements

Page 13: Algorithms and Pseudocode

Algorithms and PseudocodeLearning Objectives:(a) understand algorithms (written in pseudocode or flow diagram), explain what they do, and correct or complete them(b) produce algorithms in pseudocode or flow diagrams to solve problems.

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 chose one and stick with it throughout their algorithm.

INPUTS:

READOBTAIN

GETINPUT

OUTPUTS:

PRINTDISPLAYSHOW

OUTPUT

Pseudocode and their

statements

Page 14: Algorithms and Pseudocode

Algorithms and PseudocodeLearning Objectives:(a) understand algorithms (written in pseudocode or flow diagram), explain what they do, and correct or complete them(b) produce algorithms in pseudocode or flow diagrams to solve problems.

Variable Assignment “Key Words”At times your program will assign values to variables.

In pseudocode this is done using the following key words. SET

INITPseudocode and their

statements

Very commonly, an arrow is used instead e.g.:

X 10This means SET X to 10

Page 15: Algorithms and Pseudocode

Algorithms and PseudocodeLearning Objectives:(a) understand algorithms (written in pseudocode or flow diagram), explain what they do, and correct or complete them(b) produce algorithms in pseudocode or flow diagrams to solve problems.

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.

IFTHENELSE

ELSE-IFENDIF

Pseudocode and their

statements

Page 16: Algorithms and Pseudocode

Algorithms and PseudocodeLearning Objectives:(a) understand algorithms (written in pseudocode or flow diagram), explain what they do, and correct or complete them(b) produce algorithms in pseudocode or flow diagrams to solve problems.

Loops / Iterations “Key Words”Programs will often loop in places while certain conditions occur (infinitely) or for a set number of times (finitely).

Loops use the following key words:

FINITE:

FORINFINITE:

WHILE / ENDWHILE

REPEAT / UNTIL

Pseudocode and their

statements

Page 17: Algorithms and Pseudocode

Algorithms and Pseudocode

Pseudocode and their

statements

Learning Objectives:(a) understand algorithms (written in pseudocode or flow diagram), explain what they do, and correct or complete them(b) produce algorithms in pseudocode or flow diagrams to solve problems.

,

Summary

Pseudocode is written a bit like a programming language however it should never be confused with one. It is independent of any language.

Page 18: Algorithms and Pseudocode

Algorithms and PseudocodePseudocode and their key

words / statements in action…

Learning Objectives:(a) understand algorithms (written in pseudocode or flow diagram), explain what they do, and correct or complete them(b) produce algorithms in pseudocode or flow diagrams to solve problems.

GET HeightGET BaseAnswer (Base * Height) / 2DISPLAY “Right angled triangle area” = AnswerEND

1. Use of capitalised pseudocode key words/statements

2. Indentation

GET NameFOR x = 1 TO 10

DISPLAY “Your name is” = NameEND

Page 19: Algorithms and Pseudocode

Algorithms and PseudocodeLearning Objectives:(a) understand algorithms (written in pseudocode or flow diagram), explain what they do, and correct or complete them(b) produce algorithms in pseudocode or flow diagrams to solve problems.

4 things you need to be able to do with algorithms 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

Page 20: Algorithms and Pseudocode

Algorithms and PseudocodeLearning Objectives:(a) understand algorithms (written in pseudocode or flow diagram), explain what they do, and correct or complete them(b) produce algorithms in pseudocode or flow diagrams to solve problems.

Tips on reading algorithms.

• Start from top and read each line at a time.

• Remember what the key words, arrows and statements mean

• Logically work out what the algorithms is doing.

Task: What is this algorithm doing?

Remember, arrows often show the assignment of a value to a variable. Here the variable is a list and will contain 5 items

Page 21: Algorithms and Pseudocode

Algorithms and PseudocodeLearning Objectives:(a) understand algorithms (written in pseudocode or flow diagram), explain what they do, and correct or complete them(b) produce algorithms in pseudocode or flow diagrams to solve problems.

Tips on correcting algorithms.Read the pseudocode and look for decisions which don’t make sense

Or it might be a loop which you cannot get into or perhaps out of.

STARTX 10WHILE X < 10

DO X X+1END

STARTWHILE TRUE

X TemperatureIF X > 19

Heating TRUEELSE-IF X < 21

Heating FALSEEND

Page 22: Algorithms and Pseudocode

Algorithms and PseudocodeLearning Objectives:(a) understand algorithms (written in pseudocode or flow diagram), explain what they do, and correct or complete them(b) produce algorithms in pseudocode or flow diagrams to solve problems.

Tips on completing algorithms.• 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 Use your knowledge of the pesudocode keywords

• …and complete the algorithm

STARTINPUT Length, Width, Height

objectVolume ←Print “Volume =

”END

Page 23: Algorithms and Pseudocode

Algorithms and PseudocodeLearning Objectives:(a) understand algorithms (written in pseudocode or flow diagram), explain what they do, and correct or complete them(b) produce algorithms in pseudocode or flow diagrams to solve problems.

Tips on writing algorithms.