Subtopic Notes
7 Algorithm design and problem-solving
7. Algorithm design and problem-solving
Algorithm
Solution to a problem expressed as a sequence of defined steps.
Three basic programming constructs:
- Sequence: The steps are performed one after another
- Selection: Depending on conditions different steps are performed.
- Repetition/Iteration/Loops: A set of steps are performed one after another
Programming Life Cycle
Analysis:
- Firstly the problem must be identified and detailed requirements or specification may be documented
- Uses tools like abstraction and decomposition in this stage
- Abstraction:
- Filtering out unnecessary details and only working with the relevant data
- Reduces the complexity of algorithm
- Decomposition of the problem
- Breaking down a problem into simpler and smaller subproblems which is easier to handle
- Every computer system is made up of subsystems, which are made up of further sub-systems
- Program Modules (Procedure/Function): Reusable block of code designed to perform a specific task
- Algorithms are decomposed into these parts: Inputs, Processes, Outputs, Storage
Design
- Program specification from the analysis stage is used to design the program
- Documentation: Written text or instructions explaining how a system or software works, helping users or developers understand and use it effectively
- Documentation uses methods like Structure Charts, Flowchart, Pseudocode, Structured english
- Structured English: A subset of English that consist of command statements used to describe an algorithm
Coding
- The main program is written down or developed based on the design
- Different modules may use different language and maybe created by different teams
Testing
- The completed program is now tested using different type of test data or inputs to identify the errors
- The bugs or issues identified in the application is corrected
- Testing is done to make sure the modules work as expected
- Iterative Testing: Conducting tests on modules, making changes as required and repeating these until all requirements are met
Structure Diagrams/Chart
Every computer system is made up of subsystems, which are made up of further sub-systems. Structure chart is used to represent this top-down design using a tree structure, showing the relationship between modules with the data transferred along it.
Pseudocode
- Writing an algorithm using plain English and programming-like structures to outline the logic of the problem
- For syntax it is recommended to use the pseudocode guide given in the syllabus.
Flowchart
A diagram that uses symbols and arrows to show a visual representation of the algorithm
| Action | Symbol | Description |
|---|---|---|
| Flowline | Represents control passing between the connected shapes. | |
| Process | Represents something being performed or done | |
| Subroutine | Represents a subroutine call that will relate to a separate, non-linked flowchart | |
| Input/Output | Represents the input or output of something into or out of the flowchart. | |
| Decision | Represents a decision (Yes/No or True/False) that results in two lines representing the different possible outcomes. | |
| Terminator | Represents the ‘Start’ and ‘Stop’ of the process. |
Validation
Checks whether the input data is correct, reasonable, and meets the required criteria. (You must need to write algorithm)
- Range Check: Checks if an input number is within acceptable values. Eg. Checking if age is greater than 18 and less than 80
- Length Check: Checks if input has a precise number of characters or the length falls in a specific range. Eg. checking if phone number is exactly 11 digits
- Type Check: Checks if data entered is of the required data type. Eg. Age field will only accept numbers
- Presence Check: Checks if an input is given. Eg. Required fields in forms
- Format Check: Checks if data is in specified format. Eg. dd/mm/yy for date fields
- Check Digit:
- Final digit included in an identification number
- The digit is calculated from other digits
- Used in ISBN, License Plates, Barcodes
Verification
Verification ensures the input data matches the source or expected values accurately. Verification may happen during data entry or data transmission.
- Entry Level
- Double Entry
- Input is given twice in separate fields
- The program compares the values and rejects if they don't match
- Visual Check
- The program displays the data and gives a prompt to the user to confirm before proceeding
- Double Entry
- During data transmission
- Parity Check
- Echo Check
- ARQ - Automatic Repeat Request
- Checksum
Test Data
Different types of test data may be used to check how the computer handles different scenarios. Let the eg. acceptable range be 20 to 40 inclusive
- Normal: Data falling under acceptable range to test if the results are as expected. Eg. 20 <= any value <= 40
- Abnormal/ Erroneous : Data that should be rejected and error should be displayed. Eg. 85, 15
- Extreme: Largest and smallest values that normal data may take Eg. 20, 40
- Boundary: Used to check where the largest and smallest value occur. At each boundary, two values are checked where one is rejected and the other is not. Eg. 19, 20, 40, 41
Trace Table/Dry Run
-
Checks the outcomes of every steps in an algorithm
-
Record the values of variable every time it is updated
-
Dry Run: Manually executing an algorithm following it’s step
-
A trace table might have columns for these
- Step: The line number being executed is written here
- Input: The values provided to the program is written here
- Variable: Whenever a variable's value changes, the new value is written in respective column
- Output: Whenever a value is outputted, it is written here
-
The values of the table can be checked if they provide the expected results. Errors can be detected if unexpected outputs are found.
