Chapter (7 & 8)
Algorithms
and
programming
Program Development Life Cycle
Variable and a Constant
A variable is a storage location. It is a named value that
contains data that can be changed throughout the execution.
A constant is also a storage location. It is a named location that
contains a value that we don’t want to change during the
running of the program.
Use Suitable Declaration
✓ Meaningful Names
•The name should describe exactly the type of data stored in it.
Example
count = 0
student_name = “ ”
Const ticket_price=25
Use Suitable Declaration
✓ Variables
• Use lower case letters at the start of variable names
•Use either camel casing (without any white space, 1st letter is
lower and every word starts with a capital letter [fileNotFound]) or
snake casing (uses an underscore between words to create
separation [build_docker])
Use Suitable Declaration
✓ Constants
• Use “Const” at the start of the name
• Capitalise name
• Use either camel casing or snake casing
Camel
Snake
Use Suitable Declaration
✓ Array
• Can be either a specified or unspecified length
• Always use the square brakets
No length given
Length declared
Data type
Mathematical operators
Comparison Operators
Operators Description
== equal to
!= , <> not equal
> greater than
< less than
>= greater than or equal to
<= less than or equal to
Logical Operators
Operators Description
&& And
|| Or
ALGORITHMS
An algorithm is a set of instructions/steps/ rules that are followed to solve a
problem.
Algorithm design
There are two common methods to express algorithm designs, they are
1. Pseudocode
2. Flowcharts.
Pseudocode
➢ PSEUDOCODE is a method of expressing an algorithm design.
➢ Pseudocode is lines of instructions written in a language close to English but
with common programming terms used where possible (selection and iteration
etc).
➢ Pseudocode is set out with the same structure as a programming language,
but it is not a not programming language in itself.
Pseudocode
➢ a non-proportional font is used throughout
➢ all keywords (words used to describe a specific action e.g. INPUT) are written
in capital letters
➢ all names given to data items and subroutines start with a capital letter
➢ where conditional and loop statements are used, repeated or selected
statements are indented by two spaces
Flowcharts
❑ Flowcharts are a graphical way of representing an algorithm design.
❑ A flowchart must have a clear start and finish (unless looping).
Sequence
In an algorithm, each instruction is written one after the other. This is a called a
sequence.
E.g:
Output “How many number”
Input userinput
Number=number+1
Selection or Conditional statements
❖ Creating the ability to choose the path to follow in a
program is called selection.
❖ When different actions are preformed by an algorithm
according to the values of the variables, conditional statements
can be used to decide which action should be taken.
There are 2 types of selection :
▪ IF.. THEN .. ELSE .. ENDIF
▪ CASE .. OF .. OTHERWISE .. ENDCASE
IF.. THEN .. ELSE .. ENDIF
❑ A logical condition is tested.
❑ A positive (true) outcome will result in the THEN
statements of code being executed.
❑ A negative (false) outcome will result in the ELSE statements
IF.. THEN .. ELSE .. ENDIF
CASE .. OF .. OTHERWISE .. ENDCASE
CASE .. OF .. OTHERWISE .. ENDCASE
Purpose Of A Conditional Statement
▪ To allow different routes through a program
▪ dependent on meeting certain criteria
Iteration (Looping)
In a program there will be some instructions that need to be repeated.
To repeat instructions we put them in a loop and this is referred to as
iteration.
There are 3 types of iteration :
❑ FOR .. TO .. NEXT
❑ REPEAT .. UNTIL
❑ WHILE .. DO .. ENDWHILE
FOR .. TO .. NEXT
❑ Amount of iterations (times around the loop) is predetermined (fixed) at
beginning of the loop
❑ Already know how many iterations of the code is required
REPEAT .. UNTIL
❑ Condition/check is placed at the bottom of the loop, this is referred to as bottom
testing (or a post condition).
❑ At least one iteration of the loop will beexecuted
❑ If an exit criteria has been met yet
WHILE .. DO .. ENDWHILE
❑ Condition is placed at the start of the loop, this is referred to as top testing (or a pre-
condition)
❑ Criteria has been met yet (to exit the loop)
Procedures and Functions
A SUB-ROUTINE is a set of programming instructions for a given task that forms a
sub-system, not the whole system. Sub-routines written in high-level programming
languages are called ‘procedures’ or ‘functions’.
A procedure is a set of programming statements grouped together under a single
name that can be called to perform a task at any point in a program.
A function is a set of programming statements grouped together under a single
name that can be called to perform a task at any point in a program. In contrast to a
procedure, a function will return a value back to the main program.
Parameters are the variables that store the values of the arguments passed to a
procedure or function. Some but not all procedures and functions will have
parameters.
Counting and Totalling
❖Counting
❑ Counting is used with repetition with the counter increased by 1
every time the loopis repeated.
❖Totalling
❑ Totalling is used with repetition with the total updated every time
the loop is repeated.
Predefined Function, Library, Predefined Procedure
Predefined Function
a pre-programmed set of instructions that return a value.
(converting integer to string)
Library
a store of pre-programmed instructions that can be imported into a
program
(print, cout, cin)
Predefined Procedures
a pre-programmed set of instructions that do not return a value.
(clear the screen command)
Global and Local Variables
• A global variable can be used by any part of a program – its scope covers the
whole program.
• A local variable can only be used by the part of the program it has been
declared in – its scope is restricted to that part of the program.
Arrays
❑ An array is a data structure containing several elements of the same
data type; these elements can be accessed using the same identifier
name.
❑ An array is a way of storing data that is all related and of the same
type.
e.g: a list of the names of the students in your class
❑ An array is a data structure, storing more than one item of data
(value) at a time
❑ Allows to store an unlimited number of items with same data types.
Arrays
An Element is an individual data location in an array.
Each of the individual items in an array is called an element.
The index indicates the position of the element within the array
Read - Outputting data from an array.
Write – Inputting data into an array to store it.
One dimensional Array
A one-dimensional array can be referred to as a list.
0 1 2 3 4
ZingerTower BigMac FilletTower ChickenSandwich Whopper
e.g. burgers[2] contains “Fillet Tower”
One-dimensional Array Declaration
• the name of the array
• the first index value
• the last index value
• and the data type
DECLARE MyList : ARRAY[0:9] OF INTEGER
Two-dimensional Arrays
❑ can create relationships between stored data
❑ A two-dimensional array can be referred to as a table, with rows
and columns.
Name[4]
0 1 2 3 4
Jenny Diane Claire Caragh Ellie
Score[4]
0 1 2 3 4
1000 772 534 1423 2425
Ellie has a high score of 2425, and Claire of 534
Two-dimensional Array Declaration
• the first index value for rows
• the last index value for rows
• the first index value for columns
• the last index value for columns
• and the data type
DECLARE MyTable : ARRAY[0:9,0:2] OF INTEGER
Advantage and Disadvantage of Array
accessing an element is very easy by using the index number
search process can be applied easily
its size is always fixed
memory allocated to it cannot be increased or decreased
Purpose of storing data in a file
• Data stored in RAM will be lost when the computer is switched off, when data
is saved to a file it is stored permanently.
• Data stored in a file can thus be accessed by the same program at a later date
or accessed by another program.
• Data stored in a file can also be sent to be used on other computer(s).
• The storage of data in files is one of the most used features of programming.
Top-Down Design
❑ Top down design is the name given to breaking a problem down into increasingly
smaller and smaller manageable parts (also known as decomposition).
❑ Top-down design is the decomposition 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 performs a single action.
❑ Sometimes referred to as:
• Modules
• Sub-routines
• Procedures
Decomposing a problem
• Inputs – the data used by the system that needs to be entered while the
system is active
• Processes – the tasks that need to be performed using the input data
and any other previously stored data
• Outputs – information that needs to be displayed or printed for the
users of the system
• Storage – data that needs to be stored in files on an appropriate
medium for use in the future.
Decomposing a problem
• For example, the alarm app can be decomposed into:
• inputs – time to set the alarm, remove a previously set alarm time, switch
an alarm off, press snooze button
• processes – continuously check if the current time matches an alarm
time that has been set, storage and removal of alarm times,
management of snooze
• outputs – continuous sound/tune (at alarm time or after snooze time
expired)
• storage – time(s) for alarms set.
Structure Diagrams
❑ Structure diagrams are a graphical way of representing a problem, showing the
different levels of detail. They are a great way to illustrate all the systems and
sub-systems. A diagram that shows tasks that have been broken down and
how they relate to each others
Benefits of Top-Down Design
❖ Breaking a problem down into smaller parts/tasks make it far easier to
understand, solve and manage
❖ Top down design allows several programmers or teams to work on the
same project, without getting in each other’s way
❖ Each module of code to be tested separately
FINDING ERRORS (Debugging)
❑ Finding syntax error
❑ Finding Logical Errors
Syntax and Logic Error
Syntax Error
Syntax Error occurs when a programmer does not follow the rules or structure
of the language they are writing in.
Logic Error
A logic error is an error in the code that causes the program to do something it
should not.
Testing
❑ Testing is the process of finding and removing errors from your code.
❑ removing syntax errors is easy to spot as your interpreter
❑ Finding Logical errors can be a lot trickier and can end up being a very
time consuming process.
Example of Test Data
A system has validation to ensure that only numerical values between 1 and 10
are entered as an input.
The test data for this could be:
❑ Normal data: 5
❑ Boundary data (extreme data): 1
❑ Boundary data (extreme data): 10
❑ Abnormal data (erroneous data): Thirteen
Test Data
❑ Normal data
• Normal data is test data that is typical (expected) and should be
accepted by the system.
❑ Boundary data (extreme data)
• Boundary data (sometimes called extreme data) is test data at the
upper or lower limits of expectations that should be accepted by the
system.
❑ Abnormal data (erroneous data)
• Abnormal data is test data that falls outside of what is acceptable and
should be rejected by the system.
Differences between Extreme and Boundary Data
• Extreme data is the largest/smallest acceptable value
• Boundary data is the largest/smallest acceptable value and the corresponding
smallest/largest rejected value
TRACE TABLES
❑ A trace table provides a structured method of recording and studying the
results and data for each step of the algorithm.
Validation
❑ Validation is an automatic check to ensure that data entered is
sensible and feasible.
❑ Validation cannot ensure data is accurate.
Validation methods
Validation Method Description
Checks the data falls between an acceptable upper and lower value, within
Range check
a set range. E.g: number will be 1 to 5
Checks that the data entered is of an expected type, e.g. text or a number
Type check
Email address, ID Number
Checks the number of characters meets expectations, e.g. an 8
Length check
characters password
Checks that the user has at least inputted something, stopping them from
Presence check
accidentally entering nothing
An extra digit added to a number which is calculated from the other digits,
Check digit
this ensures the rest of the number has been entered properly
Verification
❑ Verification is a way of preventing errors when data is copied from
one medium to another
❑ Verification does not check if data makes sense or is within
acceptable boundaries
❑ Only checks that the data entered is identical to the original source
❑ To test if the data input is the same as the data that was intended to
be input.
Verification methods
Verification Method Description
Double entry Data is entered twice and the computer checks that they match up.
Eg:
Enter password:
Re enter password:
The user manually reads and compares the newly inputted data against the original
Visual check source to ensure they match