Pseudocode and General Stuff

Sample Pseudocode

BEGIN
    CONSTANT StdDeduct = 10000
    CONSTANT DepDeduct = 2000
    CONSTANT Rate = 0.20
 
    INPUT GrossIncome
    INPUT NumDep
 
    TotalDeduct ← StdDeduct + (NumDep * DepDeduct)
    TaxableIncome ← GrossIncome - TotalDeduct
    IncomeTax ← TaxableIncome * Rate
 
    IF IncomeTax < 0
        OUTPUT 'Income Tax is Negative !'
    ELSE
        OUTPUT IncomeTax
    ENDIF
END

Common Pseudocode Keywords

  • BEGIN / END
  • INPUT / OUTPUT
  • IF / ELSE / ENDIF
  • FOR / ENDFOR
  • WHILE / ENDWHILE
  • CONSTANT
  • — define
    • <identifier> ← <value>

Array Declaration

DECLARE numbers : ARRAY[0:3] OF INTEGER

Data Validation

Purpose

To ensure that the data entered is sensible and reasonable

TypeHow it WorksExample
Existence CheckChecks if input is already in the systemStudent name is in the database
Format CheckChecks if the data is in the correct formatDate in the format DD/MM/YYYY
Length CheckChecks if the length of the data is correctLength of password
Presence CheckChecks if data is entered into a fieldUsername cannot be left blank
Range CheckChecks if the data value is within a certain rangeMark must be from 0 to 100
Type CheckChecks if data is of the correct typeNumber must be an integer (120), not a string (“120”)
Check DigitThe last one or two digits in data are used to verify whether the other digits are correctNRIC/FIN number

Data Verification

Purpose

To ensure the input data matches the original source

MethodHow it WorksExample
Double EntryData is entered twice to reduce the chance of errorsCreating a new password and entering it twice to reduce the chance of typos
ProofreadChecks that entered data is as intendedReviewing an online form before submitting to ensure the data entered is correct

Programming Errors

  1. Syntax error occurs when the syntax rules in the programming language is not adhered to (e.g. error in the syntax of a sequence of characters of tokens). Program will not compile until all syntax errors are corrected.
  2. Logical error is caused by wrong program design. This could happen when the algorithm for solving the program in incorrect. The program may be compiled and executed but does not give the expected output.
  3. Run-time error is detected at run-time e.g. stack overflow, division by 0, open a file that does not exist.

Modular Approach

Definition

AspectDetails
DefinitionModularity is the feature of designing a problem solution as a collection of well-defined separate modules, each serving a specific purpose and connected to the main program.
ModuleA module is a complete part-program used within the main program.

Advantages

AdvantageExplanation
Encourages code reuseModules can be stored in a library and reused in other solutions.
Easier to debugModules are small and easier to test individually.
Easier to maintain and modifyModules can be added or removed easily.
Supports teamworkDifferent programmers can work on different modules simultaneously, and each module can be coded and tested separately.
Easier project managementLarge projects become easier to monitor and control.

Disadvantages

DisadvantageExplanation
Time-consuming planningExtra time is needed to divide the problem into modules.
Larger memory usageMore memory space may be required.
Limited visibilityProgrammers may not see all the code or documentation.
Over-modularisationToo many modules can make the system unnecessarily complex.

Incremental Approach

An incremental approach solves a small version of the problem first and gradually extends the solution to larger versions instead of recalculating from scratch.

Advantages

AdvantageExplanation
Efficient testing and debuggingSmall portions are added and tested step by step.
Early problem detectionBugs can be identified and fixed early because development starts small.

Disadvantages

DisadvantageExplanation
Code may become messyThe algorithm can become unstructured as it grows.
Harder to reuse codeThe code may not be easily reusable in other algorithms.