Algorithms

12
1 Algorithms for Program Design

Transcript of Algorithms

Page 1: Algorithms

1

Algorithms

forProgram Design

Page 2: Algorithms

22

Process Modelling

Process modelling is performed during the design

stage of developing a new system.

Data Flow Diagrams - show WHAT the new system

must be able to do.

Process Specification - provides details of HOW the

required functionality is achieved.

Various techniques used to clarify procedures

Structure Diagrams

Flow Charts

Pseudocode / Structured English

Provides the logical design for a computer algorithm

Page 3: Algorithms

33

Flow ChartsStartStart

• Not to be confused with Data Flow Diagrams (DFDs)

Shows steps required to achieve the correct outcome

Flow charts provide a graphical representation of an algorithm• Can become complex and hard to follow

• Best used to give overview of functionality.

Pseudocode provides a non-technical description of an algorithm• More detailed and closely related to

programming code

Structure diagrams are useful to show a hierarchical overview of program modules • Helps to identify how the main project

components will be created

• Input - Processing - Output

Subtotal = 0VAT = 1.175Subtotal = 0VAT = 1.175

Input: ItemPriceInput: ItemPrice

End of Item list?End of Item list?

Subtotal = Subtotal + ItemPrice

Subtotal = Subtotal + ItemPrice

Total = Subtotal * VATTotal = Subtotal * VAT

Output: “Total Price = £” (Total)

Output: “Total Price = £” (Total)

StopStop

nono

yesyes

Page 4: Algorithms

44

Pseudocode

Pseudocode is a technique used during the design

stage of developing a program.

Used to translate the users requirements into a into a

complete sequence of unambiguous, logical steps.

It is written in Structured English to describe the

system operations accurately and precisely,

including all the steps need to code the program.

Can be understood by any programmer & coded into

any language.

Page 5: Algorithms

55

How to….Create an Algorithm

Start with a statement of the user’s needs

Then write a program specification

Then write out in sentences what the program needs to do at each step

All of the time, consider how a computer runs an event driven program

INPUT – PROCESS – OUTPUT

Page 6: Algorithms

66

How to ... Pseudocode

Starting with general sentences (Structured English)

Remove as many of the extra words as you can - just

leave the bare minimum

If values are being entered - input:

use the term “read in” or “get”…..

For any processing or calculations

use the logical operators + - * / < > = “True”, “False”

If results are being displayed - output:

use the term “print”

The final pseudocode should be language independent,

but must avoid any ambiguity.

Page 7: Algorithms

77

Example:

Description of the stages of a program for adding two numbers and displaying the result:

The user types in Number1

The user types in Number2

The program adds Number1 and Number2

The program displays the Total on the screen

This isn’t written in pseudocode quite yet……

Page 8: Algorithms

88

Example:

Read in Number1

Read in Number2

Total = Number1 + Number2

Print (Total)

Now this is like program code

Because of this it is called pseudocode

Structured English is what it is written in

Page 9: Algorithms

99

Example: Verify Product

Page 10: Algorithms

1010

Example: Verify Product

For each Ordered Product Get Product(CatNo), Ordered(CatNo)If Product(CatNo) = Ordered (CatNo) Then

Get Product (Level), Ordered (Quantity), If Product (Level) >= Ordered (Quantity)

Then Order accepted = True Else Order accepted = False End If

ElsePrint Invalid Product No

End IfNext ProductPrint confirmed order

Page 11: Algorithms

1111

Example: HND course

Page 12: Algorithms

1212

Example: Credit Card AuthorisationSTART Bank connection

GET Total due, Card type, Card number, Expiry dateGET Stolen cards listIF Stolen = True THEN

Print “Retain card and destroy”ELSE

LOOP WHILE Counter < 3 AND PIN = False Increment Counter GET PIN

IF PIN = True THEN GET Limit, Balance

IF Total Due < Limit - Balance THEN GET Ref (Transaction) Balance = Balance + Total Due PRINT “Transaction authorised” & Ref

ELSE Authorised = False PRINT “Insufficient funds available”

END IFEXIT Bank connection

END IFEND LOOP PRINT “Incorrect PIN - Retain card and refer to bank”

END IFEXIT Bank connection