Ch7: Algorithm Design
Program Development Life Cycle:
It has following five stages:
Analysis
Design
Coding
Testing
Maintenance
Analysis
o It means to identify the problems in the existing system and set the clear objectives of
the new system. Requirement specification of the new system is defined.
Analysis is done with two tools one is abstraction and other is decomposition.
Abstraction: It means to keep the key elements to develop new system and discard the
unnecessary details
Decomposition: It means to break up complex problem into smaller parts which can
further be subdivided into smaller parts so that smaller portions can be solved easily.
Top down design: It means the problem is solved in modular approach or procedural
approach. A major problem is solve in hierarchical way.
Design:
o It involves
structure charts
flow charts
pseudo codes
Coding:
o On the basis of design work a programmer can do coding to get new application using
any programming language/tool
Page 1 of 20
Ch7: Algorithm Design
Testing:
o Once the coding is done the software is tested to make sure it is working properly.
Testing is done with
Normal set of data
Abnormal set of data
Extreme data e.g. lower extreme or upper extreme
Boundary data e.g. if 1 is the lower possible value then check with 0 and 1. 0
will be rejected and 1 is accepted
Iterative Testing: It means that modular tests are conducted, code amended, and tests repeated
until the module performs as required.
Age can be from 10 to 20 years
Type of test : Normal
Example: 15
Expected Result: should be accepted
Reason: to make sure data is accepted without any problem
Type of test : Abnormal
Example : 7 or 25
Expected Result: Should be rejected
Reason: to make sure out of range or abnormal values are not accepted.
Type of test : Extreme
Example : 10 or 20
Expected Result: Should be Accepted
Reason: minimum and maximum possible values are entered. If these are accepted rest will be
accepted as well.
Type of test : Boundary data can be minimum possible or one point less than minimum
For example. 0 to 100 is accepted
Boundary data will be 0 or -1
0 is accepted / -1 is rejected
Boundary data can be 100 or 101
100 is accepted and 101 is rejected
Dry Run: Trace the pseudo code or flow chart with dummy data is known as Dry Run.
Page 2 of 20
Ch7: Algorithm Design
Maintenance:
It means how the program written in any high level language will be easy to understand for
further updating for another programmer? For this purpose following techniques can be used:
i. Meaningful Identifier Names:
All variables/identifiers should be given meaningful names so that it wont be difficult
for other programmer to understand the purpose of these identifiers
ii. Add Comments:
Each programming line or part of code can be added with comments that will
explain the purpose of each piece of code. It will also help in future for further
improvements
iii. Procedural Approach:
It is good skilled approach to used procedures or modules for different parts of the
program. It will provide easy way for further development.
Computer System: a computer system is made up of software, data , hardware,
communications and people. Each computer system can be divided into a set of sub-
systems and each sub system is further divided into sub system until each sub system
performs a single action which can’t be further divided.
Components parts of any Computer System:
Input: the data used by system needs to be entered
Process: Tasks needs to be performed using input data and previously stored data
Output: information that needs to be displayed on screen or printer paper
Storage: data that needs to be stored for future use
Page 3 of 20
Ch7: Algorithm Design
Structure Diagrams: It shows the design of a computer system in hierarchical way.
Example:
Question from Specimen Paper:
Features of Structure diagrams:
i. Show the hierarchical design of computer program
ii. It is a diagrammatic form of a computer program
iii. It shows program in a top down way
iv. It shows each level with breakdown of system into sub systems.
Page 4 of 20
Ch7: Algorithm Design
Algorithm: an ordered or series of steps to solve a problem.
State Two techniques to build an algorithm:
Algorithm can be design using two techniques:
1. Pseudocodes:
It is a simple method of shown an algorithm. It describes that the algorithm does by using
English key words similar to those used in high level language.
Rules of pseudocodes:
i. All keywords are written in capital format
ii. All names given to data items and subroutines start with capital letters
iii. Repeated or selected statements are indented by two spaces
2. Flow Charts:
A diagram that shows the steps required for a task and the order in which the steps are to be
performed.
Symbols:
Input/Output Condition/Selection
Start and Stop
Process
Use of procedure or library routine
Data flow (direction) get external data
Page 5 of 20
Ch7: Algorithm Design
Sequence, Selection and Iteration:
Sequence: It means all instructions are given in right order in proper sequence for example in
order to calculate area of triangle we need to get input of base and height then we can apply
formula for area e.g.
Input base
Input height
Area = ½ x base x height
Output Area
All instructions are given in proper sequence to get accurate results.
Selection:
There are two selection/conditional statements are used
i. If …. Then…else (these are used for one or two conditions with either or solutions
ii. Case… otherwise … EndCase (these can be used for multiple conditions and solutions ,
multiple outcomes
INPUT Number INPUT Direction
IF Number > = 1 and Number < = 10 Then Case of Direction
Output “Accepted ” W: Output “West”
ELSE E : Output “East”
Output “Not Accepted” N : OUTPUT “North”
END IF S : OUTPUT “South”
Otherwise
OUTPUT “Not valid direction
ENDCASE
Page 6 of 20
Ch7: Algorithm Design
Iterations:
It means the loop. Repeat set of instructions for given number of times
Three loops are used in programming:
i. For…. Next loop (count controlled loop)
a. It is used for fixed number of iterations e.g. from 1 to 10 etc
b. It cannot be used in situations where loop has to terminate on certain condition
ii. Repeat…. Until
a. This loop is known is post conditional loop it can be used in situations in which loop will
run for one time at least because condition is given at the end
b. This loop can also be used in which loop terminates on certain condition
iii. While…Do…EndWhile
a. This loop is known as pre-conditional loop. It can be used in situation where loop will
not run for one time if condition which is given in the beginning is false
b. This can also be used in situation where loop terminates on certain condition
NOTE : Learn to draw flow charts of the following and other problems yourself as done in class.
EXAMPLE OF TOTALLING: EXAMPLE OF COUNTING Maximum and Minimum Value
For Count = 1 to 10 Negative, positive,percentage = 0 Max = 0
Input Number Min = 100
Sum = sum + Number For Count = 1 to 20 For Count = 1 to 20
Input number INPUT Marks
Next Count If number >=0 then IF Marks > Max THEN
Average = Sum / 10 positive = positive + 1 Max = Marks
Print Sum, Average If number < 0 then END IF
negative = negative +1 IF Marks < Min THEN
Next count Min = Marks
Percentage = negative/20* 100 END IF
Print positive, negative, percent NEXT Count
OUTPUT Max , MIN
Use of Repeat loop Use of While loop Use of Nested loop
Sum,Avg,Count = 0 Sum , Avg, Count = 0
Repeat While (Count < 10 ) Do For Student = 1 to 20
INPUT Num INPUT Num Sum = 0
Sum = Sum + Num Sum = Sum + Num Avg = 0
For Subject = 1 to 5
Count = Count + 1 Count = Count + 1 Input Number
UNTIL Count = 10 ENDWHILE Sum = Sum + Number
Avg = Sum /10 Avg = Sum / Num Next Subject
OUTPUT Sum , Avg OUTPUT Avg , Sum Avg = Sum / 5
Output Sum , Avg
Next Student
Page 7 of 20
Ch7: Algorithm Design
Operators used in programming
Mathematical operators: Logical/Comparison Operator: Boolean/Relational Operators:
+ Add > AND
- Subtract >= OR
* Multiply < NOT
/ Division <= Example: IF (A > B AND B > C)
^ Exponent (Raise the <> THEN Output “True”
power) Example: If X > Y THEN
( ) Group Output “ the Value of X is
greater than Y”
Example: X x+y
Other Operators: DIV : It returns quotient value Round : It rounds decimal number
MOD : It returns remainder when one number is divided by to nearest decimal point
value when one number is another
divided with another X = 5 DIV 2 or X = DIV (5,2) X = Round ( 4.5 , 0)
X = 5 Mod 2
X will get 1 X will get 2 X will get 5
Variables: These are named memory locations STRING Functions:
whose value can change during execution of the
program OUTPUT LCASE (“ COMPUTER SCIENCE”)
Converts string lower case
Types:
o Integer: It stores whole numbers that can be positive or OUTPUT UCASE (“Computer Science”)4,
negative Convert string into Upper case
o Real : It means to hold numbers with decimals
o Char : It means to hold a single character OUTPUT LENGTH ( “Computer Science”)
o String : It means to hold a complete string It will print number of characters including spaces
o Boolean: It means to hold True or False/ Yes or No in string e.g. 16
Declaration Substr () : It returns portion of the string.
DECLARE X : INTEGER
DECLARE Y : REAL X = “Computer Science”
DECLARE Z : CHAR
DECLARE Q : STRING Output Substr(X , 4 , 3)
DECLARE R : BOOLEAN It will print put
Constant:
Constants: These are named memory locations
whose value remains the same during execution
of the program
Constant SalePrice 500
Page 8 of 20
Ch7: Algorithm Design
Validation : It means data is valid according to given standard. It is done automatically through
programming code.
Validation Checks:
Range Check: (It means the values are in Length check (It means data contains exact
specific range for example age can be from 5 number of characters for example password can
years to 10 years ) be of 8 characters long)
Repeat Repeat
INPUT Age Input password
UNTIL Age > = 5 and Age < = 10 Until Length (password) = 8
Presence check (It means no important field is Type Check (It means data entered is of given
left blank) type for example numbers are required only
numbers should be entered no text or characters)
Repeat
INPUT CNIC Repeat
Until CNIC < > “ ” Input Age
Until Age = DIV (Age,1)
Format Check (It means characters entered Check Digits: it is the final digit included in a
should be in pre-defined format for example code. It is calculated from all the other digits in
bank code can “BAH999” the code.
Repeat
Input Code
Until Substr(Code, 1, 3) = “BAH”
Verification: It means checking of data when copied from one source to another. It is done with two
techniques:
- Visual Checking/Proof reading (It means a manual check completed by the user who is
entering the data.
- Double entry (It means the same data is entered twice if both are same accepted
otherwise rejected)
Double Entry:
Repeat
Input password1
Input password2
UNTIL password1 = password2
Page 9 of 20
Ch7: Algorithm Design
Dry run with Pseudo code and Flow Chart
Pseudo Code:
A=0
B=0
C = 100
OUTPUT “Enter your tn values”
Repeat
INPUT X
IF X > B THEN
B=X
ENDIF
IF X < C THEN
C=X
END IF
A=A+1
UNTIL A = 10
OUTPUT B, C
Trace the above code with following data
4 , 8 , 19 , 17 , 3, 11, 6, 1, 13 , 9
A B C X Output
0 0 100 4
4 4
1 8
8
2 19 19
3 17
4 3 3
5 11
6 6
7 1 1
8 13
9 9
10 19 , 1
Page 10 of 20
Ch7: Algorithm Design
Page 11 of 20
Ch7: Algorithm Design
Page 12 of 20
Ch7: Algorithm Design
Page 13 of 20
Ch7: Algorithm Design
Page 14 of 20
Ch7: Algorithm Design
Page 15 of 20
Ch7: Algorithm Design
Page 16 of 20
Ch7: Algorithm Design
Page 17 of 20
Ch7: Algorithm Design
Page 18 of 20
Ch7: Algorithm Design
Error Detection in pseudo code
Page 19 of 20
Ch7: Algorithm Design
Page 20 of 20