Algorithms


      for
Program Design


                 1
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
                                                       2
Start
           Start
                                               Flow Charts
     Subtotal = 00
      Subtotal =                  •   Not to be confused with Data Flow Diagrams (DFDs)
     VAT = 1.175
     VAT = 1.175                      Shows steps required to achieve the
                                      correct outcome
    Input: ItemPrice
     Input: ItemPrice                 Flow charts provide a graphical
                                      representation of an algorithm
 Subtotal = Subtotal +
  Subtotal = Subtotal +                •   Can become complex and hard to follow
      ItemPrice
       ItemPrice                       •   Best used to give overview of functionality.
                                      Pseudocode provides a non-technical
                             no
    End of Item list?
     End of Item list?                description of an algorithm
                                       •   More detailed and closely related to
             yes                           programming code
Total = Subtotal **VAT
 Total = Subtotal VAT                 Structure diagrams are useful to show a
                                      hierarchical overview of program modules
        Output:
         Output:                       •   Helps to identify how the main project
“Total Price = £” (Total)
 “Total Price = £” (Total)                 components will be created
                                       •   Input - Processing - Output
          Stop
           Stop
                                                                                          3
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.

                                                         4
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



                                                 5
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.
                                                             6
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……


                                                  7
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


                                               8
Example: Verify Product




                          9
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
        Else
                Print Invalid Product No
        End If
Next Product
Print confirmed order
                                                                10
Example: HND course




                      11
Example: Credit Card Authorisation
 START Bank connection
 GET Total due, Card type, Card number, Expiry date
 GET Stolen cards list
 IF 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 IF
                                  EXIT Bank connection
                       END IF
            END LOOP
             PRINT “Incorrect PIN - Retain card and refer to bank”
 END IF
 EXIT Bank connection
                                                                                    12

Algorithms

  • 1.
    Algorithms for Program Design 1
  • 2.
    Process Modelling Process modellingis 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 2
  • 3.
    Start Start Flow Charts Subtotal = 00 Subtotal = • Not to be confused with Data Flow Diagrams (DFDs) VAT = 1.175 VAT = 1.175 Shows steps required to achieve the correct outcome Input: ItemPrice Input: ItemPrice Flow charts provide a graphical representation of an algorithm Subtotal = Subtotal + Subtotal = Subtotal + • Can become complex and hard to follow ItemPrice ItemPrice • Best used to give overview of functionality. Pseudocode provides a non-technical no End of Item list? End of Item list? description of an algorithm • More detailed and closely related to yes programming code Total = Subtotal **VAT Total = Subtotal VAT Structure diagrams are useful to show a hierarchical overview of program modules Output: Output: • Helps to identify how the main project “Total Price = £” (Total) “Total Price = £” (Total) components will be created • Input - Processing - Output Stop Stop 3
  • 4.
    Pseudocode Pseudocode is atechnique 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. 4
  • 5.
    How to….Create anAlgorithm 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 5
  • 6.
    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. 6
  • 7.
    Example: Descriptionof 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…… 7
  • 8.
    Example: Read in Number1 Readin 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 8
  • 9.
  • 10.
    Example: Verify Product Foreach 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 Else Print Invalid Product No End If Next Product Print confirmed order 10
  • 11.
  • 12.
    Example: Credit CardAuthorisation START Bank connection GET Total due, Card type, Card number, Expiry date GET Stolen cards list IF 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 IF EXIT Bank connection END IF END LOOP PRINT “Incorrect PIN - Retain card and refer to bank” END IF EXIT Bank connection 12