You are on page 1of 29

SECTION 2

Chapter 7

7.1 The program development life cycle

A series of steps or phases that software goes through from its initiation to completion. The
cycle is divided into 4 phases -

1) Analysis
A process of investigation, leading to the specification of what the program is required to do. It
uses abstraction and decomposition tools.
- Abstraction: the key elements required for the solution of the program are kept and any
unnecessary details and information that are not required is discarded.
- Decomposition: a complex problem is broken down into smaller parts, which can then be
subdivided into even smaller parts. That can be solved more easily.

2) Design
Uses the program specification from the analysis stage to show how the program should be
developed.

3) Coding
The writing of a program or suit of programs.

4) Testing
Systematic checks done on a program to make sure it works under all conditions.

7.2 Computer systems, sub-systems and decomposition

A computer system is made up of software, data, hardware, communications and people; each
computer system can be divided up into a set of sub-systems.
Each sub-system can be further divided into sub-systems and so on until each sub-system just
performs a single action.

This division can be shown using top-down designs.

Any problem can be decomposed into the following components:

» 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.

Methods to design and construct a solution to a problem-


● Structure diagrams: shows how a computer system solution can be divided into
sub-systems with each level giving a more detailed breakdown. If necessary, each
sub-system can be further divided.

● Flowcharts: shows diagrammatically the steps required to complete a task and the order
that they are to be performed. These steps, together with the order, are called an
algorithm. standard flowchart symbols include -

● Pseudocodes: a simple method of showing an algorithm; it describes what the algorithm


does by using English keywords that are very similar to those used in a high-level
programming language but without the strict syntax rules

Advantages
- It is similar to normal program code, so can be converted into program easily.
- It doesn’t require any special graphics software
- Is good to use when program is very lengthy
- Can be written quickly
- Takes up less space than Flowchart
- Can be easily edited. Easy to rewrite any part of it.

Disadvantages
- More technical than flowchart
- Can be harder to understand than flowchart.
The pseudocode for conditional statements

1) IF…THEN…ELSE…END IF
For an IF condition the THEN path is followed if the condition is true and the
ELSE path is followed if the condition is false. There may or may not be an ELSE path. The end
of the statement is shown by ENDIF.

There are different ways that an IF condition can be set up

>> Use of a Boolean variable

IF Condition A
THEN
PRINT “true”
ELSE
PRINT “false”
END IF

>> Making comparison

IF Condition A > Condition B


THEN
PRINT “...”
ELSE
PRINT “…”
END IF

>> Nested IF

IF Condition A > Condition B AND Condition C


THEN
PRINT “…”
ELSE
IF Condition B > Condition A AND Condition C
THEN
PRINT “…”
ELSE
PRINT “…”
END IF
END IF
2) CASE OF…OTHERWISE….END CASE
For a CASE statement the value of the variable decides the path to be taken.
Several values are usually specified. OTHERWISE is the path taken for all other values. The
end of the statement is shown by ENDCASE.

CASE Of Choice
1: Condition A
2: Condition B
3: Condition C
OTHERWISE PRINT “…”
END CASE

The pseudocode for iteration

1) FOR..TO..NEXT. (count-control loop)


- iterates a specific number of times
- Uses loop variable with a specified range

FOR Counter <- 1 to 10


PRINT “…”
NEX Counter

2) WHILE…DO…ENDWHILE (per-condition loop)


- repeats as long as a condition is true
- Condition is checked before entering the loop

Counter <- 0
WHILE Counter < 10 DO
PRINT “…”
Counter <- Counter + 1
ENDWHILE

3) REPEAT…UNTIL (post-condition loop)


- repeats until a condition becomes true
- Condition is checked before after each iteration

Counter <- 0
REPEAT
PRINT “…”
Counter <- Counter + 1
UNTIL Counter > 9
7.3 Explaining the purpose of an algorithm

An algorithm sets out the steps to complete a given task. This is usually shown as a flowchart or
pseudocode, so that the purpose of the task and the processes needed to complete it are clear
to those who study it.

7.4 Standard methods of solution

Standard methods used in algorithm

● Totalling

