Chapter 7 (Algorithms)
Program development life cycle
structured, organised plan of how to create the program)
These include the stages:
Analysis (Identification of the problem and requirements. Abstraction(simplifying the problem, removing unnecessary details from the problem and filtering out irrelevant details). Decomposition is used to split the problem into subproblems, allowing the developers to identify their requirements for each component and tackle these separately)
Design (overview of the program using a structure diagram, designing of algorithms using flowcharts and pseudocode)
Coding (writing the program in your chosen programming language)
Testing (program is tested for errors using test data, to ensure the program fully works, does not crash, and meets all requirements.)
Structure Diagrams
a hierarchical diagram that shows the decomposition of a system.
decomposing a program into its subprograms.
shows the relationship between different components of a system.
The diagram has the name of the program at the top and below this its subprograms.
Each subprogram can be split down further as well if required.
splitting up//decomposing a small program is to think of inputs, processes, outputs, and storage.
TOP-DOWN DESIGN is the breaking down of a computer system into a set of sub-systems, then breaking each sub-system down into a set of smaller sub-systems, until each sub-system just perform a single action
Flowcharts
A flowchart is a diagrammatic representation (using a set of standard symbols.) of an algorithm.
Flow lines
Decision
Input/Output
Process
Subroutines (2023)
Terminator
Pseudocode
Pseudocode refers to any code that is not written on a computer to run.
False codes but follows mathematical notations
Standard methods of solution
linear search (also known as sequential search, is a simple method to find a specific value in a list (or an array))
bubble sort (Bubble sort is a simple sorting algorithm that arranges a list of items (usually numbers) in ascending or descending order)
totalling (adding together a set of values//Initialise the total to 0//Add the values together (either individually or within a loop)//Example: Total<- Total+Marks)
counting (Counting is working out to count something//Initialise a counter variable to 0//Increment (add one to) the counter each time an item is entered, or found//Example: Count<- Count+1)
finding maximum, minimum, and average values
Validation Checks (2023)
Validation is an automated check carried out by a computer
to make sure the data entered is sensible/acceptable/reasonable and within set bounds.
Types:
range check (To make sure the data entered falls within a specific set of values//bounds//limits)
length check (check the number of characters that are present)
type check (checking the forms of data-integer (whole number), real (decimal number), string (any characters), Boolean (true or false))
presence check (makes sure that some data has been entered)
format check (makes sure the data is entered in a specific format, E.g. a date must be: __/ __ / ____ where each space is a number)
check digit (calculated from a set of numbers. To make sure an identification code entered is genuine or possible)
Verification Checks (2023)
To ensure that data has been accurately copied // to ensure that changes have not been made to the values originally intended when data is copied from one source to another
Types:
visual check (the user looks through the data that has been entered and confirms that no changes have been made)
double entry check (data is entered twice, the two entries are compared and if they do not match, a re-entry is requested)
Test data
check that the program works as expected
check for logic/runtime errors
check that the program rejects any invalid data that is input
check that the program only accepts reasonable data
Types:
normal test data: data that a program should accept (acceptable values)
abnormal test data: data that a program should not accept (rejected values)
extreme test data: data that is on the edge of what is allowed (largest/smallest acceptable value)
boundary test data: data that is on the edge of being accepted, and data that is on the edge of not being accepted (largest/smallest acceptable value and the corresponding smallest/largest rejected value)
Chapter 8 (Programming)
Data Types
BOOLEAN (A data type with two values, True or False )
CHAR (A single number, symbol or letter delimited by single quotes, e.g. ꞌxꞌ )
INTEGER (A whole number that can be positive, negative or zero and used in calculations)
REAL (A number with a fractional part that can be positive or negative and used in calculations)
STRING (A sequence of characters, words or phrase. Delimited by double quotes. A string may contain no characters (i.e. the empty string), e.g. "This is a string", "" )
Converting between data types
converting data from one data type to another data type//aso called Type casting
Example: Convert a string to an integer:
Number<- int("752")
Variables & Constants (2023)
variables and constants should have meaningful identifiers so that programmers/future programmers can understand their purpose
they are both used for data storage
constants store values that never change during the execution of a program
variables contain values that have been calculated within the program / can change during the execution of the program
variable example – any data that is input into a program such as a date
constant example– any value that does not change, such as Pi in mathematical formulae
Local vs Global Variables (2023)
local variables - scope is a defined block of code/subroutine/procedure/function and value cannot be changed elsewhere in the program
global variables – scope is the whole program and value can be changed anywhere in the program
Sequence
Sequential order of instructions
Selection
IF statements
It allows you to make decisions based on certain conditions.
checks whether a condition is true or false, and then executes specific code accordingly.
Uses:
Control the flow of your program.
Perform different actions based on different conditions.
Implement logic such as checking if a number is positive or negative, validating user input, or handling specific cases.
CASE statements
used to select one of several possible actions based on the value of an expression.
Uses:
Simplify code when you have multiple conditions to check.
Replace long chains of IF-ELSE-IF statements.
Execute different code blocks based on specific values.
Iteration
count-controlled loops (uses a counter to run a set number of times//FOR..TO..NEXT)
pre-condition loops (tests the condition before starting the loop//may or may not run//WHILE..DO..ENDWHILE) **FOR..TO..NEXT is also a pre-condition loop
post-condition loops (loop runs the code inside the loop once, and then checks the condition at the end of the loop//A loop that will iterate at least once// REPEAT..UNTIL)
String Handling
Length - LENGTH(<identifier>)
Substring - SUBSTRING(<identifier>, <start>, <length>)
Upper//UCASE - UCASE(<identifier>)
Lower //LCASE - LCASE(<identifier>)
LENGTH("Happy Days") will return 10
LCASE(ꞌWꞌ) will return ꞌwꞌ
UCASE("Happy") will return "HAPPY"
SUBSTRING("Happy Days", 1, 5) will return "Happy"
Types of Operators
Arithmetic( +, –, /, *, ^ (raised to power of), MOD, DIV)
Logical (=, <, <=, >, >=, <> (not equal to))
Boolean (AND, OR, NOT)
Subroutines
a self-contained piece of code that has an identifier and can be called from elsewhere in a program.
Use
Sub-program / system not the whole program/system
To perform a frequently used operation within a program
That can be called when needed
That can be reused by another program
Types:
procedures (a subroutine that does not return a value to the program that called it)
functions (a subroutine that does return a value to the program that called it)
both take one or more values as parameters.
both are defined once and called many times (Define – setting up the function/procedures and call is using a function/procedures)
Example:
FUNCTION ConvertToCm(Inches: REAL) RETURNS REAL
RETURN Inches * 2.4
ENDFUNCTION
variable Inches is called a parameter
Purpose (2024)
Procedures / functions divide the program into smaller manageable segments (making it more readable / easier to understand / easier to debug)
Procedures / functions with meaningful names help to provide documentation for the program / enable/help/provide abstraction
Procedures and functions may be re-used (in the program / in other programs / as part of a library) / run from a single line
Procedures and functions can reduce / eliminate (repeated) code
Library Routines (2023)
A collection of standard programs available for immediate use
MOD (To perform (integer) division when one number is divided by another and find the remainder)
Example: 7 MOD 2 = 1
DIV (To perform integer division)
Example: DIV(9, 4) = 2
ROUND (return a value rounded to a specified number of digits / decimal places)
Example: ROUND(4.56, 1) = 4.6
RANDOM ( To generate random numbers within a specified range)
Example: Value ← ROUND (RANDOM() * 6, 0) // returns a whole number between 0 and 6
Arrays
data structure
fixed-length structures of elements
identical data type
accessible by consecutive index numbers.
It allows you to store multiple pieces of data in one structure with one identifier
The dimension is the number of indexes required to access an element.
The index is the position of the element in an array
One-dimensional (1D)
a list / one column of items of the same data type
stored under a single identifier
with a single index to identify each element
Declaration
Example: DECLARE MyArray[1:10] OF INTEGER
Indexing
using a counter to index the array
so that the same code can be repeatedly used to check every element // every element can be checked in a loop
Two-dimensional (2D) arrays
A grid or matrix structure with rows and columns of items, all of the same data type
Stored under a single identifier
Each element is identified by two indices - one for the row and one for the column
Declaration
Example: DECLARE MyArray[1:10, 1:5] OF INTEGER
Indexing
Requires two indices, one for the row and one for the column
Allows for iteration over all elements using nested loops, iterating over rows and columns
File handling [2023, 2024]
OPEN (opening a file )
OPENFILE <File identifier> FOR <File mode>
READ (for data to be read from the file)
READFILE <File Identifier>, <Variable>
WRITE (for data to be written to the file)
WRITEFILE <File identifier>, <Variable>
CLOSE (closing a file )
CLOSEFILE <File identifier>
STORE (2023, 2024)
data is stored permanently / prevents the data from being lost
data can be moved to another computer
another copy of data can be made and stored//accessed elsewhere // backup copy//transferred to another computer
it can be recalled / used again in a program / in another program
Creating a maintainable program (2024)
always use meaningful identifier names for variables, constants, arrays, procedures, and functions (clearly identify the purpose of variables, constants, arrays, procedures, etc. )
be divided into modules for each task using procedures and functions (to avoid repeated code and to simplify logic)
be fully commented using your programming language’s commenting feature (to explain the purpose of each section of code)
use indentation and white space (to make the program readable)
Error identification in programs
Normally, the errors are found in:
INPUT/OUTPUT directives/statements
Inconsistent casing/use in identifier names
Conditional statements not fully meeting the requirements
Loops not iterating the necessary number of times
Discrepancy in the opening and closing of loops/conditional statements