[go: up one dir, main page]

0% found this document useful (0 votes)
44 views66 pages

C.S Chapter-7

Uploaded by

rlokeshreddy6060
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
44 views66 pages

C.S Chapter-7

Uploaded by

rlokeshreddy6060
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 66

By – r.

Lokesh Reddy
Gr-9 explorers
In this document, you will learn
about:
★ The stages in the program
development cycle:
– Analysis
– Design
– Coding and Iterative Testing
– Testing
★ Computer systems and sub-
systems.
★ Problem decomposition into
component parts.
★ Methods used to design and
construct solutions to
problems.
★ The purpose of an algorithm
and the processes involved in
it.
★ Standard methods of
solution:
– Linear search
– Bubble sort
– Totalling and Counting
– Finding Average, Maximum,
Minimum
★ Validation checks when data
is input.
★ Verification checks when
data is input.
★ Use of different types of test
data including:
– Documentation of a dry run
using a trace table
★ Writing, amending,
identifying, and correcting
errors Flowcharts,
Pseudocode, Programs

The stages in the program


development cycle
1.Analysis:
Before solving a problem, it
needs to be clearly defined so
everyone understands what's
required. This is called the
"requirements specification" and
ensures all objectives and
features are clear. The analysis
stage uses abstraction and
decomposition tools to identify
exactly what is required from
the program.
Abstraction:
It keeps the key elements
required for the solution to the
problem and discards any
unnecessary details and
information that is not required.
For example, a map only shows
what is required for travelling
from one place to another.
Different methods of transport
will require different types of
maps.
Decomposition:
It breaks down a complex
problem into smaller parts,
which can then be subdivided
into even smaller parts, that can
be solved easily. Any daily task
can be divided into its
constituent parts. For example,
getting dressed:
» Select items to wear.
» Remove any clothes being
worn.
» Put selected items on in order.
2. Design:
The program specification from
the analysis stage guides how
the program should be
developed. After the design
stage, the programmer knows
all tasks, how to perform them,
and how they fit together. This
is documented using structure
charts, flowcharts, and
pseudocode.
3. Coding and Iterative
testing:
The program or set of programs
is developed. Each module of
the program is written using a
suitable programming language
and then tested to see if it
works.
Iterative testing means that
modular tests are conducted,
code amended, and tests
repeated until the module
performs as required.

4.Testing:
The completed program or set
of programs is run many times
with different sets of test data.
This ensures that all the tasks
completed work together as
specified in the program design.

Computer systems and sub-


systems
A computer system consists of
software, data, hardware,
communications, and people. It
can be broken down into sub-
systems, which are further
divided until each performs a
single action. Computer
systems vary in size, from large
to small, and people interact
with many systems daily, often
without realizing it.
To understand a computer
system, it's divided into sub-
systems using top-down
design, shown in structure
diagrams. Each sub-system is
developed as a sub-routine,
illustrated by flowcharts or
pseudocode. Top-down design
breaks the system into smaller
sub-systems until each
performs a single action, a
process called stepwise
refinement. This approach
simplifies problem-solving and
is effective for both large and
small systems. For larger
systems, multiple programmers
can work on different sub-
systems simultaneously,
reducing development and
testing time.
Problem decomposition into
component parts
Any problem that uses a
computer system for its
solution needs to be
decomposed into its
component parts. The
component parts of any
computer system are:
» 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

Example: An Alarm app


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
Methods used to design
and construct solutions
to problems.
Solutions to problems need to
be designed and developed
rigorously. The use of formal
methods enables the process to
be clearly shown for others to
understand the proposed
solution. The following
methods need to be used by
IGCSE Computer Science
students:
-Structure diagrams
-Flowcharts
-Pseudocode
Structure diagrams
Structure diagrams can be
used to show top-down design
in a diagrammatic form.
Structure diagrams are
hierarchical, showing 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.
Example of structure diagram:

Flowcharts
A flowchart 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. Flowcharts
are an effective way to
communicate how the algorithm
that makes up a system or sub-
system works.
Symbols of Flowchart

Pseudocode
Pseudocode is a method of
outlining an algorithm in a way
that resembles programming,
but without the strict syntax
rules of a specific
programming language. It uses
plain English and keywords
similar to those in high-level
languages (like IF, WHILE,
FOR, ELSE, etc.), making it
easier to understand and
communicate how an algorithm
works. Data items, such as
variables and constants, are
named meaningfully, similar to
how they would be in real
code. The main purpose of
pseudocode is to provide a
clear, consistent description of
an algorithm, making it easy
for others to follow and later
translate into actual code.
The pseudocode in this book is
written in the following way to
match the pseudocode given in
the IGCSE Computer Science
syllabus and to help you
understand the algorithms more
easily:
» 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.