Total <- 0
FOR Counter <- 1 TO 10
INPUT X
Total <- Total + X
NEXT Counter
PRINT Total

● Counting

Count <- 0
FOR Counter <- 1 TO 10
IF Condition A
THEN
Count <- Count + 1
END IF
NEXT Counter
PRINT Count

● Maximum
(In maximum, store the minimum value)

Maximum <- 0
FOR Counter <- 1 TO 10
INPUT X
IF X > Maximum
THEN
Maximum <- X
END IF
NEXT Counter
PRINT Maximum
● Minimum
(In minimum, store the maximum value)

Minimum <- 0
FOR Counter <- 1 TO 10
INPUT X
IF X < Minimum
THEN
Minimum <- X
END IF
NEXT Counter
PRINT Minimum

● Linear Search
(Inspects each time in a list in turn to see if the item matches the value searched for)

INPUT N
COUNT <- 0
FOR I <- 1 TO 10
IF N = [value to be searched for]
THEN
COUNT <- COUNT + 1
END IF
NEXT I
IF COUNT > 1
THEN
PRINT “present”
ELSE
PRINT “not present”
END IF

● Bubble Sort
(Sorts items in a more meaningful order)

FOR I <- 1 TO N
FOR J <- 1 TO N - I
IF A[J] > A[J+1]
X <- A[J]
A[J] <- A[J+1]
A[J+1] <- X
END IF
NEXT J
NEXT I
7.5 Validation and Verification

Validation
The automated checking by a program that the data is reasonable before it is entered

1) Range Check
checks that the value of a number is between an upper value and a lower value

Example: to check that X is between 0 to 100

REPEAT
INPUT X
IF X < 0 OR X > 100
THEN
PRINT “value is out of range, please re-enter”
END IF
UNTIL X >= 0 OR X <= 0

2) Length Check
checks that the data entered contains an exact number of characters or a reasonable number of
characters

Example: to check that X has between 1 to 10 characters

REPEAT
INPUT X
IF LENGTH(X) < 1 OR LENGTH(X) > 10
THEN
PRINT “value too short or too long, please re-enter”
END IF
UNTIL LENGTH(X) >= 1 OR LENGTH(X) <= 10

3) Type Check
checks whether the data entered if of a specific data type

Example: to check that X is an integer

REPEAT
INPUT X
IF X <> DIV(X,1)
THEN
PRINT “please enter a whole number”
END IF
UNTIL X = DIV(X,1)

4) Presence Check
checks to ensure some data has been entered and the value has not been left blank

Example:
REPEAT
INPUT X
IF X = “”
THEN
PRINT “please enter a value”
END IF
UNTIL X <> “”

5) Format Check
checks that the characters entered conform to a predefined pattern

Example: to check that X is in format XXX9999

INPUT X
IF LENGTH(X) = 7
THEN
IF IS_APLHABETIC(SUBSTRING(X,1,3)) AND IS_NUMERIC(SUBSTRING(X,4,7))
THEN
PRINT “valid”
ELSE
PRINT “invalid format, please re-enter”
END IF
ELSE
PRINT “please enter a 7-digit value”
END IF

6) Check Digit

Verification
Checks that the data has been accurately copied from one source to another

1) Double Entry
Data is entered twice, sometimes by different operators. The computer system compares both
entries and if they are different outputs an error message requesting that the data should be
entered again
2) Screen/Visul Check
A manual check completed by the user who is entering the data. When data is entry is complete
the data is displayed on the screen and the user is asked to confirm that it is correct before
continuing.

3) Proofreading
A user enters the data. Another user reads it to confirm that the data entered is correct.

7.6 Test Data

A set of test data is all the items of data required to work through a solution.

1) Normal Data
Normal data should be used to work through the solution to find the actual results) and see if
they are the same as the expected result (s).

2) Abnormal Data
Test data should be chosen that would be rejected by the solution as not suitable, if the solution
is working properly.

3) Extreme Data
The largest and smallest values that normal data can take.

4) Boundary Data
At each boundary two values are required: one value is accepted and the other value is
rejected.

7.7 Trace tables to document the dry runs of algorithms

