You are on page 1of 11

Theory Practical

Algorithm Design and Problem Solving:


Algorithm: A sequence of defined steps/instructions.
Stages of an algorithm:
1. Input
2. Process
3. Output

Algorithms can be documented by a program designer using English, program flowcharts, and
pseudocode.
When to present : when designing a solution to a problem
Purpose : to describe a solution as a sequence of steps.

Problem Decomposition: Breaking a problem down into sub-tasks. This is done to make the
problem easier to solve. It makes the solution easier to implement/test/maintain.

Stepwise-refinement: A method for increasing the level of detail of an algorithm.


It is used to express the algorithm in a level of sufficient detail.
It makes the problem more understandable.

Transferable Skill: Knowledge of one programming language


can be applied to an unknown language. This helps programmers learn new languages

Features that a programmer should recognise from an unknown language:


● Control Structures/Selection Statements/Iteration statements/IO statements
● Modular structure (Functions/procedures)
● Parameters to and from subroutines
● Variable declaration/assignment/data structures

An Abstract Data Structure is a collection of data and a set of associated operations:


● Create a new instance of the data structure
● Find an element in the data structure
● Insert a new element into the data structure
● Delete an element from the data structure
● Access all elements stored in the data structure
Data Types and structures:

Records
In pseudocode a record type is declared as:

TYPE <TypeIdentifier>
DECLARE <field identifier> : <data type>
.
ENDTYPE
E.g
TYPE Node
DECLARE data : INTEGER
Declare next : INTEGER
ENDTUPE

We can now declare a variable of this record type:

DECLARE <variable identifier> : <record type>


E.g Declare Head : Node

And then access an individual field using the dot notation:

<variable identifier>.<field identifier>


E.g Head.data

Arrays
Array: A structure for the temporary storage of data
Array index: row or column number of an individual array element
Upper bound: the highest number index of an array dimension
Lower bound: the smallest number index of an array dimension

In pseudocode, a 1D array declaration is written as:


DECLARE <arrayIdentifier> : ARRAY[<lowerBound>:<upperBound>] OF
<dataType>
Here is a pseudocode example:
DECLARE List1 : ARRAY[1:3] OF STRING // 3 elements in this list
DECLARE List2 : ARRAY[0:5] OF INTEGER // 6 elements in this list
DECLARE List4 : ARRAY[0:25] OF CHAR // 26 elements in this list
Bubble sort: a sort method where adjacent pairs of values are compared and swapped
Limit ← MaxIndex – 1 // MaxIndex: The upper bound of the array
Repeat
NoSwaps ← TRUE
FOR j ← 0 TO Limit// number of pairs of elements compared
IF MyList[j] > MyList[j + 1]
THEN
Temp ← MyList[j] // temp used for swapping values
MyList[j] ← MyList[j + 1]
MyList[j + 1] ← Temp
NoSwaps ← FALSE
ENDIF
NEXT j
Limit ← Limit– 1.
UNTIL NoSwaps = TRUE

Linear Search: checking each element of an array in turn for a required value

MaxIndex ← 6 // Integer: The number of elements in the array


INPUT SearchValue // The value to be searched for
Found ← FALSE // Boolean: True if found and False if not found
Count ← 1
While count <= MaxIndex AND Found = FALSE
If MyList[Count] = SearchValue
Found ← TRUE
ENDIF
Count ← Count + 1
ENDWHILE
PRINT Found

Basic DataTypes

String : sequence of characters


Integer : Whole Number
Real : Decimal Number
Char : Single Character
Boolean : two option e.g yes,no
Date : e.g 24/10/1995
Programming

Constructs of an algorithm:
● Sequence: Instructions/lines of code are executed in a fixed order.
● Assignment: A value is given to a variable
● Selection: Testing a condition to determine the sequence of execution//path of program
execution.
● Repetition/Iteration: A method of executing certain lines of code more than once