Assignment operator
A value is assigned to a variable
using the ← operator, with the
variable on the left and the
expression or value on the right.
The expression can be a single
value or a combination of values
using mathematical operators.
Mathematical Operators
Operator Action Example
+ Addition x←5+2
- Subtraction

x←5-
2
* Multiplicati
on
x←5*
2
/ Division x←5/2
(quotient)
MOD Modulus x ← 5 MOD
(remainder) 2
^ Exponentiat x←2^3
ion (power)
() Group x ← (5 + 3)
*
Conditional Statements:
When different actions are
performed by an algorithm
according to the values of the
variables, conditional
statements can be used to
decide which action should be
taken. There are two main
types of Conditional
Statements.
1.IF THEN ELSE
Conditional Statement
SYNTAX:
IF Condition
THEN
Statement
ELSE
Statement
ENDIF

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.
Example of pseudocode
using IF condition:
IF Age < 18
THEN
OUTPUT "Child"
ELSE
OUTPUT "Adult"
ENDIF
There are various methods
to structure an IF condition.
They are:
» Use of a Boolean variable
that can have the value
TRUE or FALSE.
»Comparisons made by
using comparison
operators, where
comparisons are made
from left to right.
IF Conditional Statement
has two types. They are:
❖ Nested IF Conditional
Statement
Syntax:
IF Condition
THEN
Statement
ELSE
IF Condition
THEN
Statement
ELSE
Statement
ENDIF
ENDIF
A nested if statement
refers to an if-else
structure in which one or
more if statements are
placed inside another if or
else block. This allows you
to check multiple
conditions sequentially,
where a secondary
condition is only evaluated
if a previous condition is
met
Q. Write a pseudocode to check
whether the entered percentage
mark is failed, pass or invalid
mark.
OUTPUT "Please enter a mark”
INPUT Mark
IF Mark < 0 OR Mark > 100
THEN
OUTPUT "Invalid Mark"
ELSE
IF Mark > 49
THEN
OUTPUT "Pass"
ELSE
OUTPUT "Fail"
ENDIF
ENDIF

❖ IF ELSEIF ELSE Conditional


Statement
Syntax:
IF Condition Then
Statement
ELSEIF
Statement
ELSE
Statement
ENDIF

An IF-ELSEIF-ELSE
conditional statement in
pseudocode is used to check
multiple conditions sequentially.
It executes a block of code
depending on which condition is
True. If none of the conditions
are true, the ELSE block is
executed.
Example of pseudocode
using IF ELSEIF ELSE
condition:
IF x > 10 THEN
OUTPUT "x is greater
than 10"
ELSEIF x = 10
OUTPUT "x is equal to 10"
ELSE
OUTPUT "x is less than
10"
ENDIF
Comparison operators:

2. CASE OF … OTHERWISE …
ENDCASE
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.
Syntax:
CASE OF < variable >
"Case 1": OUTPUT "Message"
"Case 2": OUTPUT "Message"
"Case 3": OUTPUT "Message"
"Case 4": OUTPUT "Message"
OTHERWISE, OUTPUT"
"Message

Example:
OUTPUT "Please enter your
grade"
INPUT Grade
CASE OF Grade
"A": OUTPUT "Excellent"
"B": OUTPUT “Good"
"C": OUTPUT "Average"
OTHERWISE
OUTPUT " Improvement is
needed "
ENDCASE
LOOPING OR iterating
STATEMENTS:
There are three types of looping
statements. They are:
1. FOR … TO … NEXT
SYNTAX:
FOR variable ← n1 TO n2
OUTPUT variable
NEXT variable
Example:
FOR counter ← 1 TO 10
OUTPUT “OAK
VALLEY”
NEXT counter
The number of iterations is
known.
It is an iterating statement
initializing of variable and
condition is done at once.
It is a pre - condition loop.
2. REPEAT … UNTIL
Syntax:
Variable ← 0
REPEAT
Statement
Variable ← Variable
+1
UNTIL Variable condition
Example:
counter ← 0
REPEAT
OUTPUT “OAK
VALLEY”
counter ← + 1
UNTIL counter > 10
1.Repeat until loop is an
iterating statement where the
initialization of the variable is
done separately.
2.The number of iterations is
not known.
3.It is posting condition loop
4.This loop iterates at least
once even if condition is false
3. WHILE … DO…
ENDWHILE
Syntax:
Variable ←0
WHILE Variable (condition) DO
Variable ←Variable+1
OUTPUT Variable
ENDWHILE
Example:
counter ←0
WHILE counter >10 DO
counter ← counter +1
OUTPUT counter
ENDWHILE
While loop is an iterating
statement where the
initialization of the variable is
done separately.
The number of iterations is
not known.
It is pre-condition loop.
This loop will not iterate at
least once even if condition is
false.