A trace table can be used to record the results from each step in an algorithm; it is used to
record the value of an item (variable) each time that it changes. The manual exercise of working
through an algorithm step by step is called a dry run.

7.8 Identifying errors in an algorithm

Trace tables and test data can be used to identify and correct errors.

7.9 Writing and amending algorithms

There are a number of stages when producing an algorithm for a given problem:
1) Make sure that the problem is clearly specified - the purpose of the algorithm and the
tasks to be completed by the algorithm.

2) Break the problem down into sub-problems; if it is complex, you may want to consider
writing an algorithm for each sub-problem.

3) Decide on how any data is to be obtained and stored, what is going to happen to the
data and how any results are going to be displayed

4) Design the structure of your algorithm using a structure diagram.

5) Decide on how you are going to construct your algorithm, either using a flowchart or
pseudocode.

6) Construct your algorithm, making sure that it can be easily read and understood by
someone else.

7) Use several sets of test data (Normal, Abnormal and Boundary) to dry run your
algorithm and show the results in trace tables, to enable you to find any errors.

8) If any errors are found, correct them and repeat the process until you think that your
algorithm works perfectly.

Chapter 8

8.1 Programming Concepts

Constructs of a Program
● Data use – variables, constants and arrays
● Sequence – order of steps in a task
● Selection – choosing a path through a program
● Iteration – repetition of a sequence of steps in a program
● Operators use arithmetic for calculations and logic and Boolean for decisions.

Variables and Constants

● Variables - A variable in a computer program is a named data store that contains a value
that may change during the execution of a program.

● Constants - A constant in a computer program is a named data store that contains a


value that does not change during the execution of a program.
Basic Data Types

● integer - a positive or negative whole number that can be used with mathematical
operators

● real - a positive or negative number with a fractional part. Real numbers can be used
with mathematical operators

● char - a variable or constant that is a single character

● string - a variable or constant that is several characters in length. Strings vary in length
and may even have no characters (known as an empty string); the characters can be
letters and/or digits and/or any other printable symbol (If a number is stored as a string
then it cannot be used in calculations.)

● Boolean - a variable or constant that can have only two values TRUE or FALSE.

Declaration of variables and constants

1. Variable Declaration:

To declare a variable in pseudocode, you typically use a keyword like DECLARE followed by the
variable name. Example:

DECLARE <variable names> AS <data type>

2. Constant Declaration:
In pseudocode, constants are often declared using a keyword like CONSTANT followed by the
constant name and its value. Example:

CONSTANT <constant name> = <constant value>

Sequence
The order in which the steps of a program are executed

Selection
Allowing selection of different paths through the steps of a program.

Iteration
A section of program code that can repeat itself under certain conditions
Counting
Keeping tract of the number of terms an action is performed

Totalling
Keeping a total that values are added to

String handling

● Length - finds the number of characters in a string

Example: finding the number of character in ‘Computer Science’


LENGTH(“Computer Scince”)

Note - text to be written in quotes, variable to be written without quote

● Substring - extracting part of a string


