Paper 2: Algorithm Design and Problem-solving


Algorithm Design and Problem-Solving

Computational Thinking Skills

Abstraction

Decomposition

Benefits of Decomposition

Algorithms

Stepwise Refinement

Data Types and Structures

Defining Record Structures (using arrays of given records and type)

Advantages of Records

Uses of BOOLEAN

Arrays

Files

Advantages of storing data

WRITE mode overwrites a file completely/ overwrites any existing data in file

How to store a set of data in a file

Abstract Data Types

Linked Lists

Adding / inserting a new node

Advantages of linked lists

Disadvantages of linked lists

Implementing a Linked List

Use Record based Implementation

Queues

Adding / inserting a value from a queue

Removing an item from a queue

Required Operations

Implementing a queue

Stack

Implementing a stack

Why files may not appear correctly if using a stack

Programming

Features that made codes understandable

Importance of ‘Good Practice’

Constants

Library Routines / Program Libraries

Benefits of using Libraries

Use

Constructs

Selection constructs

Iterative constructs

Structured Programming

Functions / Procedures

Benefits

A function returns a value a procedure can output values

Software Development

Program Development Life Cycle

Benefits

Stages

  1. Analysis
  1. Design
  1. Coding
  1. Testing

Waterfall Design

Benefits of Waterfall Design

Disadvantages of Waterfall Design

Iterative Design

Benefits of Iterative Design

Drawbacks of Iterative Design

Rapid Application Development (RAD)

Benefits of RAD

Drawbacks of RAD

Program Design

Purpose of Structure Charts

Symbols

State-Transition Diagrams

Program Testing and Maintenance

Types of Maintenance

Types of Testing

Types of Errors

Syntax Errors

A program with no syntax errors…

Run-time Errors

Test Data

Pseudocode Programming

Data Types

INTEGER a whole number 5, -3
REAL a number containing a fractional part 4,7, -4.0
CHAR a single character 'x', '@'
STRING a sequence of zero or more characters "hi", ""
BOOLEAN the logical values TRUE and FALSE TRUE, FALSE
DATE a valid calendar date dd/mm/yyyy

Variable Declarations / Constants

DECLARE Counter : INTEGER  

DECLARE TotalToPay : REAL

DECLARE GameOver : BOOLEAN

CONSTANT HourlyRate = 6.50

Assignments

<identifier> ← <value> so for example: Counter ← Counter + 1

Arrays

DECLARE StudentNames : ARRAY[1:30] OF STRING

DECLARE NoughtsAndCrosses : ARRAY[1:3,1:3] OF CHAR

StudentNames[1] ← "Ali"

NoughtsAndCrosses[2,3] ← 'X'

User-defined types

Non-composite data types

A user-defined non-composite data type with a list of possible values is called an enumerated data type.

TYPE Season = (Spring, Summer, Autumn, Winter)

A used-defined non-composite data type referencing a memory location is called a pointer. The pointer should be declared as follows:

TYPE TIntPointer = ^INTEGER

Composite data types

A composite data type is a collection of data that can consist of one or more data types, grouped under one identifier.

TYPE StudentRecord
  DECLARE LastName : STRING
  DECLARE FirstName : STRING
  DECLARE DateOfBirth : DATE
  DECLARE YearGroup : INTEGER
  DECLARE FormGroup : CHAR
ENDTYPE

There is also the set data type which is like set() in python

TYPE LetterSet = SET OF CHAR
DEFINE Vowels ('A', 'E', 'I', 'O', 'U'): Letter Set

Using user-defined data types

DECLARE Pupil1 : StudentRecord  
DECLARE Pupil2 : StudentRecord
DECLARE Form : ARRAY[1:30] OF StudentRecord
DECLARE ThisSeason : Season
DECLARE NextSeason : Season
DECLARE MyPointer : TIntPointer

Pupil1.LastName ← "Johnson"
Pupil1.FirstName ← "Leroy"
Pupil1.DateOfBirth ← 02/01/2005
Pupil1.YearGroup ← 6
Pupil1.FormGroup ← 'A'

Pupil2 ← Pupil1

FOR Index ← 1 TO 30
  Form[Index].YearGroup ← Form[Index].YearGroup + 1
NEXT Index

ThisSeason ← Spring
MyPointer ← ^ThisSeason
NextSeason ← MyPointer^ + 1

Input and output

INPUT Answer

OUTPUT Score

OUTPUT "You have", Lives, " lives left"

Selection

IF <condition> THEN
  <statement(s)>
ENDIF
IF <condition> THEN
  <statement(s)>
ELSE
  <statement(s)>
ENDIF
INPUT Move
CASE OF Move
  'W' : ...
  'S' : ...
  'A' : ...
  'D' : ...
  'OTHERWISE': CALL Beep
ENDCASE

Loops

FOR Index ← 1 TO 100
  OUTPUT Index
NEXT Index
REPEAT
  OUTPUT "Please enter the password"
  INPUT Password
UNTIL Password = "Secret"
WHILE Number > 9
  Number <- Number - 9
ENDWHILE

Procedures and Functions

PROCEDURE <identifier>()
  <statements(s)>
ENDPROCEDURE

CALL <identifier>()
PROCEDURE <identifier>(<param1>: <type>, ...)
  <statements(s)>
ENDPROCEDURE

CALL <identifier>(value1, ...)

By default pass by value is assumed this can be modified by writing BYREF

FUNCTION <identifier(<params>: <types>, ...) RETURNS <type>

  RETURN <vlue>
ENDFUNCTION

File handling

DECLARE LineOfText : STRING
OPENFILE "FileA.txt" FOR READ
OPENFILE "FileB.txt" FOR WRITE

WHILE NOT EOF("FileA.txt")
  READFILE "FileA.txt" LineOfText
  IF LineOfText = "" THEN
    WRITEFILE "FileB.txt", "------------------------"  
  ELSE
    WRITEFILE "FileB.txt", LineOfText
  ENDIF
ENDWHILE

CLOSEFILE "FileA.txt"
CLOSEFILE "FileB.txt"

Handling random files

DECLARE Pupil : Student 
DECLARE NewPupil : Student
DECLARE Position : INTEGER

NewPupil1.LastName ← "Johnson"
NewPupil1.FirstName ← "Leroy"
NewPupil1.DateOfBirth ← 02/01/2005
NewPupil1.YearGroup ← 6
NewPupil1.FormGroup ← 'A'

OPENFILE "StudentFile.Dat" FOR RANDOM
FOR Position ← 20 TO 10 STEP -1
  SEEK "StudentFile.Dat", Position
  GETRECORD, "StudentFile.Dat", Pupil
  SEEK "StudentFile.Dat" Position + 1
  PUTRECORD "StudentFile.Dat", Pupil
NEXT NextPosition

SEEK "StudentFile.Dat", 10
PUTRECORD "StudentFile.Dat", NewPupil

CLOSEFILE "StudentFile.Dat"

Object-oriented Programming

CLASS Pet
  PRIVATE Name : STRING
  PUBLIC PROCEDURE NEW(GivenName: String)
    Name ← GivenName
  ENDPROCEDURE
ENDCLASS
CLASS Cat INHERITS Pet
  PRIVATE Breed: INTEGER
  PUBLIC PROCEDURE NEW(GivenName: STRING, GivenBreed: STRING)
    SUPER.NEW(GivenName)
    Breed ← GivenBreed
  ENDPROCEDURE
ENDCLASS

MyCat ← Cat("Kitty", "Shorthaired")