Types of loops:
● Count-controlled: the number of iterations is known/fixed e.g FOR
● Post Condition: the number of iterations depends on some condition being tested at the
end. The loop is executed at least once. E.g Repeat
● Pre Condition: the number of iterations depends on some condition being tested at the
beginning. The loop may never be executed. E.g While

Parameter: a value passed between modules(functions)

Passing Parameters to a function:


● By value: the actual value is passed into the procedure
● By reference: the address of the variable is passed into the procedure
Software Development
Software Development Cycle:
1) Problem Definition/ Analysis
2) Design
3) code/implementation
4) Testing
5) Maintenance
Purpose of upward & downward arrows :
- Upward arrows: more work is required at a previous stage to complete the
current stage
- Downward arrows: result from one stage is passed to the next
Main decisions in Design stage :
- Programming language
- Data structure
- Use of library routines (Functions)
- Testing methods
- Abstraction : removing information that is not essential, for quicker design

The waterfall model:


Makes software development process in a linear sequential flow. This means that any
phase in the development process begins only if the previous phase is complete

Benefits:
● Simple to understand as the stages are clearly defined.
● Easy to manage due to the fixed stages in the model. Each stage has specific outcomes.
● Stages are processed and completed one at a time.

Drawbacks:
● No working software is produced until late during the life cycle.
● Not a good model for complex projects.
● Poor model for long and ongoing projects.
● Integration is done at the very end, which doesn’t allow identifying potential technical or
business issues early.
The Iterative Model
development starts with implementing a small part of requirements. Repeated (iterative) reviews
to identify further requirements eventually result in the complete system.
Types of iterative

Rapid Application Development (RAD) model :


Uses prototyping. A prototype is a working model of part of the solution.

In the RAD model, the modules are developed in parallel as prototypes and are integrated
to make the complete product for faster product delivery..

The analysis, design, code and test phases are incorporated into a series of short, iterative
development cycles.

Benefits of iterative / rapid :


● There is a working model of the system at a very early stage of development , which can
be reviewed by the client.
● Changing requirements can be done.
● Increases reusability of components.
● Integration from the very beginning solves a lot of integration issues.

Drawbacks:
● Only systems that can be modularised can be built using RAD.
● Requires highly skilled developers/designers.
● Requires user involvement throughout the life cycle.

Structure chart: a graphical representation shows how a program is broken down to


sub-tasks(modules), Modules could be function or procedure

Finite state machine (FSM): a machine that consists of a fixed set of possible states with a set
of inputs that change the state and a set of possible outputs
State-transition table: a table that gives information about the states of an FSM
Testing:

Stub testing: Testing may be carried out before modules are developed when they’re not yet
ready for full testing. Module stubs contain simple code to provide a known response.

Black-box testing: A testing method used when the structure of the program is unknown. Can
reveal:
● Run-time error
● Logical error
● The algorithm is incorrect. Incorrect calculator performed

White-box testing: Testing all possible paths through the code. Normal, boundary and
erroneous data are used for testing.

Dry run (Walkthrough): the process of checking the execution of an algorithm or program by
recording variable values in a trace table. A trace table a table with a column for each variable
that records their changing values.

Integration testing: Tested by the programmer or in-house testers when separate modules have
been written and tested to ensure the modules work together as expected.

Alpha testing: Tested by the programmer or in-house testers near the end of development after
integration testing and before beta testing. This is done to find errors not found in earlier testing
and to ensure the software is ready for beta.

Beta testing: Tested by a small selection of potential end-users or clients before the general
release of the software. This is done to receive constructive feedback when tested in real-life
scenarios. It ensures that the software is ready for release.
si
Acceptance testing: Tested by the end-user of the software when the software is finished to
ensure the software is what the customer ordered and if meets the user requirements.

Types of test data:


Normal (valid): Typical data values that are valid

Abnormal (erroneous): Data values that the system should not accept