Example: extracting the word “Science” from “Computer Science”
SUBSTRING(“Cmputer Scince”, 10,7

Note - space is considered as a character in programming

● Upper - convert all letters in a string to upper case


Example: converting the word “Computer Science” to upper case
UCASE(“Computer Scince”)

● Lower - convert all letters in a string to lower case


Example: converting the word “Computer Science” to lower case
LCASE(“Computer Scince”)

Operators
A special character or word in a programming language that identifies an action to be
performed
● Arithmetic Operator - an operator used to perform calculations

Operator Action

+ add

- subtract

* multiply

/ divide

^ raise to the power

MOD reminder division

DIV integer division (quotient without decimal)

● Logical Operator - an operator used to decide the path to take through. Program
if the expression formed is true or false

Operator Action

> greater than

< less than

= equal to

>= greater than and equal to

<= less than and equal to

<> not equal

● Boolean Operator - an operator that is used with logical operators to form more
complex expressions

Operator Action
AND both ture

OR either true

NOT not true

Nesting
The inclusion of one type of code construct inside another

Procedures and Functions

When writing an algorithm, there are often similar tasks to perform that make use of the
same groups of statements. Instead of repeating these statements and writing new
code every time they are required, many programming languages make use of
subroutines, also known as named procedures or functions. These are defined once
and can be called many times within a program.

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.

● Procedures

- Syntax of procedure without parameters:


PROCEDURE ProcedureName
[commands]
END PROCEDURE

CALL ProcudureName
- Here is an example of a procedure without parameters in pseudocode:
PROCEDURE Stars
PRINT “*******”
ENDPROCEDURE

- The procedure can be called in the main part of the program as many times as it
is required in the following way:
CALL Stars

- Syntax of procedure with parameters:


PROCEDURE ProcedureName (ParameterName : ParameterDatatype)
[Commands]
ENDPROCEDURE

CALL ProecdureName (ParameterValue)

- We can add parameters to a procedure:


PROCEDURE Stars (Number : INTEGER) //parameter with data type
FOR I <- 1 TO Number
PRINT “*”
NEXT I
ENDPROCEDURE

- The procedure with parameters are called like this: (assuming we want to print 7
stars)
CALL Stars(7)

● Functions

- Syntax of function with parameters


FUNCTION FunctionName (ParameterName : ParameterDatatype) RETURNS
DataTypeOfValueReturned
[Commands]
RETURN ValueToBeReturned
ENDFUNCTION

- Here is an example of a function to convert a temperature from fahrenheit to


celsius:
FUNCTION Celsius (Temperature : REAL) RETURNS REAL
RETURN (Temperature - 32)/1.8
END FUNCTION

- To call a function in the main program we can assign the return value into a
variable :
Temp <- Celsius(Temp)

When procedures and functions are defined, the first statement in the definition is a
header, which contains:
» the name of the procedure or function
» any parameters passed to the procedure or function, and their data type
» the data type of the return value for a function.

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.

Library Routines

- Programming language development systems often provide library routines that can be
readily incorporated into programs.
- Library routines are pre-tested and ready for use, making programming tasks easier.
- Integrated Development Environments (IDEs) typically include a standard library of
functions and procedures.
- Standard library routines perform various tasks, including string handling.

Library Routines required for IGCSE-


● MOD – returns the remainder of a division
● DIV – returns the quotient (i.e. the whole number part) of a division
● ROUND – returns a value rounded to a given number of decimal places
● RANDOM – returns a random number.
Examples:
● Value1 <--- MOD(10,3) returns the remainder of 10 divided by 3
● Value2 <---- DIV(10,3) returns the quotient of 10 divided by 3
● Value3 <--- ROUND(6.97354, 2) returns the value rounded to 2 decimal places
● Value4 <--- RANDOM() returns a random number between 0 and 1 inclusive

8.2 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.The position of each element in an array is
identified using the array’s index.

● One-dimensional arrays- it can be referred to as a list.

a one-dimensional array is declared in pseudocode as:


» the name of the array
» the first index value
» the last index value
» and the data type

Example:
DECLARE VariableName : ARRAY[1:NumberOfRows] OF DataType

● Two-dimensional arrays - it can be referred to as a table

a two-dimensional array is declared in pseudocode as:


» 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

Example:
DECLARE VariableName : ARRAY[1:NumberOfRows, 1:NumberOfColumns] OF DataTya

Note: When using a 2-D array with loop structures, there needs to be two loops, one for each
index.

Sample Array Questions -


1) Write a pseudocode to calculate the sum of each row
DECLARE X : ARRAY[1:10 , 1:5]
FOR I <- 1 TO 10
SUM <- 0
FOR J <- 1 TO 5
SUM <- SUM + X [I][J]
NEXT J
PRINT SUM
NEXT I

2) Write a pseudocode to calculate the sum of each column


DECLARE X : ARRAY[1:10 , 1:]
FOR J <- 1 TO 5
SUM <- 0
FOR I <- 1 TO 10
SUM <- SUM + X [I][J]
NEXT I
PRINT SUM
NEXT J

8.3 File Handling

purpose of storing data in a file

computer programs store data that will be required again in a file. While any 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).

using files

files can be opened in the following modes-

1) read
Read data from the file

