Cs Practical Full
Cs Practical Full
Abstraction: it is the process of extracting information that is essential, and ignoring anything
irrelevant, for a solution
What is Abstraction?
It is a part of computational thinking that enables computer scientists to develop clear models
for a complex problem. It encourages the development of simple models for a specific purpose
by removing unnecessary characteristics from the model.
Stages of Abstraction:
● Stage 1: identify the purpose of the model of the situation that is to be built
What is Decomposition?
It is a part of computational thinking that enables computer
scientists to break a complex problem into smaller parts which
can be further broken
2. Flowchart: shows diagrammatically the steps required for a task and in which order
they’re executed using a set of symbols linked together with flow lines
Identifiers:
Each line of pseudocode is usually a single step in an algorithm. Identifier names used in
pseudocode should be meaningful, and are usually case insensitive.
Note: programming languages may not have the same selection constructs as pseudocode, so
it’s important to be able to write a program that performs the same task as the solution in
pseudocode
To perform iteration using FOR, Repeat Until, and WHILE loops:
A FOR loop have a fixed number of repetitions, the STEP increment is an optional expression
that must be a whole number
REPEAT
OUTPUT “Please enter a positive number”
INPUT Number
UNTIL Number > 0
WHILE, REPEAT and IF statements make use of comparisons to decide whether statements
within a loop are repeated. The comparisons make use of relational and logical operators (AND,
OR, NOT).
- The outcome of the comparisons are either TRUE or FALSE
REPEAT
OUTPUT “Please enter a positive number less than fifty”
INPUT Number
UNTIL (Number > 0) AND (Number < 50)
A simple algorithm can be clearly documented using statements. A more realistic algorithm to
find the average of a number of integers input would include:
- Checks that all the values input are whole numbers
- The number input to determine how many integers are input are positive
For writing in structured English, the wording needs to be unambiguous and easy to understand.
For Example:
Structured English
1 Enter time taken to run marathon in hours, minutes and seconds
Identifier Table
Identifier Name Description
Flowcharts are diagrams showing the structure of an algorithm using an agreed set of symbols
For Example:
This is an example of a flowchart of an algorithm that can be used to check an exam grade
9.2.5 - Stepwise Refinement
When an algorithm is written to solve a more complex problem, decomposition is used to break
the problem down into smaller and manageable parts.
- This is called stepwise refinement
Stepwise Refinement: the practice of dividing each part of a larger problem into a series of
smaller parts and so on
Boolean Logical values, True (1) and False (2) BOOLEAN bool
10.1.2 - Records
Composite Data Type: a data type constructed using several of the basic data types available in
a programming language
Record: it is a composite data type formed by the inclusion of several related item of different
data types
Format Example
TYPE TYPE
<Typename> TbookRecord
DECLARE <identifier> : <data type> DECLARE title : STRING
DECLARE <identifier> : <data type> DECLARE author : STRING
DECLARE <identifier> : <data type> DECLARE publisher : STRING
ENDTYPE DECLARE noPages : INTEGER
DECLARE fiction : BOOLEAN
ENDTYPE
10.2.1 - 1D Arrays
Array: it is a data structure containing several elements of the same data type. The elements
can be accessed using the same identifier name
What is a 1D Array?
It can be referred to as a list
10.2.2 - 2D Arrays
What is a 2D Array?
It can be referred to as a table, with rows and columns
Linear Search: it is a method for finding items stored in an array. Each element of the array is
checked from lower to upper bound until the item is found.
Algorithm Explained:
Computer programs store data in a file. Every file is identified by its filename
Pseudocode for opening a file: OPEN <file identifier> FOR <file mode>
The first item added to a stack is The first item added to a queue is A new item is always added to
the last item removed the first item removed the start of the list
Note:
- Items can be added to the stack (push) and removed (pop)
- Items can be added to the queue (enqueue) and removed (dequeue)
Use of Pointers:
Stacks, queues and linked lists make use of pointers to manage their operations. Items in
stacks and queues are added at the end. Linked lists use an ordering algorithm to order them in
ascending or descending order.
Front Pointer: points to the first item in the stack Front Pointer: points to the first item in the queue
Top Pointer: points to the last item in the stack Rear Pointer: points to the last item in the queue
The pointers are equal when there’s only one item The pointers are equal where there’s only one
in the stack item in the queue
The value of the base pointer remains the same during stack operations. A stack can be
implemented using an array and a set of pointers
Items can be removed from any position in the linked list, the empty positions in the array must
be managed as a empty linked list, called the heap
For Example
The startPointer = –1, as the list has no elements. The heap is set up as a linked list ready for
use.
The startPointer is set to the element pointed to by the heapPointer where 37 is inserted. The
heapPointer is set to point to the next element in the heap by using the value stored in the
element with the same index in the pointer list. Since this is also the last element in the list the
node pointer for it is reset to –1.
The startPointer is changed to the heapPointer and 45 is stored in the element indexed by the
heapPointer. The node pointer for this element is set to the old startPointer. The node pointer for
the heapPointer is reset to point to the next element in the heap by using the value stored in the
element with the same index in the pointer list.
Section 11 - Programming
Constant: it is a named value that can’t change during the execution of a program
Variable: it is a named value that can’t change during the execution of a program
Note: all variables and constants should be declared before use. Constants will always be
assigned a value when declared
Good Practices:
1. Assigning values to any variables that are used
2. Creating a identifier list and check that every variable has been declared and assigned a
value
Question: write an algorithm using pseudocode to calculate and output the volume and surface
area of a sphere for any radius that is input
Solution:
Table 11.1 shows the declaration of some of the constants and variables
Table 11.2 shows examples of input statements
Table 11.4 shows how to calculate the volume and surface area
Table 11.5 shows how to output the results
Complete Program:
Library Routines: a tested and ready-to-use routine available in the development system of a
programming language that can be incorporated into a program
A programming language IDE includes a standard library of function and procedures as well as
an interpreter/compiler
11.2.1 - CASE and IF
The diagram above shows that choices can be made using a condition based on:
An Example:
11.2.2 - Loops
Loops: enables sections of code to be repeated as required. There are different types of loops:
- A count-controlled loop for…next
- A post condition loop repeat…until
- A precondition loop while…do…endwhile
Procedure: a small section of a program that performs a specific task. It is defined once and can
be called many times
Format Example
CALL stars
CALL stars(7)
Table 11.12 shows how to define a procedure with a parameter in 3 programming languages
1. By Value: if a variable is used, the value of the variable can’t be changed within the
procedure
2. By reference: the value of the variable passed as the parameter can be changed by the
procedure
Defining a procedure with parameters passed by reference in pseudocode:
● Format: PROCEDURE <identifier> (BYREF <parameter 1> : <datatype> <statements>
ENDPROCEDURE
CALL celsius(myTemp)
11.3.2 - Functions
Functions: it is a block of organized, reusable code that is used to perform a single, related
action
Defining a function without parameters in pseudocode Defining a function with parameters in pseudocode
FUNCTION <identifier> RETURNS <data type> FUNCTION <identifier> (<parameter 1> : <datatype>)
<statements> RETURNS <data type>
ENDFUNCTION <statements>
ENDFUNCTION
Note: RETURN is used as one of the statements in a function to specify the value to be
returned. It is usually the last statement in the function
Section 12 - Software Development
Program Development Life Cycle: the process of developing a program set out in the 5 stages
of analysis, design, coding, testing and maintenance
Analysis: the problem needs to be clearly defined so that the solution can be found
(requirements specification). This stage involved a feasibility study, investigation and fact finding
Design: this stage requires the programmer to know what to do, tasks to be completed, how
each task is performed and work together. This is documented using structure charts and etc
Coding: this stage involves writing the program using a programming language
Testing: this stage involves testing the program using different sets of data to test if the program
works the way it is intended
Maintenance: this stage involves maintaining the program, which can be done by correcting any
errors or improving the functionality
12.1.3 - Different Development Life Cycles
The Waterfall Model: this model works by making sure each stage is completed before moving
to the next one.
- Used for small projects, which have known requirements
The Iterative Model: this model works by developing a simple subset of the requirements, then
enhances the model and runs the cycle again. The cycle is repeated until the full system is
developed.
- Used for projects which have major requirements but some details change
● Incremental development ● Easier to test and ● Not suitable for short projects
● Working programs produced debug small programs ● Needs good planning
● High customer involvement ● More flexible ● Whole system needs to be
● Customers involved at defined at the start
each iteration
Rapid Application Development (RAD): it is a development cycle that develops different parts of
requirements in a parallel manner, using different teams.
- Used for complex projects that need to be developed in a short time
- Prototyping is used to show customers for early feedback
Structure Chart: it is a modelling tool in program design used to decompose a problem into a set
of sub-tasks
- It shows the hierarchy of different modules and how they connect/interact
Figure 12.6 - A structure chart for converting from Fahrenheit to Celsius using selection
Figure 12.7 - A structure chart for converting from Farenheit to Celsius using iteration
Converting from Structure Chart to Pseudocode:
Figure 12.8 - A structure chart for calculating volume and surface area
Step 3: the “calculate volume” and “calculate surface area” modules needs to be defined as
functions
Step 4: the input and output modules are defined as procedures
Finite State Machine (FSM): it is a mathematical model of a machine that can be in one of a
fixed set of possible states. One state is changed to another by an external input (transition)
State Transition Diagram: a diagram showing the behavior of a finite state machine
Characteristics of a State Transition Diagram State Transition Diagram for unlocking a door
● States are represented as nodes (circles) 1. Locked and waiting for the input of the first digit
● Transitions are represented as interconnected 2. Waiting for the input of the second digit
arrows 3. Waiting for the input of the third digit
● Events are represented as labels on arrows 4. Unlocked
● Conditions can be specified in square brackets
after the event label
● The initial state is indicated by an arrow with a
black dot
● A stopped state is indicated by a double circle
Syntax Errors; they are errors in the grammar of a source program. Syntax errors need to be
corrected before they are executed
Logic Errors: they are errors in the logic of a program, meaning the program doesn’t do what it
is supposed to do. They are found when the program is tested
Run-Time Errors: occurs when the program is executed. The program may halt or go into infinite
loop, which has to be stopped by brute force
● Step 2: a test plan is drawn showing all the stages of testing and every test that will be
performed
Note: there are several formal methods of testing used to ensure that a program is robust and
works to the required standard
Program Testing in Design Stage:
Pseudocode is written, which can be tested using a dry-run, where the developer works through
a program and documents the results using a trace table
Walkthrough: it is a formal version of a dry run using predefined test cases. It is where another
member of the development team dry-runs the pseudocode
During the program development and testing, each module is tested. Test plans are set out as a
table:
Types of Test Data:
Types of Testing:
- Whitebox Testing: testing of how each procedure works including testing the structure
and logic of every program module
- Blackbox Testing: testing a module’s inputs and outputs
- Integration Testing: testing of separate modules to ensure that they work together
- Stub Testing: use of dummy modules for testing purposes
➢ Corrective Maintenance: used to correct any errors that appear during use
➢ Perfective Maintenance: used to improve the performance of a program during its use
➢ Adaptive Maintenance: used to alter a program so it can perform any new tasks required
by the user