Boundary (extreme): Data values that are at a boundary or an extreme end of the range of
normal data; test data should include values just within the boundary (that is, valid data) and just
outside the boundary (that is, invalid data)
Types of errors:

● Syntax error: A statement in the source code that breaks the rules of the language
● Logic error: An error in the algorithm that causes the program not to behave as intended
● Run-time error: A program performs an invalid operation. Examples: Tries dividing by
zero; enters an infinite loop; stops unexpectedly.

Program fault: Something that makes the program not do what it is supposed to do, under
certain circumstances.

Ways of minimising program faults during design and coding stages:


1. The use of tried and tested library //subroutines
2. The use of modular programming(to break the problem down and make it easier to solve)
3. The use of good programming practice to make the code easier to read. (example: using
sensible variable names)
4. IDE features ( auto-complete)

Ways of finding program faults:


1. Dry run(Tracing)
2. Try different test values to see which ones fail
3. Use single stepping to execute instructions

Testing techniques:
Single stepping:
● Executes a line of code at a time
● Use to trace the path of execution (sequence)
● Track variable values using a watch window

Advantages of using subroutines:


● Allows the subroutine code to be called from multiple places
● Subroutine code may be (independently) tested and debugged
● If the subroutine task changes the change needs to be made only once
● Reduces unnecessary duplication
● Enables sharing of development between programmers

Benefits of using subtasks(structure chart):


1. Subtasks make the solution more manageable. They make the algorithm easier to follow
2. A subtask makes the problem easier to solve and design.
3. A subtask is useful when a part of the algorithm is repeated.
Types of maintenance:
Adaptive maintenance: changes due to change in requirements. These changes can affect the
program algorithm and its data structures.
Corrective maintenance: changes to correct identified errors
Perfective maintenance: modifying a program to improve performance or maintainability

Using identifier names such as I1, I2, and I3 is not a good practice because the names are not
meaningful. It is easy to use the wrong identifier names. This makes the program more difficult to
understand/debug/modify/test.

Methods of making code easier to understand:


● Meaningful variable names
● Indentation / white space / blank lines
● Capitalisation of keywords
Text files
File: A structure for the permanent storage of data
Features of using text files:
● Data from the arrays is written to the files before the program is terminated
● Data can then be read from the files when the program is run again
● No need to (re-)enter the data manually

Advantage of using text files rather than arrays: The data is retained when the program is
terminated.

Reading from a text file


The following pseudocode statements provide facilities for reading from a file:

OPENFILE <filename> FOR READ // open file for reading


READFILE <filename>, <stringVariable> // read a line of text from the
file
CLOSEFILE <filename> // close file

Appending to a text file


Sometimes we may wish to add data to an existing file rather than create a new file.
The following pseudocode statements provide facilities for appending to a file:

OPENFILE <filename> FOR APPEND // open file for append


WRITEFILE <filename>, <stringValue> // write a line of text to the file
CLOSEFILE <filename> // close file

The end-of-file (EOF) marker


This is a function will return FALSE if the end of the file
is not yet reached and will return TRUE if the end-of-file marker has been reached.

In pseudocode we call this function EOF().


If it is possible that the file contains no data, it is better to use the construct WHILE NOT EOF().
For example, the following pseudocode statements read a text file and output its contents:
OPENFILE "Test.txt" FOR READ
WHILE NOT EOF("Test.txt") DO
READFILE "Test.txt", TextString
OUTPUT TextString
ENDWHILE
CLOSEFILE "Test.txt"
Stacks: Abstract data structure that works with concept LIFO(last in first out)

Queues:A stract data structure that works with concept FIFO(first in first out)

Linked Lists:
Node: an element of a list
Pointer: a variable that stores the address of the node it points to
Null pointer: a pointer that does not point at anything
Start pointer: a variable that stores the address of the first element of a linked list

Identifier Table contains


1- names of identifiers used
2- The data type of the identifier
3- values of any constants used

You might also like