Syntax-
OPEN <file name> FOR <file mode>
READ <variable>
CLOSEFILE <file name>

Example -
FOR I <- 1 TO 10
OPENFILE “file.txt” FOR READ
X <- file.txt
READ X
NEXT I
CLOSE FILE “file.txt”

NOTE: ‘CLOSEFILE’ statement using FOR..TO.. NEXT Loop can only be used when we know
the number of elements. If the number of elements is unknown we use the statement ‘EOF’
which stands for End Of File using a REPEAT…UNTIL Loop.

Example-
REPEAT
OPENFILE “file.txt” FOR READ
X <- file.txt
READ X
UNTIL EOF “file.txt”

2) Write
writes data to the file, any existing data stored in the file will be overwritten.

Syntax-
OPEN <file name> FOR <file mode>
WRITE <variable>
CLOSEFILE <file name>

Example -
FOR I <- 1 TO 10
OPENFILE “file.txt” FOR WRITE
X <- file.txt
WRITE X
NEXT I
CLOSE FILE “file.txt”

Here are examples of writing a line of text to a file and reading the line of text back from the file.
The pseudocode algorithm has comments to explain each stage of the task.
Chapter 9

9.1 databases

A database is a structured collection of data that allows people to extract information in a way
that meets their needs. That data can include text , numbers, pictures and anything that can be
stored in a computer. A single database contains only one table

Why do we need a database?


● To store data about people, things, and events.
● Any modifications or additions need to be made only once, ensuring data
consistency.
● All users access and utilize the same set of data, promoting uniformity.
● Relational databases store data in a non-repetitive manner, eliminating
duplication.
Components of a Database
● Tables - a collection of related records in a database
● Record - a collection of fields that describe one item
● Field - a database table

the number of records in a database may vary as new records can be added and deleted from a
table as required. The number of fields in a database is fixed so each record contains the same
number of fields.

Validation in databases
● Database management software automatically provides some validation checks,
while others need to be set up by the developer during construction.

Database data types

A data type classifies how the data is stored, displayed and the operations that can be
performed on the stored value.

Primary Key
A field in a database that uniquely identifies a record. It does not contain any duplicate data.

Structured Query Language - SQL


● Structured Query Language (SQL) is the standard language for writing scripts to
retrieve valuable information from databases.
● By using SQL, we can learn how to retrieve and display specific information
needed from a database.
SQL Scripts
● An SQL script is a collection of SQL commands that are used to perform a
specific task, often stored in a file for reusability.
● To comprehend SQL and interpret the output of an SQL script, practical
experience in writing SQL scripts is necessary.

Select Statements:
SELECT (fieldsname)
FROM (tablesname)
WHERE (condition)
ORDER BY (sortingcondition) ;

Selecting Sum of values in a table:


SELECT SUM ( fieldsname )
FROM (tablesname)
WHERE (condition)
ORDER BY (sortingcondition) ;

Counting the number of records where the field matches a specified condition
SELECT COUNT ( fieldsname )
FROM (tablesname)
WHERE (condition)
ORDER BY (sortingcondition) ;

=> ORDER BY Field1, Field2, etc. – this specifies a sort in ascending or alphabetical
order starting with the first field.
=> ORDER BY Field1, Field2 DESC – this specifies a sort in descending or reverse
alphabetical order starting with the first field.

Note: ORDER BY is not necessary to add. It has to be only added if required!


Chapter 10

10.1 standard logic gate symbols

Truth Tables

Truth tables are used to trace the output from a logic gate or logic circuit. The NOT gate is the
only logic gate with one input; the other five gates have two inputs.

Although, each logic gate can only have two inputs, logic circuit can have two or more inputs.
10.2 The function of the six logic gates
10.3 logic circuits,logic expressions, truth tables and problem
statements

● Problem Statement
can be used to create a logic expression, logic circuit and a truth table
● Logic or Boolean Expression
can be used to create logic circuit and complete a truth complete

● Truth Table
can be used to create a logic expression and logic circuit
● Logic Circuit
can be used to create a Boolean expression and truth table

This logic expression can be used to create a truth table as shown above

You might also like