The purpose of an
algorithm and the
processes involved in it.
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
Standard methods of solution:
Linear search
A search checks if a value exists in
a list by systematically going
through each item. The standard
search method for IGCSE is the
linear search, which examines
each item in the list one by one to
see if it matches the target value.
Example of Pseudocode:
OUTPUT "Please enter name to
find " INPUT Name
Found ← FALSE
Counter ← 1
REPEAT
IF Name = Student Name
[Counter]
THEN
Found ← TRUE
ELSE
Counter ← Counter + 1
ENDIF
UNTIL Found OR Counter > Class
Size
IF Found THEN
OUTPUT Name, “found at
position
Counter, " in the list."
ELSE OUTPUT Name, " not
found." ENDIF

Bubble sort
Lists are more useful when items
are sorted in a meaningful order,
like alphabetical for names or
ascending/descending for
numbers. For IGCSE, you need to
understand the bubble sort
method. In a bubble sort, each
item is compared to the next, and
they are swapped if in the wrong
order. This process starts from
the first element and continues
until the next-to-last. After each
pass, the largest element is in the
correct position. The process
repeats, excluding the last sorted
element each time, until the
entire list is sorted or no swaps
are needed.
For example, the bubble sort
algorithm can be used to sort a
list of ten temperatures stored in
the array, Temperature [], into
ascending order. It could be
written in pseudocode as:
First ← 1
Last ← 10
REPEAT
Swap ← FALSE
FOR Index ← First TO Last - 1
IF Tempera [Index]>Tempera
[Index +1]
THEN
Temp ← Tempera [Index]
Tempera [Index]←Tempera
Index + 1]
Tempera [Index + 1] ←
Temp
Swap ← TRUE
ENDIF
NEXT Index
Last ← Last – 1
UNTIL (NOT Swap) OR Last = 1
Totalling
Totalling means keeping a total
that values are added to.
For example, keeping a
running total of the marks
awarded to each student in a
class.
Total ← 0
FOR Counter ← 1 TO Class Size
Total ←Total+ Student Mark
[Counter]
NEXT Counter

Counting
Keeping a count of the number
of times an action is performed
is another standard method.
For example, counting the
number of students that were
awarded a pass mark:
Pass Count ← 0
FOR Counter ← 1 TO Class Size
INPUT Student Mark
IF Student Mark > 50 THEN
Pass Count ← Pass Count +
1
NEXT Counter
Count ← Count + 1
Finding Maximum,
Minimum
Method -1
Finding the largest and
smallest values in a list are two
standard methods that are
frequently found in algorithms.
For example, finding the
highest and lowest mark
awarded to a class of students.
Maxmark ← 0
Minmark ← 100
FOR Counter ← 1 TO Class
Size
IF StudentMark[Counter] >
MaxMark
THEN
MaxMark ←
StudentMark[Counter]
ENDIF
IF
StudentMark[Counter]<MinMa
rk
THEN
MinMark ←
StudentMark[Counter]
ENDIF
NEXT Counter
MetHOD-2
If the largest and smallest
values are not known, an
alternative method is to set the
maximum and minimum values
to the first item in the list.
For example, using this method
to find the highest and lowest
mark awarded to a class of
students.
MaximumMark ←
StudentMark[1] MinimumMark
← StudentMark[1]
FOR Counter ← 2 TO ClassSize
IF StudentMark[Counter] >
MaxMark
THEN
MaxMark ←
StudentMark[Counter]
ENDIF
IF StudentMark[Counter] <
MinMark
THEN
MinMark ←
StudentMark[Counter]
ENDIF
NEXT Counter
Average
Calculating the average (mean) of
all the values in a list is an
extension of the totalling method.
For example, calculating the
average mark for a class of
students.
Total ← 0
FOR Counter ← 1 TO ClassSize
Total ← Total +
StudentMark[Counter]
NEXT Counter
Average ← Total / ClassSize
Validation checks
when data is input
Validation is the automated
process where a program
checks if the input data is
reasonable and acceptable
before storing it in a
system. If the data fails
validation, an error
message is displayed
explaining why it was
rejected, and the user is
prompted to enter the data
again.
There are many different
types of validation checks
including:
» Range checks
» Length checks
» Type checks
» Presence checks
» Format checks
» Check digits.
Range checks
A range check checks that the
value of a number is between
an upper value and a lower
value
EXAMPLE:
OUTPUT "Please enter the
student's marks "
REPEAT
INPUT Mark
IF Mark < 0 OR Mark > 100
THEN
OUTPUT "The student's
mark should be in the
range 0 to 100, please re-
enter the mark "
ENDIF
UNTIL Mark >= 0 AND Mark
<= 100

Length checks
Length checks
A length check checks either:
» That data contains an exact
number of characters or that the
data entered is a reasonable
number of characters.
For Example that a password
must be exactly eight characters
in length so that passwords with
seven or fewer characters or nine
or more characters would be
rejected, for instance:
OUTPUT "Please enter your
password of eight characters "
REPEAT INPUT Password
IF LENGTH(Password) <> 8
THEN
OUTPUT "Your password
must be exactly eight characters,
please re-enter "
ENDIF
UNTIL LENGTH(Password) = 8
Type checks
A type check checks that the data
entered is of a given data type.
For example, that the number of
brothers or sisters would be an
integer (whole number).
OUTPUT "How many brothers do
you have? "
REPEAT
INPUT No.ofBrothers
IF
No.ofBrother<>DIV(No.ofBrothe
rs, 1)
THEN
OUTPUT "This must be a
whole number, please re-enter"
ENDIF
UNTIL NumberOfBrothers =
DIV(NumberOfBrothers, 1)

Presence checks
A presence check checks to
ensure that some data has been
entered and the value has not
been left blank.
For example, an email address
for an online transaction must be
completed.
OUTPUT "Please enter your email
address"
REPEAT
INPUT EmailAddress
IF EmailAddress = ""
THEN
OUTPUT "*=Required "
ENDIF
UNTIL EmailAddress <> ""

Format checks
A format check checks that the
characters entered conform to a
pre-defined pattern.
Check digits
A check digit is the final digit
included in a code; it is calculated
from all the other digits in the
code. Check digits are used for
barcodes, product codes,
International Standard Book
Numbers (ISBN) and Vehicle
Identification Numbers (VIN).
Check digits are used to identify
errors in data entry caused by
mis-typing or mis-scanning a
barcode.
They can usually detect the
following types of error:
» an incorrect digit entered, for
example, 5327 entered instead of
5307
» transposition errors where two
numbers have changed order for
example 5037 instead of 5307
» omitted or extra digits, for
example, 537 instead of 5307 or
53107 instead of 5307
» phonetic errors, for example,
13, thirteen, instead of 30, thirty.

Verification checks when


data is input
Verification is checking that data
has been accurately copied from
one source to another .
For instance, input into a
computer or transferred from one
part of a computer system to
another.

Verification methods for


input data include:
» Double entry
For double entry the 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 is entered again.
» Screen/visual check.
A screen/visual check is a manual
check completed by the user who
is entering the data. When the
data entry is complete the data is
displayed on the screen and the
user is asked to confirm that it is
correct before continuing.

Use of different types of


test data including:
A set of test data is all the
items of data required to work
through a solution.
There are four different Types
of Test data:
❖ Normal data - Normal data
refers to valid and typical inputs
within the expected range that a
program should handle
correctly during testing
❖ Abnormal / erroneous data -
Abnormal data is invalid input
used in testing to see how a
program handles unexpected or
incorrect values.
❖ Extreme data - Extreme data
refers to input values at the very
edge of the acceptable range. It
is used in testing to ensure a
program handles the maximum
and minimum limits correctly.
❖ Boundary data - Boundary
data refers to values at the
edges just inside and just
outside the valid range. It is
used in testing to check how a
program handles values near
the limits of acceptable input.
EXAMPLE oF test data of a
range 1 - 100 :

Documentation of a dry run


using a trace table
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.
A trace table is set up with a
column for each variable and a
column for any output. Test data
is then used to dry run the
flowchart and record the results
on the trace table.
During a dry run:
» every time the value of a
variable is changed, the new
value is entered in that column of
the trace table
» every time a value is output, the
value is shown in the output
column
Writing, amending, identifying,
and correcting errors
Flowcharts, Pseudocode,
Programs.
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 in to
sub-problems; if it is complex, you
may want to consider writing an
algorithm for each sub-problem.
Most problems, even the simplest
ones can be divided into:
– Set up processes
– Input
– Processing of data
– Permanent storage of data (if
required)
– Output of results.
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. If
you are told how to construct your
algorithm, then follow the
guidance.
6. When constructing an algorithm,
ensure it is clear and easy to
understand. Use precise, well-
structured steps with meaningful
names for variables and data
stores. Be accurate with conditions
in loops and selections, like using
Counter >= 10 instead of vague
descriptions. Clarity and precision
are essential, just like in program
code, to make the algorithm easy
to follow and implement.
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.
Have a look at this structure
diagram and flowchart for the
algorithm to select the largest,
Max, and smallest, Min, numbers
from a list of ten numbers. This
time the flowchart is more easily
readable than the structure chart:
THANK YOU

You might also like