0% found this document useful (0 votes)
8 views228 pages

Paper2 Final Notes 2023 Python

The document outlines the program development life cycle, which consists of five stages: Analysis, Design, Coding, Testing, and Maintenance. It explains key concepts such as abstraction and decomposition, and provides examples of structured English, pseudocode, and flowcharts for problem-solving. Additionally, it covers data types, variable declaration, and the importance of input and output in programming.

Uploaded by

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

Paper2 Final Notes 2023 Python

The document outlines the program development life cycle, which consists of five stages: Analysis, Design, Coding, Testing, and Maintenance. It explains key concepts such as abstraction and decomposition, and provides examples of structured English, pseudocode, and flowcharts for problem-solving. Additionally, it covers data types, variable declaration, and the importance of input and output in programming.

Uploaded by

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

2210 O-level Comp-Sci

2210
By Navid Saqib

As per the Syllabus of 2023-25 +923334259883 Paper - 2


2210 O-level Comp-Sci 2210

The Development Life Cycle


The program development life cycle is used to convert a manual system to a computerized
system and is divided into five stages:

• Analysis
• Design
• Coding
• Testing
• Maintenance

Analysis:
To solve a problem, it needs to be clearly defined or understood. So the person should
clearly know what should be developed as a solution. To study the system, we mostly use
abstraction and decomposition. Let’s understand them,

Abstraction:
means picking up only the right information for the solution and dropping all irrelevant
information, simplest example is when you provide information to a map application to go
to a specific place map application drops all other maps part and loads only the relevant
map to define routes.

Let’s see it in new direction


Abstraction Is developing clear models or a clear solution to complex problem. Abstraction
involves extracting information based on criteria while ignoring what is not relevant in a
solution, only including what is necessary to solve that problem
Abstraction developed simplified models that are suited to a specific purpose by eliminating
any unnecessary information from that model. Examples of abstraction are- Maps,
calendars, Timetables.

Stages:

1. The first stage is to identify the purpose of the model of the situation that is to be built.

2. Use the information gathered from appropriate sources to identify what details need to
be included in the model and what should be removed

Navid Saqib: 03334259882 Page 1


2210 O-level Comp-Sci 2210

Decomposition:

This means dividing the main problem into small subproblems so that those subproblems
can be solved easily as individual one.

Design:
In the design stage, we create Pseudocode, flowcharts, structural charts, and layout screens
on the bases of the program specification obtained in the Analysis Stage. The design stage
decides what will be created in the shape of software.

Coding and iterative testing


Each program module is created using an appropriate programming language, then
evaluated to determine if it functions as intended. Iterative testing entails running modular
tests, changing the code, then running tests again and again until the module satisfies the
requirements.

Testing
The resulting program or set of programs repeatedly run with various test data sets. This
guarantees that every task carried out functions in accordance with how the software was
designed.

Navid Saqib: 03334259882 Page 2


2210 O-level Comp-Sci 2210

System and Sub systems


A computer system is made up of people, data, hardware, software, and communications.
Each computer system can be broken down into a number of smaller systems.

Each sub-system can be further broken down into even smaller sub-systems, and so on,
until each sub-system only does one action.

In decomposition stage, many simple problems some time become more complex than at
first sight.

Main
Level0 Program

Level1 Module -1 Module-2

Level2 Module-1.1 Module-1.2 Module-2.1

Dividing the modules in 1.1, 1.2 subsections is also known as known as STEP WISE REFINEMENT or Top
Down Design

Main Steps of breaking down a problem/programming

• Setting and declaration • Setting and declaration


• Input • Input
• Process • Calculations/constructs
• Output • Output

Navid Saqib: 03334259882 Page 3


2210 O-level Comp-Sci 2210

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

Structure English Flowchart Pseudocode

•Structured English – a method •a diagrammatic •Pseudocode – a method of


of showing the logical steps in representation of an showing the detailed logical
an algorithm, using an agreed algorithm. steps in an algorithm, using
subset of straightforward keywords, identifiers with
English words for commands meaningful names, and
andmathematical operations. mathematical operators

Structure English example

• //find average of given numbers


1 Ask for the number of values
2 Loop that number of times
3 Enter a value in loop
4 Add the value to the Total in loop
5 Calculate and output average

Q: write a cooking recipe ?


Q: write how to find a sum of 5 numbers

Navid Saqib: 03334259882 Page 4


2210 O-level Comp-Sci 2210

Pseudo-code: is an English written statement with logical expressions.

Pseudocode example

•//find average of given numbers


Total ← 0
PRINT "Enter the number of values to average"
INPUT Number
FOR Counter ← 1 TO Number
PRINT "Enter value"
INPUT Value
Total ← Total + Value
NEXT Counter
Average ← Total / Number
PRINT "The average of ", Number, " values is ", Average

Sample flow chart

Navid Saqib: 03334259882 Page 5


2210 O-level Comp-Sci 2210

Knowing Pseudo-codes
Pseudo code:

Informal high-level description of a computer program or other algorithm, intended for human
reading rather than machine reading.

Pseudo-code is not a programming language, but simply an informal way of describing a


program. It does not require strict syntax but instead serves as a general representation of a
program's functions.

Main Steps of solving a problem/programming

• Setting and declaration • Setting and declaration


• Input • Input
• Process • Calculations/constructs
• Output • Output

Navid Saqib: 03334259882 Page 6


2210 O-level Comp-Sci 2210

IDENTIFIER TABLE

• Deciding about ingredients Using IDENTIFIER(variable) TABLE

Ingredient/Equipment Name Quantity Needed Description


Tea 1 Tea spoon Required for the Flavor and color
Sugar 2 Tea spoon For sweetener
Water 1 cup For Diluting the substance
Milk ½ cup For enriches the taste
Kettle 1 unit For processing
Stove 1 For processing
Cup 1 unit For pouring tea
Saucer 1unit For cup
Spoon 1 For presentation and mixing sugar
Teapot 1 For making tea in it

Solution to the problem step by step:

Put kettle on stove If you take sugar then


Put Water in Kettle Add the sugar to the cup
If it is an electric kettle then End if
Switch the kettle on Check while water has not boiled
Else Wait
Put it on lit gas/turn stove on Endwhile
End if Add water to teapot
Setup the cup and saucer Wait for 3 minutes to let it BRU
Put a spoon on the saucer Pour tea into the cup
Get the Teapot If there is a sugar in the cup
Put the tea in teapot Stir the tea
End if
If you take the milk then
Add the milk to the cup Repeat
Endif Wait
Until tea is cool enough to drink
Enjoy Tea!

The solution given is a conceptual one not a programming code

Navid Saqib: 03334259882 Page 7


2210 O-level Comp-Sci 2210

Flowchart
What is a flowchart?

• A flowchart is a picture (graphical representation) of the problem solving process.


• A flowchart gives a step-by-step procedure for solution of a problem.

Elements of a flowchart:

• Various geometrical shaped boxes represent the steps of the solution.


• The shapes are connected by directional arrows to show the flow of the solution.

Uses of a flowchart:

• To specify the method of solving a problem.


• To plan the sequence of a computer program.
• Communicate ideas, solutions.

Navid Saqib: 03334259882 Page 8


2210 O-level Comp-Sci 2210

Let’s study few examples

Navid Saqib: 03334259882 Page 9


2210 O-level Comp-Sci 2210

Another example:

Navid Saqib: 03334259882 Page 10


2210 O-level Comp-Sci 2210

Another example:

Navid Saqib: 03334259882 Page 11


2210 O-level Comp-Sci 2210

Another example:

Navid Saqib: 03334259882 Page 12


2210 O-level Comp-Sci 2210

Another example:

Navid Saqib: 03334259882 Page 13


2210 O-level Comp-Sci 2210

Pseudocode:

Navid Saqib: 03334259882 Page 14


2210 O-level Comp-Sci 2210

Q: Lengths of three sides of a triangle a, b, c are given as input. The following flowchart finds
if the triangle is isosceles, equilateral, or scalene. Some boxes in the flowchart are filled in for
you, fill in the rest of the details.
Hint:

• In an equilateral triangle three sides are equal.


• In an isosceles triangle two sides are equal.
• In a scalene triangle three sides are not equal.

Navid Saqib: 03334259882 Page 15


2210 O-level Comp-Sci 2210

Q: There are four types of fruits Apples, Oranges, Bananas and Grapes. Each student can pick
up two fruits. There are some conditions which have to be used to pick up the fruits. Draw a
flow chart which can take the name of first fruit as an input and print the names of second
fruit or fruits that can be picked up.

The conditions are:


• If you pick an apple you can pick banana.
• If you pick orange you can pick grapes.
• If you pick grapes you can pick banana.

Hint: There will be one input box to read the first fruit, three decision boxes and four output
boxes in the flowchart.

Navid Saqib: 03334259882 Page 16


2210 O-level Comp-Sci 2210

State the names of these symbol and their purposes

Symbol1 :__________________

Purpose:

Symbol2 :__________________

Purpose:

Symbol3 :__________________

Purpose:

Navid Saqib: 03334259882 Page 17


2210 O-level Comp-Sci 2210

Symbol4 :__________________

Purpose:

Q:

Navid Saqib: 03334259882 Page 18


2210 O-level Comp-Sci 2210

Difference between Structure English, pseudocode, and program code

1 2 3

Set total equal to zero total ← 0 dim total as integer = 0

Type Description Memory Space Example


a whole number from -2,147,483,648
Integer 4 bytes 37,453
through 2,147,483,647
Visual Basic does not use Real Numbers,
Real instead, it uses Single and Double, which - -
both allow for decimal places
Single 1.5 x 10-45 to 3.4 x 1038 4 bytes 1002.375
Double 5.0 x 10-324 to 1.7 x 10308 8 bytes 9997.775
Decimal 7.9228 x 10-28 to 7.9228 x 1028 16 bytes 3.8
either TRUE or FALSE
Boolean Alternatively 1 or 0 4 bytes (!) TRUE
Alternatively Yes or No
Char A single character 2 bytes J
A Unicode string with a
maximum length of
String A collection of characters Cabbage
2,147,483,647
characters
There are several different types of date
formats that you can apply. 01/01/0001 to 08/17/1924
Date/Time 8 bytes
12/31/9999 and times from 12:00:00 AM 14:34:23
(midnight) through 11:59:59.9999999 PM

Navid Saqib: 03334259882 Page 19


2210 O-level Comp-Sci 2210

Programming Stage 1: Setting and declaration


Here is a table showing all Data Types

Setting and declaration contain two elements

Variables – (changeable values) which can change during the execution of the program
e.g

Price, quantity, total, speed, no of students

Constant – (static Values) which cannot change their values during the program
e.g

Value of π which is 3.142 or 22/7

*In a program you can also create your own constant by declaring its value in the start
of the program

How to declare a variable and a constant in Python?

Num1 = 0
Here the variable of num1 has been declared this contains no initial value but it has to
be input by the user

Pseudo code:
DECLARE num1 : INTEGER
DECLARE num2 : INTEGER

Though it is said that python don’t require variables but the good practice is to define them

Navid Saqib: 03334259882 Page 20


2210 O-level Comp-Sci 2210

Local variable: Local variable can be accessed only in one module where it is stated.

Global variable: Global variable can be accessed in other modules as well

Navid Saqib: 03334259882 Page 21


2210 O-level Comp-Sci 2210

Constants

< constant name > = Value

PI = 3.142

Now the pihas been declared as a constant its value will remain static unless we don’t
change it by writing another programming statement

Pseudocode

CONSTANT PI = 3.142

Navid Saqib: 03334259882 Page 22


2210 O-level Comp-Sci 2210

Programming stage 2: INPUT

There are two ways of input


• User message
• Assigning value to variable/ obtaining value from user/predefined value

After declaring the variable, it's time to assign it a value so that it can be used for further
processing. To assign a value to a variable, you must first display a valid user message to the
user, so he or she understands what type of input is required.

Pseudocode:
DECLARE NUM1 : INTEGER
OUTPUT “Input num 1”
INPUT num1
OUTPUT ”Input num 2”
INPUT num2

This message will appear like this:

Navid Saqib: 03334259882 Page 23


2210 O-level Comp-Sci 2210

Programming stage 3: OUTPUT

Displaying output is the one of the most important parts of a program. A program will be
useless until and unless output is not properly displayed.
• To output the value of a variable “,” is used right after the end of user message
• This ”,” is known as which separate the User message with value of variable.

The code is written like this:

Output will be

pseudocode:
OUTPUT “Num1 given is : ”, num1

Navid Saqib: 03334259882 Page 24


2210 O-level Comp-Sci 2210

Third stage: Processing

This stage has a very important role in a program. Most probably it is the most complex stage in
programming language.
All the other three stages can be done in this stage as well
• Such as you can declare a new variable in this stage.
• You can ask for an input from user.
• Show him the output and do the processing.

Operators used in process stage:

• Math’s Operators +, -, *, / , Div, Mod


• Relational operators >, >=, <, <=
• Logical operators AND, OR, NOT

1. Mathematical operators are widely used such as:

• Addition

Navid Saqib: 03334259882 Page 25


2210 O-level Comp-Sci 2210

• Subtraction

• Division

• Multiply

Navid Saqib: 03334259882 Page 26


2210 O-level Comp-Sci 2210

• DIV – (outputs the quotient)

• MOD - (outputs the remainder)

Navid Saqib: 03334259882 Page 27


2210 O-level Comp-Sci 2210

2. Relational/Comparison operators

Used in creating simple conditions like

A>=40,B<10,C<> 1

Mostly used in selection IF THEN CONSTRUCT

Navid Saqib: 03334259882 Page 28


2210 O-level Comp-Sci 2210

3. Logical Operators
Operator VB code
AND And
OR Or
NOT Not

NOT(0) = 1

NOT(1) = 0

Examples OF AND, OR

Burger Burger Burger


OR

OR

AND AND OR AND


Fries Fries Fries
AND AND AND
Coke Coke Coke
Study the Pseudocode

Navid Saqib: 03334259882 Page 29


2210 O-level Comp-Sci 2210

Totaling:
Let’s make a situation. Today is your birthday and you being very excited about how many
Birthday presents would you receive.

• Your brother said that he would give you only 3 Gift


• Sister said she would give you 4 presents
• Mom excuses for giving just 2 gifts
• Your dad is outside in the market and you don’t know how many gifts he would bring for
you.

Now let’s make a Visual basic code for the total number of gifts you would receive.

Navid Saqib: 03334259882 Page 30


2210 O-level Comp-Sci 2210

3 constants declared, their values


would remain constant throughout
2 variables the program
whose values
would be
entered by the
user (dad) and to Totaling done after adding all the gifts
processed (total)

Total number of gifts shown as output

Increment
Let’s make another situation for increment. Suppose you have $150 in your wallet. You receive
$20 more from your father. Then your friend came and returns you your $5 you have lent him
last week. Now you would ask your brother for money and you don’t know how many he would
give you.

Now let’s make a pseudo code and visual basic code for your total increment to your wallet.

Pseudo code:

CONSTANT wallet : INTEGER


CONSTANT dad : INTEGER
CONSTANT friend : INTEGER
DECLARE bro : INTEGER
DECLARE tmoney : INTEGER
Bro = INPUT ”how much money did your brother has given to you ??”
Tmoney = wallet + dad + friend + bro
OUTPUT “Now your total money in the wallet is : ”,tmoney

Navid Saqib: 03334259882 Page 31


2210 O-level Comp-Sci 2210

python code: 2 variables tmoney has to be


3 constants
declared processed and bro has to be entered
manually by user

Navid Saqib: 03334259882 Page 32


2210 O-level Comp-Sci 2210

Decrement
Suppose you have $200 in your wallet. You went to a store and bought some vegetables and
fruits for $20. Then you pay a debt of $100 to a bank. Your child has said that he would be
requiring some money for his school assignment. At the end of the day how many you have left
with??? Let’s make a Visual basic code and Pseudo code for this situation.

Pseudo code :

CONSTANT total : INTEGER = 200


CONSTANT vegfrui : INTEGER = 20
CONSTANT debt : INTEGER = 100
DECLARE child : INTEGER
DECLARE remaning : INTEGER
Child = INPUT “How much money did you give to your child”
remaning = total – (vegfrui + debt + child)
OUTPUT “Remaning money after the expenditures is :”, remaning

Python code:

Navid Saqib: 03334259882 Page 33


2210 O-level Comp-Sci 2210

Problem : create a program for every formula

Navid Saqib: 03334259882 Page 34


2210 O-level Comp-Sci 2210

Counting (per unit 1, 10, 100 ….)


The count is basically used in the loop, to count the number of turns such a loop makes 10
rounds then the count will be assigned by the value of 10. Count always is increment pre-unit,
one by one, and always starts from 1
Suppose you are said to count the chairs in the room. What would you do??
Simply start counting the chairs, but in your mind what are you doing??
You are just simply adding the 1 in the previous number of chair stored in your mind

Command for count

Count = count + 1

Navid Saqib: 03334259882 Page 35


2210 O-level Comp-Sci 2210

A Nursing home of orphanage has 10 kids to support. Nursing staff has to give lunch box to all
those kids who are less than 10 of age and pocket money to those who are more than 10 in
age.

PSEUDOCODE :

DECLARE amount : INTEGER

DECLARE total : INTEGER

DECLARE age : INTEGER


DECLARE upperage : INTEGER

DECLARE lowerage : INTEGER

FOR child = 0 to 3

age = INPUT “Enter the age of Child”

IF age > 10
THEN

amount = INPUT“how much money did you give to your child”

OUTPUT ”today’s pocket money is : ”,amount

upperage = upperage + 1

ELSE

OUTPUT “He/She needs a lunch box”

lowerage = lowerage + 1

ENDIF

ENDFOR

OUTPUT”Total amount you paid to the kids : ”,total

OUTPUT”Kids count with Age > 10 ”,upperage

OUTPUT”kids count who needs a lunch box :”,lowerage

Let’s make a Visual basic program

Navid Saqib: 03334259882 Page 36


2210 O-level Comp-Sci 2210

Count being done one


unit being added into
it

Navid Saqib: 03334259882 Page 37


2210 O-level Comp-Sci 2210

Selection:
There are two types of selection statements If and Case, where if statement has three
categories.

a) IF ….THEN…. ENDIF(one way)


b) IF ….THEN….ELSE…..ENDIF ( Two-way)
c) IF….THEN ….ELSEIF….ENDIF (Multiple – way)
d) Caseof…… otherwise ….end (case statement)

IF STATEMENT (One Way)

IF Condition THEN

Task

ENDIF

Pseudo code

DECLARE num1 : INTEGER = 0


Num1 = INPUT ”Enter num 1”
IF num1 > 0
THEN
OUTPUT ”Positive”
ENDIF
IF num1 = 0
THEN
OUTPUT”Exactly equals to zero”
ENDIF
IF num1 < 0
THEN
OUTPUT”Negative”
ENDIF

Navid Saqib: 03334259882 Page 38


2210 O-level Comp-Sci 2210

This statement is used only when students have to deal with one condition such as

One If block handle


one Task (positive)

One If block handle


one Task(equal to
zero)

One If block handle


one Task(Negative)

Here IF the value entered in num1 is lesser than 0 then “Negative” will be displayed.

Navid Saqib: 03334259882 Page 39


2210 O-level Comp-Sci 2210

More Examples:

PSEUDO CODE:

DECLARE Data : INTEGER


Data = INPUT “Enter data”

IF Data = 10

THEN

OUTPUT”Exactly equal to 10”

ENDIF

IF Data = 20

THEN

OUTPUT”Exactly equals to 20 ”

ENDIF

IF Data < 0

THEN

OUTPUT”less than 0”

ENDIF

Navid Saqib: 03334259882 Page 40


2210 O-level Comp-Sci 2210

python CODE:

Navid Saqib: 03334259882 Page 41


2210 O-level Comp-Sci 2210

Another example:

PSEUDOCODE:

DECLARE age : INTEGER


DECLARE name : INTEGER

name = INPUT “PLEASE ENTER YOUR NAME”

age = INPUT “enter your age”

IF age > 10 AND age < 18

THEN

OUTPUT “Dear”, name , “You are old enough to ride a bike”

ENDIF

IF age > 10

THEN

OUTPUT “Dear”, name , “You can also drive a car”

ENDIF

IF age < 10

THEN

OUTPUT “Dear”, name , “You can only ride a Bicycle”

ENDIF

Navid Saqib: 03334259882 Page 42


2210 O-level Comp-Sci 2210

python code:

Navid Saqib: 03334259882 Page 43


2210 O-level Comp-Sci 2210

b) IF….THEN….ELSE….ENDIF (Two-way)

IF Condition THEN

Task1

ELSE

Task2

ENDIF

Nesting : using IF then statement inside the IF than statement, if you have 3 conditions 2 of it will be
used in positive part and 1 will be used in else portion

IF Condition 1 THEN

IF Condition 2 THEN

Task1

ELSE

Task2

ENDIF

ELSE

Task3

ENDIF

Navid Saqib: 03334259882 Page 44


2210 O-level Comp-Sci 2210

Pseudocode:

DECLARE num1 : INTEGER = 0

Num1 = INPUT “Enter num 1”

IF num1 > 0

THEN

IF num1 = 0

THEN

OUTPUT”Exactly equals to zero”

ENDIF

IF num1 > 0

THEN

OUTPUT”Positive”

ENDIF

ELSE

OUTPUT”Negative ”

ENDIF

Navid Saqib: 03334259882 Page 45


2210 O-level Comp-Sci 2210

This statement is used when students have to deal with 2 conditions such as
Is a nested if, true
portion contain
two single if
blocks. Stating if
value is >0 again
where value is
equal to zero then
first task will run
otherwise second
task is run

Here a nested if has been used in the first block the

Navid Saqib: 03334259882 Page 46


2210 O-level Comp-Sci 2210

Finding Even And Odd numbers

Navid Saqib: 03334259882 Page 47


2210 O-level Comp-Sci 2210

Module Module1
Dim num As Integer
Sub Main()
Console.Write("Enter a Number : ")
num = Console.ReadLine()

If (num Mod 2 = 0) Then


Console.WriteLine("{0} is even", num)
Else
Console.WriteLine("{0} is odd", num)
End If
'This line is to hold the output screen
Console.ReadKey()
End Sub
End Module
Another example of finding vowels

Module Module1
Sub Main()
Dim alphabet, c As Char
Console.Write("Enter a Alphabet : ")
alphabet = Console.ReadLine()
c = UCase(alphabet)
If c = "A" Or c = "E" Or c = "I" Or c = "O" Or c = "U" Then

Navid Saqib: 03334259882 Page 48


2210 O-level Comp-Sci 2210

Console.WriteLine("{0} is vowel", alphabet)


Else
Console.WriteLine("{0} is not vowel", alphabet)
End If
'This line is to hold the output screen
Console.ReadKey()
End Sub
End Module
C)IF….THEN….ELSEIF….ENDIF (Multiple ways) Using AND OR NOT in conditions

IF Condition 1 THEN

Task1

ELSEIF Condition 2 THEN

Task2

ELSEIF Condition 3 THEN

Task3

ELSEIF Condition 4 THEN

Task4

ELSE

Error

ENDIF

Navid Saqib: 03334259882 Page 49


2210 O-level Comp-Sci 2210

Pseudo code:

DECLARE num1 : INTEGER


num1 = INPUT “Input number 1”

IF num1 > 10 and num1 < 20


THEN

OUTPUT”Greater than 10”

ELSE IF num1 > 20 and num1 < 30


THEN

OUTPUT”Greater than 30”

ENDIF

ELSEIF num1 > 30 and num1 < 40


THEN

OUTPUT”Greater than 30”

ELSEIF num1 > 40 and num1 < 50


THEN

OUTPUT”Greater than 40”

ELSEIF num1 > 50 and num1 < 60


THEN

OUTPUT”Greater than 50”

ENDIF

Navid Saqib: 03334259882 Page 50


2210 O-level Comp-Sci 2210

This statement is used when students have to deal with more than 2 or multiple conditions, you
can cater error in else or you can directly deal condition in elseif such as

Navid Saqib: 03334259882 Page 51


2210 O-level Comp-Sci 2210

Another example of multiple if (Grading System in a school) GRADES ARE MOSTLY DONE ON
MARKS

DECLARE name : STRING


DECLARE marks1 : INTEGER
DECLARE marks2 : INTEGER
DECLARE total : INTEGER
DECLARE percentage : INTEGER
DECLARE grade : STRING

name = INPUT "Input student name"


marks1 = INPUT "Input marks in test 1"
marks2 = INPUT"Input marks in test 2"

total = Marks1 + marks2


percentage = (total / 200) * 100

IF percentage >= 90
THEN
grade = "A*"
ELSEIF percentage >80 and percentage < 90
THEN
grade = "A"
ELSEIF percentage > 70 and percentage < 80
THEN
grade = "B"
ELSEIF percentage > 60 and percentage < 70
THEN
grade = "C"
ELSEIF percentage > 50 and percentage < 60
THEN
grade = "D"
ELSE
grade = "FAIL"
ENDIF

OUTPUT "Student name is : ", name , " " , "His percentage is : ", percentage , " " , "and
their grade is : ", grade

Navid Saqib: 03334259882 Page 52


2210 O-level Comp-Sci 2210

python code:

Navid Saqib: 03334259882 Page 53


2210 O-level Comp-Sci 2210

CASE STATEMENT
It is alternative selections construct like IF statement. First the variable has to be declared upon
which case has to be applied. There are 3 ways to write a CASE construct.
Pseudo code:
DECLARE grade : STRING
grade = INPUT “Input student grade”
Case of grade
“A” : OUTPUT ”Top grade”
“F”, “U” : OUTPUT ”FAIL”
“B” to “E” : OUTPUT ”Pass”
OTHERWISE
OUTPUT “ Invalid grade”
END CASE
python code:

Single value

Single value
separated by comma

Range

Other entries would be


rejected with a user
message

Navid Saqib: 03334259882 Page 54


2210 O-level Comp-Sci 2210

Another example

Now we will make a calculator using case statement

Pseudo code

DECLARE num1 : INTEGER


DECLARE num2 : INTEGER
DECLARE choice : STRING
DECLARE result : INTEGER

num1 = INPUT “Enter the value 1”


num2 = INPUT “Enter the value 2”
OUTPUT “Enter “+” for Addition”
OUTPUT “Enter “*” for Multiplication”
OUTPUT “Enter “/” for Division”
OUTPUT “Enter “-” for Subtraction”
choice = INPUT ”Enter choice”

CASE OF choice

“+” : result = num1 + num2


result = “result is :”,result
“*” : result = num1 * num2
result = “result is :”,result
“/” : result = num1 / num2
result = “result is :”,result
“-” : result = num1 - num2
result = “result is :”,result
ENDCASE

Note* : if you want you can Add case else to state the error option in it

CASE OF choice

“+” : result = num1 + num2


result = “result is :”,result
“*” : result = num1 * num2
result = “result is :”,result
“/” : result = num1 / num2
result = “result is :”,result
“-” : result = num1 - num2
result = “result is :”,result
CASE ELSE
OUTPUT “no other operation available ”
ENDCASE

Navid Saqib: 03334259882 Page 55


2210 O-level Comp-Sci 2210

Navid Saqib: 03334259882 Page 56


2210 O-level Comp-Sci 2210

Another example of grading using case statement

Navid Saqib: 03334259882 Page 57


2210 O-level Comp-Sci 2210

Write Pseudocode of above given code:


______________________________________________________________________________

______________________________________________________________________________

______________________________________________________________________________

______________________________________________________________________________

______________________________________________________________________________

______________________________________________________________________________

______________________________________________________________________________

______________________________________________________________________________

______________________________________________________________________________

______________________________________________________________________________

______________________________________________________________________________

______________________________________________________________________________

______________________________________________________________________________

______________________________________________________________________________

______________________________________________________________________________

______________________________________________________________________________

______________________________________________________________________________

______________________________________________________________________________

______________________________________________________________________________

______________________________________________________________________________

______________________________________________________________________________

______________________________________________________________________________

Navid Saqib: 03334259882 Page 58


2210 O-level Comp-Sci 2210

Another example:

Navid Saqib: 03334259882 Page 59


2210 O-level Comp-Sci 2210

Write Pseudocode of above given code:


______________________________________________________________________________

______________________________________________________________________________

______________________________________________________________________________

______________________________________________________________________________

______________________________________________________________________________

______________________________________________________________________________

______________________________________________________________________________

______________________________________________________________________________

______________________________________________________________________________

______________________________________________________________________________

______________________________________________________________________________

______________________________________________________________________________

______________________________________________________________________________

______________________________________________________________________________

______________________________________________________________________________

______________________________________________________________________________

______________________________________________________________________________

______________________________________________________________________________

______________________________________________________________________________

______________________________________________________________________________

______________________________________________________________________________

______________________________________________________________________________

Navid Saqib: 03334259882 Page 60


2210 O-level Comp-Sci 2210

Nesting of IF statements
Nesting is used in programming whenever multiple categories has to be described under one
or more options for example

Pseudocode: here in positive range EXACTLY EQUALS TO ZERO and other POSISTIVE
INTERGERS force us to do nesting

DECLARE num1 : INTEGER = 0

Num1 = INPUT “Enter num 1”


IF num1 > 0
Then
IF num1 = 0
Then
OUTPUT”Exactly equals to zero”
ENDIF
IF num1 > 0
Then
OUTPUT”Positive”
ENDIF
ELSE
OUTPUT”Negative ”
ENDIF

Navid Saqib: 03334259882 Page 61


2210 O-level Comp-Sci 2210

Is a nested if, true


portion contain
two single if
blocks. Stating if
value is >0 again
where value is
equal to zero then
first task will run
otherwise second
task is run

Navid Saqib: 03334259882 Page 62


2210 O-level Comp-Sci 2210

Another example: Pizza Name: Fajita has three options to select

• Small
• Medium
• Large

Remaining code on next page

Navid Saqib: 03334259882 Page 63


2210 O-level Comp-Sci 2210

Navid Saqib: 03334259882 Page 64


2210 O-level Comp-Sci 2210

Navid Saqib: 03334259882 Page 65


2210 O-level Comp-Sci 2210

Write Pseudocode of above given code:


______________________________________________________________________________

______________________________________________________________________________

______________________________________________________________________________

______________________________________________________________________________

______________________________________________________________________________

______________________________________________________________________________

______________________________________________________________________________

______________________________________________________________________________

______________________________________________________________________________

______________________________________________________________________________

______________________________________________________________________________

______________________________________________________________________________

______________________________________________________________________________

______________________________________________________________________________

______________________________________________________________________________

______________________________________________________________________________

______________________________________________________________________________

______________________________________________________________________________

______________________________________________________________________________

______________________________________________________________________________

______________________________________________________________________________

______________________________________________________________________________

Navid Saqib: 03334259882 Page 66


2210 O-level Comp-Sci 2210

______________________________________________________________________________

______________________________________________________________________________

______________________________________________________________________________

______________________________________________________________________________

______________________________________________________________________________

______________________________________________________________________________

______________________________________________________________________________

______________________________________________________________________________

______________________________________________________________________________

______________________________________________________________________________

______________________________________________________________________________

______________________________________________________________________________

______________________________________________________________________________

______________________________________________________________________________

______________________________________________________________________________

______________________________________________________________________________

______________________________________________________________________________

______________________________________________________________________________

______________________________________________________________________________

______________________________________________________________________________

______________________________________________________________________________

______________________________________________________________________________

Navid Saqib: 03334259882 Page 67


2210 O-level Comp-Sci 2210

ITERATION
Iteration is another name for LOOPS. There are 3 types of iteration

Non Conditional Loops

• FOR TO NEXT or count-controlled loops

Conditional Loops

• DO LOOP UNTIL or Post condition loops equal ant to Repeat…until


• WHILE LOOP or Pre condition loops

FOR TO NEXT (count-controlled loops)

This iteration is used when numbers of turns or loops are known. Variable which is used for
loop is incremented inside the loop so that loop performs the fixed number of turns and it
executes the loop until given value is reached.

Pseudo code:

FOR count = 1 to 100

grade = INPUT “Input student grade”

Case of grade
“A” : OUTPUT ”Top grade”
“F”, “U” : OUTPUT ”FAIL”
“B” to “E” : OUTPUT ”Pass”
OTHERWISE
OUTPUT “ Invalid grade”
END CASE
END FOR

Navid Saqib: 03334259882 Page 68


2210 O-level Comp-Sci 2210

Step OPTION is used to divide a larger loop into equal smaller divisions. Can run in negative as
well

Loop Established For count 1 to


100
Step used to divide 100 turns
into 10 turns.

End of FOR TO
NEXT loop

Navid Saqib: 03334259882 Page 69


2210 O-level Comp-Sci 2210

Write Pseudocode of above given code:


______________________________________________________________________________

______________________________________________________________________________

______________________________________________________________________________

______________________________________________________________________________

______________________________________________________________________________

______________________________________________________________________________

______________________________________________________________________________

______________________________________________________________________________

______________________________________________________________________________

______________________________________________________________________________

______________________________________________________________________________

______________________________________________________________________________

______________________________________________________________________________

______________________________________________________________________________

______________________________________________________________________________

______________________________________________________________________________

______________________________________________________________________________

______________________________________________________________________________

______________________________________________________________________________

______________________________________________________________________________

______________________________________________________________________________

______________________________________________________________________________

Navid Saqib: 03334259882 Page 70


2210 O-level Comp-Sci 2210

Another example of for…to… next loop

Here the code will ask to input the speed of 10 cars and check them against limit speed and will
fine them if their speed exceeds speed limit.

Pseudo code

DECLARE carspeed : INTEGER


CONSTANT fine : INTEGER =500
DECLARE Totalfine : INTEGER
DECLARE speedlimit : INTEGER = 120

FOR count = 1 to 10
speed = INPUT”input car speed”

IF speed > speedlimit


Then
OUTPUT ”your fine is : ”,fine
Totalfine = totalfine + fine
ElSE
OUTPUT ”You are driving on a safe speed”
ENDIF

ENDFOR

OUTPUT “Total Fine Collected :” ,totalfine

Navid Saqib: 03334259882 Page 71


2210 O-level Comp-Sci 2210

Python code:

Navid Saqib: 03334259882 Page 72


2210 O-level Comp-Sci 2210

Change in situation: Company wants to know how many out of 10 cars have issued challan.

Defining of Counting
Variable

Counter has to be
placed where challan
has to issued

Navid Saqib: 03334259882 Page 73


2210 O-level Comp-Sci 2210

Write Pseudocode of above given code:


______________________________________________________________________________

______________________________________________________________________________

______________________________________________________________________________

______________________________________________________________________________

______________________________________________________________________________

______________________________________________________________________________

______________________________________________________________________________

______________________________________________________________________________

______________________________________________________________________________

______________________________________________________________________________

______________________________________________________________________________

______________________________________________________________________________

______________________________________________________________________________

______________________________________________________________________________

______________________________________________________________________________

______________________________________________________________________________

______________________________________________________________________________

______________________________________________________________________________

______________________________________________________________________________

______________________________________________________________________________

______________________________________________________________________________

______________________________________________________________________________

Navid Saqib: 03334259882 Page 74


2210 O-level Comp-Sci 2210

Finding prime numbers out of 1 to 100

Navid Saqib: 03334259882 Page 75


2210 O-level Comp-Sci 2210

Write Pseudocode of above given code:


______________________________________________________________________________

______________________________________________________________________________

______________________________________________________________________________

______________________________________________________________________________

______________________________________________________________________________

______________________________________________________________________________

______________________________________________________________________________

______________________________________________________________________________

______________________________________________________________________________

______________________________________________________________________________

______________________________________________________________________________

______________________________________________________________________________

______________________________________________________________________________

______________________________________________________________________________

______________________________________________________________________________

______________________________________________________________________________

______________________________________________________________________________

______________________________________________________________________________

______________________________________________________________________________

______________________________________________________________________________

______________________________________________________________________________

______________________________________________________________________________

Navid Saqib: 03334259882 Page 76


2210 O-level Comp-Sci 2210

DO LOOP UNTILL (Post-conditions loops)

As the name suggests, this loops executes the statement, evaluates it and then compare it with
the given condition. As long as the condition is FALSE the loop is executed again and again and
loop ends as the condition is satisfied.

Pseudo code:

Marks = INPUT “Input marks”


Repeat
marks =INPUT ”Input valid marks ”
UNTILL marks > 0 and marks < 100
OUTPUT ”valid marks : ”, marks

Python code :

Here values less than 0 and greater than 100 are shown as invalid marks and value between 0
and 100 is displayed as valid marks.

Navid Saqib: 03334259882 Page 77


2210 O-level Comp-Sci 2210

And other example: This code is written to output the error until the user inputs the valid
number of quantities to be bought. (Greater than 5 but less than 15)

Pseudo code:

DECLARE name : STRING


DECLARE price : INTEGER
DECLARE qty : INTEGER
DECLARE total : INTEGER
Name = INPUT “Input your name”
Qty = INPUT “Input your quantity”
REPEAT
Qty = INPUT ”please input qty greater than 5 but less than 15”
UNTILL qty > 5 and qty < 15
Price = INPUT “Input price”
Total = price * qty
OUTPUT “the total is : ”, total

python code:

Do Until loop to output error until the


user input the valid entry

Navid Saqib: 03334259882 Page 78


2210 O-level Comp-Sci 2210

Q : Dray run the code given Above against 20,1,-18,14

WHILE LOOP (pre-condition loop)

This loop evaluates the condition before the statements within the loop are executed. Pre-
condition loops will execute the statements within the loop as long as the condition evaluates
to True. When the condition evaluates to False, execution will go to the next statement after
the loop

Verification
is a process in a manual written data is compared with typed data or with computer file to decide that
data inputted in correct.

Validation through While loop


Validation is the automatic process of a program ensuring that the data is accurate. a computer system
must authorize it before it can be used. When an entity verifies data If the data is refused by the
computer system, a message should be output. Why the data was refused and why a second chance to
enter the data was provided?

There are many different types of validation checks including:


» range checks
» length checks
» type checks
» presence checks
» format checks
» check digits

Range Checks: defining a particular range so the input value should be restricted to that by using 2 steps

• get input from user


• define the range for input

Navid Saqib: 03334259882 Page 79


2210 O-level Comp-Sci 2210

Pseudo code:

marks = INPUT “Input marks”

while marks < 0 AND marks > 100 DO

marks = INPUT “Input valid marks”

ENDWHILE

OUTPUT ”Valid marks are : ”,marks

• Marks greater than 100 and smaller than 0 are rejected with a user message “input
invalid marks”

Navid Saqib: 03334259882 Page 80


2210 O-level Comp-Sci 2210

Example 2
Pseudocode:

DECLARE name : STRING


DECLARE price : INTEGER
DECLARE qty : INTEGER
DECLARE total : INTEGER

Name = INPUT ”Input Your name ”


Qty = INPUT “Input your quantity”

While qty < 5 or qty > 15 DO


Qty = INPUT “Please input quantity greater than 5 but less than 15”
ENDWHILE

Price = INPUT “input price :”


Total = qty * price
OUTPUT ”total is : ”, total

Python code: Another way of validation. Whenever you want to validate marks or numeric
values always use While……..Endwhile

Navid Saqib: 03334259882 Page 81


2210 O-level Comp-Sci 2210

Write Pseudocode of above given code:


______________________________________________________________________________

______________________________________________________________________________

______________________________________________________________________________

______________________________________________________________________________

______________________________________________________________________________

______________________________________________________________________________

______________________________________________________________________________

______________________________________________________________________________

______________________________________________________________________________

______________________________________________________________________________

______________________________________________________________________________

______________________________________________________________________________

______________________________________________________________________________

______________________________________________________________________________

______________________________________________________________________________

______________________________________________________________________________

______________________________________________________________________________

______________________________________________________________________________

______________________________________________________________________________

______________________________________________________________________________

______________________________________________________________________________

______________________________________________________________________________

Navid Saqib: 03334259882 Page 82


2210 O-level Comp-Sci 2210

Range check using while..endwhile/Repeat until/Do Loop until

Navid Saqib: 03334259882 Page 83


2210 O-level Comp-Sci 2210

Another example of validation

Navid Saqib: 03334259882 Page 84


2210 O-level Comp-Sci 2210

Test1 Marks
are validated
from 0 to 25

Test2 Marks
are validated
from 0 to 35

Test3 Marks
are validated
from 0 to 45

Navid Saqib: 03334259882 Page 85


2210 O-level Comp-Sci 2210

Write Pseudocode of above given code:


______________________________________________________________________________

______________________________________________________________________________

______________________________________________________________________________

______________________________________________________________________________

______________________________________________________________________________

______________________________________________________________________________

______________________________________________________________________________

______________________________________________________________________________

______________________________________________________________________________

______________________________________________________________________________

______________________________________________________________________________

______________________________________________________________________________

______________________________________________________________________________

______________________________________________________________________________

______________________________________________________________________________

______________________________________________________________________________

______________________________________________________________________________

______________________________________________________________________________

______________________________________________________________________________

______________________________________________________________________________

______________________________________________________________________________

______________________________________________________________________________

Navid Saqib: 03334259882 Page 86


2210 O-level Comp-Sci 2210

length checks: is restricting to particular characters, say 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.

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

Navid Saqib: 03334259882 Page 87


2210 O-level Comp-Sci 2210

OUTPUT "Please enter your family name "


REPEAT

INPUT FamilyName

IF LENGTH(FamilyName) > 30 OR LENGTH(FamilyName) < 2

THEN

OUTPUT "Too short or too long, please re-enter "

ENDIF
UNTIL LENGTH(FamilyName) <= 30 AND LENGTH(FamilyName) >= 2

Write a visual basic code for the given pseudocode


______________________________________________________________________________

______________________________________________________________________________

______________________________________________________________________________

______________________________________________________________________________

______________________________________________________________________________

______________________________________________________________________________

______________________________________________________________________________

______________________________________________________________________________

Navid Saqib: 03334259882 Page 88


2210 O-level Comp-Sci 2210

Presence check :
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.

python Code:

Navid Saqib: 03334259882 Page 89


2210 O-level Comp-Sci 2210

Format check :

A format check checks to ensure that some data in a specific format has been entered, for example, an
ABC1234.

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

Navid Saqib: 03334259882 Page 90


2210 O-level Comp-Sci 2210

python program: this program refers to STRING MANIPULATION refer to that section first

Navid Saqib: 03334259882 Page 91


2210 O-level Comp-Sci 2210

More Questions:

Q: write validation code in pseudocode of

Presence check
format check
range check

Q: Develop this code in vb and test it by providing different values. Then Dry run it

Navid Saqib: 03334259882 Page 92


2210 O-level Comp-Sci 2210

Verification is the process of making sure that information is accurately copied from one source
to another, such as when it is entered into a computer or transferred from one portion of a
computer system to another.

Verification methods for input data include:


» Double entry
» Screen/visual check.

Data is entered twice for double entry, sometimes by different operators. When the computer
system compares the two entries, it generates an error message requesting that the data be
entered again.

A SCREEN/VISUAL CHECK is a manual check completed by the data entry user. When all of the
data has been entered, it is displayed on the screen, and the user is asked to confirm that it is
correct before proceeding.

Navid Saqib: 03334259882 Page 93


2210 O-level Comp-Sci 2210

Dealing with Test Data: contains two parts

1-suggestion of test data

• Normal data
• Abnormal data
• Extreme/boundary data

Normal Data: are the valid ranges for example a valid range for a QTY field is 0 to 40 specified by the
business, so that will be considered as a valid data range.

Abnormal Data: are the invalid range like in Qty field less than 0 and greater than 40 are the invalid
ranges.

Extreme/Boundary Data: 0 and 40 will be considered as the boundary range or the extreme range.

Dataset: 56,-18,0,40

Data Set Test Type Expected Result Original Result


56 Normal Data No error should be Accepted Accepted no error
-18 Abnormal Data Error will be produced error
0 Boundary Data Accepted no error No error accepted
40 Boundary Data Accepted no error No error accepted

Navid Saqib: 03334259882 Page 94


2210 O-level Comp-Sci 2210

Dry run of flow chart :

Navid Saqib: 03334259882 Page 95


2210 O-level Comp-Sci 2210

Create a flow chart based on the given pseudocode:

A←0
B←0
C ← 100
OUTPUT "Enter your ten values"
REPEAT
INPUT X
IF X > B
THEN
B←X
ENDIF
IF X < C
THEN
C←X
ENDIF
A←A+1
UNTIL A = 10
OUTPUT B, C

Navid Saqib: 03334259882 Page 96


2210 O-level Comp-Sci 2210

Create flow chart Here:

Navid Saqib: 03334259882 Page 97


2210 O-level Comp-Sci 2210

Hint use dry run method to solve it and even can find error if it is there

Navid Saqib: 03334259882 Page 98


2210 O-level Comp-Sci 2210

1D Array/One dimensional Array


In Array the data is stored as a list. That list is given a name and its data type is declared in
setting and declaration part.

Products
Maize
Wheat
Biscuits
Oil
Eggs
Here the Product is the name of Array and its data type is declared as STRING

While writing a program code TWO FOR LOOPS are used

• First to input and processing


• Second to display output

In setting and declaring the lower and upper bounds has to be declared, which means that how
many data is to be stored in a single array

DIM product(0 to 4) as STRING (previous example)

See an other example

In Next Page

Navid Saqib: 03334259882 Page 99


2210 O-level Comp-Sci 2210

Navid Saqib: 03334259882 Page 100


2210 O-level Comp-Sci 2210

Examples of more than one Arrays.

Products Qty Price Total


Maize 2 3 6
Wheat 3 1 3
Biscuits 4 2 8
Oil 6 2 12
Eggs 7 1 7

names Test1 Test2 Test3 Total Avgm


Navid 20 14 16 50 16
Saqib 30 24 31
Sameen 21 20 28
Adina 20 20 12
Haider 18 10 27
Find out the total and avg marks manually

This is piece of code will ask for marks of 5 students in 3 subjects then it will calculate their
total, average and display them

Pseudo code with annotation:

//setting and declaration


DECLARE names : ARRAY [1 to 5] of STRING
DECLARE test1 : ARRAY [1 to 5] of INTEGER
DECLARE test2 : ARRAY [1 to 5] of INTEGER
DECLARE test3: ARRAY [1 to 5] of INTEGER
DECLARE total : ARRAY [1 to 5] of INTEGER
DECLARE avgm : ARRAY [1 to 5] of INTEGER
//input an processing in a loop
FOR X = 1 to 5
INPUT “Input student name”, name[X]
INPUT “Input marks for test1”, test1[X]
INPUT “Input marks for test2”, test2[X]
INPUT “Input marks for test3”, test3[X]
total[X] = test1[X] + test2[X] + test3[X]
avgm[X] = total[X] / 3
ENDFOR
//output
FOR Y = 1 to 5

Navid Saqib: 03334259882 Page 101


2210 O-level Comp-Sci 2210

OUTPUT ”student name ”, name[Y], “ ”, “And total marks are ”,total[Y], “ ”, “average marks
”,avgm[Y]
ENDFOR

Arrays declared from 0 to 4


which are going to be used
First FOR LOOP
being run for input
and processing

NOTE: Every
array is
written with
accordance to
the FOR
LOOP in
which it’s Second loop is being run in accordance with
being run Y to display the output

Navid Saqib: 03334259882 Page 102


2210 O-level Comp-Sci 2210

Navid Saqib: 03334259882 Page 103


2210 O-level Comp-Sci 2210

Test the give code and annotate the python code by adding ‘ to first line
‘setting and declaration

Q: Create one dimension arrays on following

• Buying multiple fruits from market stating its quantity and price and calculate its total
• Buying multiple grocery items from market stating its quantity and price and calculate
its total
• Buying multiple cinema tickets for different movies stating its quantity and price and
calculate its total

Navid Saqib: 03334259882 Page 104


2210 O-level Comp-Sci 2210

Another example of billing

Pseudo code:

DECLARE price : ARRAY [1 to 2] of INTEGER


DECLARE qty : ARRAY [1 to 2] of INTEGER
DECLARE total : ARRAY [1 to 2] of INTEGER
DECLARE tvalue : ARRAY [1 to 2] of INTEGER
DECLARE gtotal : ARRAY [1 to 2] of INTEGER
DECLARE pname : ARRAY [1 to 2] of STRING
FOR X = 0 to 1
INPUT"Input product name ", pname[X]
INPUT"Input quantity", qty[X]
INPUT"Input price", price[X]
Total[X] = price[X] * qty[X]
IF total < 150
THEN
tvalue[X] = total[X] * 0.0
gtotal[X] = total[X] + tvalue[X]
ELSEIF total[X] > 150 AND total[X] < 500
THEN
tvalue[X] = total[X] * 0.1
gtotal[X] = tvalue[X] + total[X]
ELSEIF total[X] > 500 and total[X] < 1000
THEN
tvalue[X] = total[X] * 0.16
gtotal[X] = total[X] + tvalue[X]
ELSEIF total[X] > 1000
THEN
tvalue[X] = total[X] * 0.2
gtotal[X] = total[X] + tvalue
ENDIF
ENDFOR
FOR Y = 1 to 2
OUTPUT"product name is : ",pname[Y], " ", "quantity is : ", qty[Y], " " , "tax value is : ",
taxval[Y], " ", "gross bill is : ",gtotal[Y]
ENDFOR

Navid Saqib: 03334259882 Page 105


2210 O-level Comp-Sci 2210

python code:

Each Array is being written in


accordance with LOOP which is being
run in this case of (X)

Navid Saqib: 03334259882 Page 106


2210 O-level Comp-Sci 2210

Finding a MAXIMUM AND MINIMUM value from an array


The examiner will ask to find out the highest, lowest, total, minimum or the maximum number
from an array we will write the visual basic codes for all the problems.
Example 1
Let’s consider a school who wants to input the numbers of 5 students in 3 tests, calculate their
total and find maximum marks.
Pseudo code:
DECLARE name : ARRAY [1 to 3] of STRING
DECLARE marks1 : ARRAY [1 to 3] of INTEGER
DECLARE marks2 : ARRAY [1 to 3] of INTEGER
DECLARE marks3 : ARRAY [1 to 3] of INTEGER
DECLARE total : ARRAY [1 to 3] of INTEGER
DECLARE min : ARRAY [1 to 3] of INTEGER = 10000000

FOR X = 1 to 3
INPUT " input marks in test 1", marks1[X]
INPUT " input marks in test 2", marks2[X]
INPUT " input marks in test 3", marks3[X]
total[X] = marks1[X] + marks2[X] + marks3[X]
NEXT
FOR A = 1 to 3
IF total[A] < min
THEN
min = total[A]
ENDIF
NEXT
OUTPUT"minimum marks are :", min
FOR Y = 1 to 3
OUTPUT"total is : ",total[Y]
NEXT

Navid Saqib: 03334259882 Page 107


2210 O-level Comp-Sci 2210

Minimum is not an array because only one value can


be minimum or maximum. Minimum is given the
largest value so that minimum values can be stored

Total being calculated

Another separate Loop being run to calculate


the minimum value from the array of total

Minimum value is displayed


outside of the loop because
minimum value has to show only
once

Navid Saqib: 03334259882 Page 108


2210 O-level Comp-Sci 2210

The same case is with the maximum numbers we just change the lesser than sign “<” with
greater than sign”>” and declare the maximum variable = 0

Now we will calculate the total of the whole class and calculate its average from the previous
code.

Navid Saqib: 03334259882 Page 109


2210 O-level Comp-Sci 2210

Visual Basic code:

Variables declared for grand total


and average of the whole class

Average being
calculated outside
the loop

Grand total being calculated in the


loop

Output is on Next Page:

Navid Saqib: 03334259882 Page 110


2210 O-level Comp-Sci 2210

Ask Teacher for further questions for practice.

Navid Saqib: 03334259882 Page 111


2210 O-level Comp-Sci 2210

Linear search in 1D Array

Linear search is a process of comparing a search item with every individual item of the array,
same we have a numeric item to be searched in an array of multiple numbers.

Let’s understand the concept:

Search item
4 NUM
12
38
2
5

A loop of 4 index from 1 to 4 will be required to perform the linear search.

Pseudocode:

DECLARE NUM: ARRAY[1:4] OF INTEGER


DECLARE SEARCHITEM :INETEGER
//INPUT 4 NUMBERS
FOR X ←1 TO 4
INPUT “ENTER NUMBER : ”,NUM[X]
ENDFOR
//LINEAR SEARCH CODE

INPUT “ENTER SEARCH ITEM : ”,SEARCHITEM


FOR Y←1 TO 4
IF SEARCHITEM = NUM[X]
THEN
OUTPUT “DATA FOUND AT LOCATION : “, Y
ELSE
OUTPUT “DATA NOT FOUND AT LOCATION : “, Y
ENDIF
ENDFOR

Navid Saqib: 03334259882 Page 112


2210 O-level Comp-Sci 2210

Navid Saqib: 03334259882 Page 113


2210 O-level Comp-Sci 2210

Problem1: the code won’t show any result if I enter 6 actually which is not in the list ,code
won’t show any answer

Solution1: use a Boolean variable to check the found or not found status if not found in list it
will show data not found in list.

Problem2: say if I have 1000 items in an array showing 9999 items as not found and one item as
found is cumbersome to find one single entry of found in it

Solution2: remove else portion in the code to remove unnecessary not found repetition.

Lets solve this problem by updating pseudocode and vb code

Navid Saqib: 03334259882 Page 114


2210 O-level Comp-Sci 2210

Here is the python code read it carefully Test it in visual studio and then write its pseudocode

Navid Saqib: 03334259882 Page 115


2210 O-level Comp-Sci 2210

Write its pseudocode here:

Navid Saqib: 03334259882 Page 116


2210 O-level Comp-Sci 2210

For searching in string code will be modified in

to this

Navid Saqib: 03334259882 Page 117


2210 O-level Comp-Sci 2210

Q: Write a pseudocode for it?

Navid Saqib: 03334259882 Page 118


2210 O-level Comp-Sci 2210

Bubble Sort:

Bubble sorting is a process of sorting data Alphabetically in ascending or in descending order, either data
in numbers or in alphabets

Process involved checking first two data elements of array if first element is greater swap will take place
otherwise it moves to second and third element of array and then carry one.

Swapping: requires a temp variable to swap two variables .

Variable A Temp Variable B

12 12 5

5 12 5

5 12 12

Navid Saqib: 03334259882 Page 119


2210 O-level Comp-Sci 2210

Pseudocode:
DECLARE NUM:ARRAY[1:3] OF INTEGER
DECLARE TEMP : INETGER
//INPUT 3 DATA ELEMENTS
FOR X← 1 TO 3
INPUT “ENTER NUMBER: “,NUM[X]
ENDFOR
OUTPUT “ “
OUTPUT “unsorted list”
FOR A ←1 TO 3
OUTPUT NUM[A]
ENDFOR
OUTPUT “ “
For E ← 1 To 3
For C = 1 To 2 - 1
IF num[C] > num[C + 1] Then
TEMP ← num[C]
num[C] ← num[C + 1]
num[C + 1] = TEMP
End If
Next
Next
OUTPUT “ “
OUTPUT "Sorted list"
For y ← 1 To 2
OUTPUT num[y]
EMDFOR

Navid Saqib: 03334259882 Page 120


2210 O-level Comp-Sci 2210

PYTHON CODE FOR BUBBLE SORT

Navid Saqib: 03334259882 Page 121


2210 O-level Comp-Sci 2210

Apply this code in python and see its working: Another Example of bubble sort

Navid Saqib: 03334259882 Page 122


2210 O-level Comp-Sci 2210

Write a pseudocode for the given code


______________________________________________________________________________

______________________________________________________________________________

______________________________________________________________________________

______________________________________________________________________________

______________________________________________________________________________

______________________________________________________________________________

______________________________________________________________________________

______________________________________________________________________________

______________________________________________________________________________

______________________________________________________________________________

______________________________________________________________________________

______________________________________________________________________________

______________________________________________________________________________

______________________________________________________________________________

______________________________________________________________________________

______________________________________________________________________________

______________________________________________________________________________

______________________________________________________________________________

______________________________________________________________________________

______________________________________________________________________________

______________________________________________________________________________

Navid Saqib: 03334259882 Page 123


2210 O-level Comp-Sci 2210

2D Array
In 1d array we created 4 different arrays to handle four elements like product name, qty, price,
total respectively

Products Qty Price Total


Maize 2 3 6
Wheat 3 1 3
Biscuits 4 2 8
Oil 6 2 12
Eggs 7 1 7

Lets change it in to one single grid of rows and cols

Products Qty Price Total


Maize 2 3 6
Wheat 3 1 3
Biscuits 4 2 8
Oil 6 2 12
Eggs 7 1 7
Understanding its locations row wise and Col wise is necessary, lets labels rows and cols.

0 1 2 3
0 Maize 2 3 6
1 Wheat 3 1 3
2 Biscuits 4 2 8
3 Oil 6 2 12
4 Eggs 7 1 7

0 1 2 3
0 0,0 0,1 0,2 0,3
1 1,0 1,1 1,2 1,3
2 2,0 2,1 2,2 2,3
3 3,0 3,1 3,2 3,3
4 4,0 4,1 4,2 4,3
These locations will help us to assign data to them e.g if I wanna assign data maize to 0,0 I will
do it like that Data[0,0] ← “Maize”

Navid Saqib: 03334259882 Page 124


2210 O-level Comp-Sci 2210

Let’s create its pseudocode.

DECLARE DATA : ARRAY[1:5,1:4] OF STRING

FOR R ← 1 to 5

For C ← 1 to 3
INPUT “ENTER DATA : “,DATA[R,C]

ENDFOR

ENDFOR

FOR ROW ← 1 TO 5

DATA[ROW,3] ← TONUM(DATA(ROW,1)) * TONUM(DATA(ROW,2))

ENDFOR

OUTPUT “P-NAME”, “ “, ”QTY” , “ “, ”PRICE” , “ “, ”TOTAL”

FOR X ← 1 to 5

For Y ← 1 to 4
OUTPUT DATA[X,Y], “ “

ENDFOR
OUTPUT “ “

ENDFOR

Navid Saqib: 03334259882 Page 125


2210 O-level Comp-Sci 2210

python code:

Navid Saqib: 03334259882 Page 126


2210 O-level Comp-Sci 2210

Let’s calculate its grand total

Write a pseudocode for the given code


______________________________________________________________________________

______________________________________________________________________________

Navid Saqib: 03334259882 Page 127


2210 O-level Comp-Sci 2210

______________________________________________________________________________

______________________________________________________________________________

______________________________________________________________________________

______________________________________________________________________________

______________________________________________________________________________

______________________________________________________________________________

______________________________________________________________________________

______________________________________________________________________________

______________________________________________________________________________

______________________________________________________________________________

______________________________________________________________________________

______________________________________________________________________________

______________________________________________________________________________

______________________________________________________________________________

______________________________________________________________________________

______________________________________________________________________________

______________________________________________________________________________

______________________________________________________________________________

______________________________________________________________________________

______________________________________________________________________________

Question : input data of 5 student name test1, test2, test3, and calculate total and avg marks
using 2D array and display Student name, test1 , test2, test3, total, avgmarks.

Navid Saqib: 03334259882 Page 128


2210 O-level Comp-Sci 2210

Procedures and Functions (Modular Programming)


Procedures: are the separate block of code which perform a task on the call given by the main code, it
doesn’t have a capability to return back any value or update any value in main program.

Or

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.

Lets see the example:

Pseudocode:

PROCEDURE Addition(p1 : INTEGER, p2 : INTEGER)


DECLARE TOTAL : INTEGER
TOTAL ← P1+P2
OUTPUT “ADDITION IS : “ , ADDITION)
END PROCEDURE
CALL Addition(10,5)

python code: addition is a procedure in which two values where forwarded 10 to p1 and 5 to p2, than
total is calculated and out is produced, so rather than main program another block will perform addition
on the behalf of main code call.

Navid Saqib: 03334259882 Page 129


2210 O-level Comp-Sci 2210

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 e.g p1 : INTEGER, p2 :
INTEGER are the two parameters

Another example: Let make a calculator

Navid Saqib: 03334259882 Page 130


2210 O-level Comp-Sci 2210

Write a pseudocode for the given code


______________________________________________________________________________

______________________________________________________________________________

______________________________________________________________________________

______________________________________________________________________________

______________________________________________________________________________

______________________________________________________________________________

______________________________________________________________________________

______________________________________________________________________________

______________________________________________________________________________

______________________________________________________________________________

______________________________________________________________________________

______________________________________________________________________________

______________________________________________________________________________

______________________________________________________________________________

______________________________________________________________________________

______________________________________________________________________________

______________________________________________________________________________

______________________________________________________________________________

______________________________________________________________________________

______________________________________________________________________________

______________________________________________________________________________

Navid Saqib: 03334259882 Page 131


2210 O-level Comp-Sci 2210

Functions: are the separate block of code which perform a task on the call given by the main code, it
does have a capability to return back any value or update any value in main program. Rather than
producing output it silently got o main code and store its result in it variable. Rather than call we use
assignment it to a variable.

Description : in this code all functions will calculate and return total value to different variables in main
program and main program will output the received values at any time.

Pseudocode:
FUNCTION Addition(p1 : INTEGER, p2 : INTEGER) RETURN INTEGER
DECLARE TOTAL : INTEGER
TOTAL ← P1+P2
RETURN TOTAL
END FUNCTION

FUNCTION SUBTRACTION(p1 : INTEGER, p2 : INTEGER) RETURN INTEGER


DECLARE TOTAL : INTEGER
TOTAL ← P1-P2
RETURN TOTAL
END FUNCTION

FUNCTION MULTIPLY(p1 : INTEGER, p2 : INTEGER) RETURN INTEGER


DECLARE TOTAL : INTEGER
TOTAL ← P1*P2
RETURN TOTAL
END FUNCTION

FUNCTION DIVISION(p1 : INTEGER, p2 : INTEGER) RETURN INTEGER


DECLARE TOTAL : INTEGER
TOTAL ← P1/P2
RETURN TOTAL
END FUNCTION

DECLARE N1,N2,ADD,SUBTRACT,MUL,DIVI : INTEGER


INPUT “ENTER NUM1 : “ , N1
INPUT “ENTER NUM2 : “ , N2
ADD←ADDITION(N1,N2)
SUBTRACT←SUBTRACTION(N1,N2)
MUL←MULTIPLY(N1,N2)
DIVI←DIVISION(N1,N2)
OUTPUT “ADDITION IS : “,ADD
OUTPUT “SUBTRACTION IS : “,SUBTRACT

Navid Saqib: 03334259882 Page 132


2210 O-level Comp-Sci 2210

OUTPUT “MULTIPLY IS : “,MUL


OUTPUT “DIVISION IS : “,DIVI
python code:

Navid Saqib: 03334259882 Page 133


2210 O-level Comp-Sci 2210

ANOTHER EXAMPLE: COVERTING FERHENHITE TO CELCIS

Python code:

Write a pseudocode for the given code


______________________________________________________________________________

______________________________________________________________________________

______________________________________________________________________________

______________________________________________________________________________

______________________________________________________________________________

______________________________________________________________________________

______________________________________________________________________________

______________________________________________________________________________

______________________________________________________________________________

______________________________________________________________________________

______________________________________________________________________________

Navid Saqib: 03334259882 Page 134


2210 O-level Comp-Sci 2210

______________________________________________________________________________

Problem : your teacher provide you a formula sheet and tell you to create functions for every individual
formula and integrate it in a full program

Navid Saqib: 03334259882 Page 135


2210 O-level Comp-Sci 2210

Local and global 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.

Psudocode:

DECLARE Number1, Number2, Answer : INTEGER


PROCEDURE Test
DECLARE Number3, Answer : INTEGER
Number1 ← 10
Number2 ← 20
Number3 ← 30
Answer ← Number1 + Number2
OUTPUT "Number1 is now ", Number1
OUTPUT "Number2 is now ", Number2
OUTPUT "Answer is now ", Answer
ENDPROCEDURE
Number1 ← 50
Number2 ← 100
Answer ← Number1 + Number2
OUTPUT "Number1 is ", Number1
OUTPUT "Number2 is ", Number2
OUTPUT "Answer is ", Answer
CALL Test
OUTPUT "Number1 is still ", Number1
OUTPUT "Number2 is still ", Number2
OUTPUT "Answer is still ", Answer
OUTPUT "Number3 is ", Number3

Navid Saqib: 03334259882 Page 136


2210 O-level Comp-Sci 2210

Files handling
Till yet we are saving all our work in variables, constants, arrays temporarily when we turn off
our computer all saved work in memory is vanished away. To save them permanently we need
to stop it on hard disk drive.

Types of files

• 1-serial file system (text file)


• 2-sequentail file system (binary file)
• 3-random file system (index file)

In syllabus of O level we will deal with serial file system only.

Lets understand front end and back end of OS (Operating System):

Frontend: contain applications programs which run to complete a particular task for the user

Backend: contains hardware controlled by kernel, all device drivers are stored in it. Its kernel
responsibility to kept it optimized in all mean.

API: a set of functions and procedures allowing the creation of applications that access the
features or data of an operating system, application, or other service. It works like a bridge
between front end and back end.

Navid Saqib: 03334259882 Page 137


2210 O-level Comp-Sci 2210

Writing process in file:

Writing process in file

Kernel Hardware’s
variable API

User

Object of file Object need Path filename and mode to run

Reading data from file:

Reading process in file

Kernel Hardware’s
variable API

User

Object of file Object need Path filename and mode to run

Navid Saqib: 03334259882 Page 138


2210 O-level Comp-Sci 2210

Appending data to a saved file:

Appending Data in file

Kernel Hardware’s
variable API

User

Object of file Object need Path filename and mode to run

Writing python code :

Attaching a library of System input output by using command system.io

Navid Saqib: 03334259882 Page 139


2210 O-level Comp-Sci 2210

Writing code:

Reading code :

Navid Saqib: 03334259882 Page 140


2210 O-level Comp-Sci 2210

Appending code:

Pseudocode :

// writing

OPENFILE FILEMANAGER FOR WRITE

FILEMANAGER ← “MINE.TXT”

DECLARE DATA AS STRING

INPUT “ENTER DATA TO BE WRITEN IN FILE: “, DATA

WRITE(DATA)

CLOSE FILEMANGER

// READING

OPENFILE FILEMANAGER1 FOR READ

FILEMANAGER1 ← “MINE.TXT”

DECLARE DATA1 AS STRING

Navid Saqib: 03334259882 Page 141


2210 O-level Comp-Sci 2210

DATA ←READ(FILEMANAGER1)

CLOSE FILEMANGER1

// APPENDING

OPENFILE FILEMANAGER2 FOR APPEND

FILEMANAGER2 ← “MINE.TXT”

DECLARE DATA2 AS STRING

INPUT “ENTER DATA TO BE APPENDED IN FILE: “, DATA2

WRITE(DATA2)

CLOSE FILEMANGER2

Navid Saqib: 03334259882 Page 142


2210 O-level Comp-Sci 2210

Example of Complete program:

Navid Saqib: 03334259882 Page 143


2210 O-level Comp-Sci 2210

Navid Saqib: 03334259882 Page 144


2210 O-level Comp-Sci 2210

Knowing Database
Database: is a collection of raw data arranged in shape of fields, records tables. Can be obtain
through putting a criteria based on query to search the Information out of these tables

Let’s understand database structure

Item can be a single


Item character like A , L, I, 2 etc

When multiple items are


Field collected at one place that is
known as field and it has a
particular Label as well

When multiple fields are


Record combined at one place that is
known as record and it
should has a unique key in it

When multiple records are


Table combined at one place that is
known as table and it should
be about one Entity like
student, fee, and teacher

Navid Saqib: 03334259882 Page 145


2210 O-level Comp-Sci 2210

When multiple tables are


Database combined at one place that is
known as Database and if it
contains multiple tables then
some sort of relationship
should be in it

Table in design Mode:

Contains multiple data


types like TEXT, NUMBER
AUTONUMUBER,
DATETIME, YES/NO etc.

Data Types and where you can use them :

Data Type Sample Where to use Description


Auto number 1 or 2 or 3 StudentID Mostly used for
CustomerID primary key to make
ProductID records unique and it
increment
automatically
Text Navid saqib Studentname Can use up to 256
FatherName characters to write
productName text to save details
any text field
Memo
number 21045 Age Where ever
Quantity mathematical
Where ever number calculation is
is required required on that field
currency $21,045 Price , TOTAL etc Where figure in
proper currency
format

Navid Saqib: 03334259882 Page 146


2210 O-level Comp-Sci 2210

Yes/NO Yes First Aid Trained : yes Whenever you need


to ask the question
from user which will
result in either Yes or
No

Relationship:

• One to Many / Many to One


• One to One
• Many to Many

One to Many: this type of relationship exist it 98% databases. For example

One owner has many Cars Example of ONE TO MANY

Alternate can be Many cars are owned by one owner example of MANY TO ONE

Another example: One student can Study Many Subjects.

Navid Saqib: 03334259882 Page 147


2210 O-level Comp-Sci 2210

One to One: this type of relationship exist 2% in database. For example

One student has one officially declared Name

One person has one NIC Number

35302-2414271-5

Q: create a table on student Entity in MS Access and feed at least 10 records in it use these
fields

• Studentid
• StudentName
• FatherName
• DOB
• Gender
• Address
• Telephone
• Mobile
• City
• Province
• Country

Q: Narrate 5 Examples of ONE TO MANY and ONE TO ONE relationship and discuss it with
teacher.

Navid Saqib: 03334259882 Page 148


2210 O-level Comp-Sci 2210

Q: create Two tables on Customer and orders Entity in MS Access and feed at least 10 records
in it use these fields

• Customerid • paymentid
• CustomerName • paymentdate
• FatherName • Amount
• DOB • Paymentmode
• Gender • Bank
• NIC# • Cheque#
• Address • Status
• Telephone
• Mobile
• City
• Province
• Country

Validation in database

Length Check

Format check only M


or F

Presence Check

Navid Saqib: 03334259882 Page 149


2210 O-level Comp-Sci 2210

Queries:

Query Based
example Grid

Query by example grid where you include fields in grid and set criteria on it as shown in image

Navid Saqib: 03334259882 Page 150


2210 O-level Comp-Sci 2210

Query by example can contain multiple elements in it like criteria , sorting , from which table it
is , field name, should it be displayed or not.

Fuel field will not be


shown in result

Criteria to search

Sorting based on
owner field

Data shown in
Result : Ascending order of
owner

Result shown as per


Volvo

Navid Saqib: 03334259882 Page 151


2210 O-level Comp-Sci 2210

Criteria used in used in Query

Text/String based
“Volvo” Find exact match of Volvo
“V*” Result should start with V
“*o” Result Should End with o
“*e*” Result should contain e in it

Number/Integer based
8 Find exact match of 8
>5 Result all values more than 5
>=10 Result all values more than equal to 10
>=5 And <=10 Result should be greater than and equal
to 10 only moth inclusive

Structure Query Language:

There are two sections of it Data Definition Language DDL used to create tables and fields.
Where as the other section is DML data manipulation language which will search data out of
the table based on given conditions, we will cover only DML here as per our syllabus

Navid Saqib: 03334259882 Page 152


2210 O-level Comp-Sci 2210

Assume that we have a crook table in a database with following records.

Selecting fields from table

Navid Saqib: 03334259882 Page 153


2210 O-level Comp-Sci 2210

SELECT name, DoB, gender, town

This line state that the fields selected out of the table is name, date of birth, gender, town

SELECT *

Means all the fields are selected from the table

FROM crooks

from the crooks table

WHERE name = ‘jane’

Means all records of only jane should be displayed

Description: select all fields from crook table and show only those records where name equal to
jane.

Navid Saqib: 03334259882 Page 154


2210 O-level Comp-Sci 2210

Navid Saqib: 03334259882 Page 155


2210 O-level Comp-Sci 2210

Let’s see another example:

SELECT SUM(numScars)

Result : 25 Means total of numscars will be displayed

SELECT COUNT(numScars)
result : 6 Means total numberof count will be displayed.

Navid Saqib: 03334259882 Page 156


2210 O-level Comp-Sci 2210

Logic Gates

Navid Saqib: 03334259882 Page 157


2210 O-level Comp-Sci 2210

Navid Saqib: 03334259882 Page 158


2210 O-level Comp-Sci 2210

Navid Saqib: 03334259882 Page 159


2210 O-level Comp-Sci 2210

Navid Saqib: 03334259882 Page 160


2210 O-level Comp-Sci 2210

Navid Saqib: 03334259882 Page 161


2210 O-level Comp-Sci 2210

Navid Saqib: 03334259882 Page 162


2210 O-level Comp-Sci 2210

Pseudocode
practice Book-let

Navid Saqib: 03334259882 Page 163


2210 O-level Comp-Sci 2210

PYTHON CODES Pseudocode


name = [None] * 2
price = [None] * 2
category = [None] * 2
qty = [None] * 2
total = [None] * 2

for X in range(2):
print("input product name")
name[X] = input()
print("enter quantity")
qty[X] = int(input())
print("There are only four categories: A, B, C,
D")
print("enter category")
category[X] = input()

if category[X] == "A":
price[X] = 150
elif category[X] == "B":
price[X] = 250
elif category[X] == "C":
price[X] = 350
elif category[X] == "D":
price[X] = 450

total[X] = price[X] * qty[X]

for Y in range(2):
print("Product name :", name[Y], "\tquantity
:", qty[Y], "\tCategory is :", category[Y], "\tyour
total is", total[Y])

Navid Saqib: 03334259882 Page 164


2210 O-level Comp-Sci 2210

data = [34, 95, 12, 6]


max_value = 0

for x in range(len(data)):
if data[x] > max_value:
max_value = data[x]

print("max data is:", max_value)

Navid Saqib: 03334259882 Page 165


2210 O-level Comp-Sci 2210

name = [""] * 5
test1 = [0] * 5
test2 = [0] * 5
test3 = [0] * 5
avg = [0] * 5

for X in range(5):
print("Input name:")
name[X] = input()
print("Input marks for test 1:")
test1[X] = int(input())
print("Input marks for test 2:")
test2[X] = int(input())
print("Input marks for test 3:")
test3[X] = int(input())
avg[X] = (test1[X] + test2[X] + test3[X]) / 3

for Y in range(5):
print("Name of student:", name[Y], "Test 1
marks:", test1[Y], "Test 2 marks:", test2[Y], "Test
3 marks:", test3[Y], "Average marks:", avg[Y])

Navid Saqib: 03334259882 Page 166


2210 O-level Comp-Sci 2210

price = [0, 0]
qty = [0, 0]
total = [0, 0]
tvalue = [0, 0]
gtotal = [0, 0]
pname = ["", ""]

for X in range(2):
print("input product name")
pname[X] = input()

print("input quantity")
qty[X] = int(input())

print("input price")
price[X] = int(input())

total[X] = qty[X] * price[X]

if total[X] < 150:


tvalue[X] = total[X] * 0.0
gtotal[X] = total[X] + tvalue[X]
elif total[X] > 150 and total[X] < 500:
tvalue[X] = total[X] * 0.1
gtotal[X] = total[X] + tvalue[X]
elif total[X] > 500 and total[X] < 1000:
tvalue[X] = total[X] * 0.16
gtotal[X] = total[X] + tvalue[X]
elif total[X] > 1000:
tvalue[X] = total[X] * 0.2
gtotal[X] = total[X] + tvalue[X]

for Y in range(2):
print("product name is :", pname[Y], "quantity
is :", qty[Y], "tax value is :", tvalue[Y], "gross bill
is :", gtotal[Y])

Navid Saqib: 03334259882 Page 167


2210 O-level Comp-Sci 2210

names = [""] * 2
marks = [0] * 2

for x in range(2):
print("Input name:")
names[x] = input()
print("Input marks:")
marks[x] = int(input())

for y in range(2):
print("Student names:", names[y], "their
marks are:", marks[y])

Navid Saqib: 03334259882 Page 168


2210 O-level Comp-Sci 2210

product = [""] * 2
qty = [0] * 2
price = [0] * 2
total = [0] * 2

for X in range(2):
print("Input product name ")
product[X] = input()

print("Enter price")
price[X] = int(input())

print("Input quantity")
qty[X] = int(input())

total[X] = qty[X] * price[X]

for Y in range(2):
print("Product name:", product[Y], "price is:",
price[Y], "quantity is:", qty[Y], "total is:",
total[Y])

noplate = [0] * 2
speed = [0] * 2
fine = [0] * 2

for X in range(2):
print("input cars number plate:")
noplate[X] = int(input())

print("Input speed of the car:")


speed[X] = int(input())

if speed[X] > 150:


fine[X] = 200
else:
print("You are driving at a safe speed")

Navid Saqib: 03334259882 Page 169


2210 O-level Comp-Sci 2210

for Y in range(2):
print("Your car number is:", noplate[Y], "your
speed was:", speed[Y], "You have to pay this
amount as a fine:", fine[Y])

names = [None] * 10

for X in range(10):
print("Enter student names:")
names[X] = input()

for Y in range(10):
print("Students names are:", names[Y])

Navid Saqib: 03334259882 Page 170


2210 O-level Comp-Sci 2210

grade = [["" for _ in range(5)] for _ in range(4)]

# Assign labels to the first row


grade[0][0] = "Student name"
grade[0][1] = "Test 1 marks"
grade[0][2] = "Test 2 marks"
grade[0][3] = "Average marks"
grade[0][4] = "Grade"

# Input data for each student


for R in range(1, 4):
for c in range(0, 3):
print("Input data at ({},{}) {}: ".format(R, c,
grade[0][c]))
grade[R][c] = input()

# Calculate average marks and assign grades


for A in range(1, 4):
grade[A][3] = (int(grade[A][1]) +
int(grade[A][2])) // 2
if grade[A][3] >= 90:
grade[A][4] = "A*"
elif 80 <= grade[A][3] < 90:
grade[A][4] = "A"
elif 70 <= grade[A][3] < 80:
grade[A][4] = "B"
elif 60 <= grade[A][3] < 70:
grade[A][4] = "C"
elif 50 <= grade[A][3] < 60:
grade[A][4] = "D"
elif 40 <= grade[A][3] < 50:
grade[A][4] = "E"
else:
grade[A][4] = "FAIL"

# Print the grades


for X in range(4):
for Y in range(5):
print(grade[X][Y], end="\t")
print()

Navid Saqib: 03334259882 Page 171


2210 O-level Comp-Sci 2210

data = [["" for _ in range(5)] for _ in range(4)]

data[0][0] = "id"
data[0][1] = "Name"
data[0][2] = "Price"
data[0][3] = "Quantity"
data[0][4] = "Total"

for R in range(1, 4):


for c in range(0, 4):
print("input data at ({},{}) {}".format(R, c,
data[0][c]))
data[R][c] = input()

for A in range(1, 4):


data[A][4] = str(float(data[A][2]) *
float(data[A][3]))

for X in range(4):
for Y in range(5):
print(data[X][Y], end="\t")
print("")

Navid Saqib: 03334259882 Page 172


2210 O-level Comp-Sci 2210

If then Else /Select Case practice:

Navid Saqib: 03334259882 Page 173


2210 O-level Comp-Sci 2210

Qno1 Write a Pseudocode

• Program will input a price of property.


• If price is more than 200000 then 20 % of the propertyprice is added to the total
• If price is less than 200000 then 5% of the propertyprice is added to the total
• Display total , taxvalue at the end

Write Pseudocode here

Navid Saqib: 03334259882 Page 174


2210 O-level Comp-Sci 2210

Qno2 Write a Pseudocode for Pizzeria Company

They serve three type of pizza to their dine-in customer’s details of pizza’s are given below

Pizza Name Price Type


Mexican 450 Small
550 Medium
650 Large
Fajita 350 Small
450 Medium
550 Large
Tandoori 550 Small
650 Medium
750 Large

Owner of the company want you to design a POS for his business. Software should possess these
features.

• It should input name of pizza a customer want


• After it should input type of pizza customer want
• Then it prompt for the quantity of pizza
• At last it should display the bill details Which include
o Name of pizza
o Type of pizza
o Pizza price
o Quantity
o Total Amount

Navid Saqib: 03334259882 Page 175


2210 O-level Comp-Sci 2210

Write Pseudocode here :

Navid Saqib: 03334259882 Page 176


2210 O-level Comp-Sci 2210

Qno3 Write a pseudocode for a moven ice cream parlor van. Moven Ice-cream parlor van
serves his customers ice-cream cones with only one or two scoops not more than that. Moven
icecream has 3 flavor to serve to his customers which are
• Choc
• Praline
• Tuttifruiti

Program supposed to follow these steps

• Input the flavor


• Input the required scoops
• Every scoops cost 40 rs
• Produce the bill

Navid Saqib: 03334259882 Page 177


2210 O-level Comp-Sci 2210

write Pseudocode here:

Navid Saqib: 03334259882 Page 178


2210 O-level Comp-Sci 2210

Qno 4 Given the sides of a parallelogram, write an algorithm to determine its type. Structured English
solution is given. Write its Pseudocode.

Begin

Read base

Read height

If base = height then Write “square”

If base <> height then Write “rectangle”

End

Pseudocode here:

Navid Saqib: 03334259882 Page 179


2210 O-level Comp-Sci 2210

Qno 4. Ask a user for their eye color as input, if they say green then call them a "Goblin", else “you
must be a different type of monster”. write its pseudocode

Begin

Read Eyes

End

Qno5 A School want to create a program which Ask student regarding its age and decide he/she is in
secondary school or not. Study the snapshots carefully and construct the pseudocode.

Navid Saqib: 03334259882 Page 180


2210 O-level Comp-Sci 2210

Qno6 : Develop program from the given example

Begin
Read Feel
Read Exercise
If feel = "Sad"AND exercise = "No"Then
Write "Go for a walk, you might feel better"
otherwise
write"Sorry I don't know how to help"
EndIf
End
Using one IF statement write code to handle the above:

How do you feel today: Happy or Sad?


Sad
Have you had some exercise: Yes or No? No Go for a walk, you might feel bette

Write pseudocode here

Navid Saqib: 03334259882 Page 181


2210 O-level Comp-Sci 2210

Qno7: In Britain a driving rule is that society members that if they are equal more than 21 years of age
and they are drunk they are not allowed to hire the car for driving. If they are not drunk, they can drive
it.

A program code is generated in python but few chunks are missing. Complete it and regenerate the
program its pseudocode

age = int(input("How old are you? "))


if age >= 21:
print("Good, that's old enough. Have you been drinking?")
drinking = input()
if drinking.lower() == "yes":
print("Come back tomorrow as per rule you’re not allowed to drive")
else:
print("Your car awaits !!!!! :-)")
else:
print("You're too young I'm afraid. Come back in a few years")

//Change this code by using Elseif

Navid Saqib: 03334259882 Page 182


2210 O-level Comp-Sci 2210

Qno8: Metro Pvt ltd has decided to facilitate his customers by providing a Vandering machine in
the mall

Machine which provides them 4 different soft drinks

• Coke
• Pepsi
• Fanta
• 7UP
Kid has to insert 20 Rs currency note in machine. System has to check the validity that it’s a
valid 20 Rs currency note then it ask for the choice of drink and on selection it drops the Drink
can in tunnel.

Where as :

Cokestatus = 1 Means can is dropped

Cokestatus = 0 Means can in not dropped

Navid Saqib: 03334259882 Page 183


2210 O-level Comp-Sci 2210

Write a pseudocode here :

Navid Saqib: 03334259882 Page 184


2210 O-level Comp-Sci 2210

Navid Saqib: 03334259882 Page 185


2210 O-level Comp-Sci 2210

Qno9: Create a login screen to do the following:

Enter username
Jonny5
RECOGNISED! Enter password:
Alive
Please enter Jonny5
If they get the username wrong it should immediately kick them out and not ask for the password. If
they get the password wrong it should kick them out.
Create its pseudocode:

Navid Saqib: 03334259882 Page 186


2210 O-level Comp-Sci 2210

Case Statements:

Qno10 an incomplete code is presented. Read it carefully and correct it than write its
pseudocode

Case Enter Restaurant and pick up menu


If Egg and Chips available Then
Order Egg and Chips
Generate bill
End If
If Pie and Chips available Then
Order Pie and Chips
Generate bill
End If
If Curry and Chips available Then
Order Curry and Chips
Generate bill
End If
If Pizza and Chips available Then
Order Pizza and Chips
Generate bill
End If
otherwise
Leave hungry
End

Navid Saqib: 03334259882 Page 187


2210 O-level Comp-Sci 2210

Navid Saqib: 03334259882 Page 188


2210 O-level Comp-Sci 2210

Qno11 Read this python code and explain the purpose of these lines

number = 5

if number in [1, 2, 3]:


print("Between 1, 2, 3 inclusive")
elif 4 <= number <= 8:
print("Between 4 and up to 8")
elif number == 9:
print("Equal to 9")
elif number == 10:
print("Equal to 10")
else:
print("Not between 1 and up to 10")

Line1 ____________________________________________________________________

Line 2____________________________________________________________________

Line 3____________________________________________________________________

Line 5____________________________________________________________________

Line 12___________________________________________________________________

Line 14___________________________________________________________________

Navid Saqib: 03334259882 Page 189


2210 O-level Comp-Sci 2210

Qno12 Create a program where someone types in the name of an animal and it outputs the
sound the animal makes in shape of text. The animals it should handle are:
• Horse - Eehehehe
• Cow - Moo
• Bear - Grr
• Sheep - Baa
• Tiger - Grr
everything else - Meow

Try and complete this task by only using 5 case statements. Write its pseudocode

Navid Saqib: 03334259882 Page 190


2210 O-level Comp-Sci 2210

Navid Saqib: 03334259882 Page 191


2210 O-level Comp-Sci 2210

Qno12 you are now going to use a case statement to create an ELECTRONIC PIANO.

Musical Note Frequency


A 220.00
B 246.94
C 261.63
D 293.66
E 329.63
F 349.23
G 392.00
Create a case statement in the code below, that will play the notes written above. The while
true loop means that the code will never end and will continue forever. For bonus points try
and get the code to accept both upper and lower case inputs

Hint: For continues loop use:


WhileTrue
'input and case statements here
WhileEnd

Remember to make sound you use:


winsound.beep(2000,500) 'beep at 2000Hz frequency for 0.5 seconds

Navid Saqib: 03334259882 Page 192


2210 O-level Comp-Sci 2210

Navid Saqib: 03334259882 Page 193


2210 O-level Comp-Sci 2210

Solution:

Navid Saqib: 03334259882 Page 194


2210 O-level Comp-Sci 2210

Qno 13 Pizza hut company offer custom pizzas coming direct from HOT Oven as per the
requirements if customer. Company offers 3 types of pizzas to the customer mentioned below

1. Small 30rs extra will be charged


2. Medium 60 rs extra will be charged
3. Large 90 rs extra will be charged
The flavors offered by the company are stated below
1. Fajita 980 rs 2. Mexican 800
3. Tandori 990 rs 4. Bar-b-que 1010
Company decided to give crest either as
1. Filled crest
2. non-filled crest
and charge 50 rs extra if it is filled.

Program should follow this sequence


• should decide about the type either small , medium or large
• should acquire flavor either fajita, Mexican tandoori bar-b-que
• should ask for filled or non-filled crest
• Ask for quantity and calculate bill
• Calculate 16% GST on total bill which will be added to the bill
• Output should look like this

Instruction: Use Nested Select case, don’t use if then else end if statement.

Navid Saqib: 03334259882 Page 195


2210 O-level Comp-Sci 2210

pseudocode here

Navid Saqib: 03334259882 Page 196


2210 O-level Comp-Sci 2210

Navid Saqib: 03334259882 Page 197


2210 O-level Comp-Sci 2210

Navid Saqib: 03334259882 Page 198


2210 O-level Comp-Sci 2210

Qno14 Café 44 decided to serve drinks through automated robot like a Bar tender. But machine
requires age verification
if customer is more the 18 year he/she can order these drinks
• Margarita 150 rs
• Pinna Colada 200 rs
• Pine Shake 300 rs
• Blended cocktail 450 rs
• Water 40 rs
Whereas those who are under the age of 18 can order only these
• Coke 25rs
• Milk Shake 30rs
• Water 20rs
• Red bull 80rs

System will follow these processes to complete the deal.

1. Will ask for age verification from customer


2. Display related choices in front of user( inshape of Menu)
3. On choice ask for quantity required
4. Calculate bill as per given price
5. Generate bill

Note* Use appropriate lay out of bill on your own and declare any price for the drink

Navid Saqib: 03334259882 Page 199


2210 O-level Comp-Sci 2210

Write pseudo code here.

Navid Saqib: 03334259882 Page 200


2210 O-level Comp-Sci 2210

Navid Saqib: 03334259882 Page 201


2210 O-level Comp-Sci 2210

Iteration/Loops practice:

Navid Saqib: 03334259882 Page 202


2210 O-level Comp-Sci 2210

QUESTION NO 1
Write a python code. First input student name then input his/her marks in 2 tests

• Test 1 marks out of 25


• Test 2 marks out of 30

Make sure that invalid marks entered would be rejected. Calculate their average and total.

Write pseudo code here:

______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________

Navid Saqib: 03334259882 Page 203


2210 O-level Comp-Sci 2210

Write program code here:

______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________

Navid Saqib: 03334259882 Page 204


2210 O-level Comp-Sci 2210

QUESTION NO 2
Write a python code. Ask for 10 positive numbers and find the smallest and largest number
between them.

Write pseudo code here:

______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________

Navid Saqib: 03334259882 Page 205


2210 O-level Comp-Sci 2210

Write program code here:

______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________

Navid Saqib: 03334259882 Page 206


2210 O-level Comp-Sci 2210

QUESTION NO 3
Input 10 positive numbers and find min and max out of it and calculate their total.

Write pseudo code here:

______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________

Navid Saqib: 03334259882 Page 207


2210 O-level Comp-Sci 2210

Write program code here:

______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________

Navid Saqib: 03334259882 Page 208


2210 O-level Comp-Sci 2210

QUESTION NO 4
Write a pseudocode. Input the names of 10 vegetables, input their price and calculate total
by multiplying it with quantity (using WHILE and FOR Loops only ).

Write pseudo code here:

______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________

Navid Saqib: 03334259882 Page 209


2210 O-level Comp-Sci 2210

Write program code here:

______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________

Navid Saqib: 03334259882 Page 210


2210 O-level Comp-Sci 2210

Iteration/Loops practice2:

Navid Saqib: 03334259882 Page 211


2210 O-level Comp-Sci 2210

Question no 1
Input the username and password, if username and password is empty it should reenter both.
Also known as validation if username and password match it should display “Access allowed”
otherwise it should display “Access Denied”
*DO THIS QUESTION USNIG BOTH REPEAT UNTILL AND WHILE LOOPS.
Username = Franklin , Password = 11237

Write pseudo code here:

______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________

Navid Saqib: 03334259882 Page 212


2210 O-level Comp-Sci 2210

Write program code here:

______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________

Navid Saqib: 03334259882 Page 213


2210 O-level Comp-Sci 2210

Question no 2
Input the marks of 10 students in 3 tests, apply validation on test1, test2, test3 out of 20, 25
and 30 respectively. Calculate its Total and avgmarks, make sure that all the invalid entries are
rejected with an error statement. Display grand total (Sum of all the marks) and average of the
whole class.

Write pseudo code here:

______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________

Navid Saqib: 03334259882 Page 214


2210 O-level Comp-Sci 2210

Write program code here:

______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________

Navid Saqib: 03334259882 Page 215


2210 O-level Comp-Sci 2210

Question no 3
Input the names 10 of late coming students and fine them according to this table.

Time Fine
Less 10 minutes 100
Greater than 10 less than 25 minutes 150
Greater than 25 less than 40 minutes 250
More than 40 minutes 300
Calculate the total fine collected at the end of the day

Write pseudo code here:

______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________

Navid Saqib: 03334259882 Page 216


2210 O-level Comp-Sci 2210

Write program code here:

______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________

Navid Saqib: 03334259882 Page 217


2210 O-level Comp-Sci 2210

1D Array practice:

Navid Saqib: 03334259882 Page 218


2210 O-level Comp-Sci 2210

Instruction: Only use Loops to handle these arrays questions.


Q no1: Initialize an array for 10 students in a class.

• a : Input 10 Names in array


• c : Output the array

Navid Saqib: 03334259882 Page 219


2210 O-level Comp-Sci 2210

Q no2: Define 2 one dimensional arrays to obtain names of student and their obtain marks and
display both in lists output showing names and their obtain marks.

Navid Saqib: 03334259882 Page 220


2210 O-level Comp-Sci 2210

Q no3: Define 3 one dimensional array to obtain Product Names, Quantity and Price, and
calculate the total by multiplying the quantity with the price

Navid Saqib: 03334259882 Page 221


2210 O-level Comp-Sci 2210

Q no4: Extension to previous question

Using last question code adjust it to find the maximum and minimum quantity required and
calculate the grand total

Qno5: Use three 1D arrays to calculate these.

• Input Names of 30 students


• Input Test1, Test 2, Test3 results of 30 students
• Calculate total of three tests of 30 students
• And at end calculate the average of total marks

Navid Saqib: 03334259882 Page 222


2210 O-level Comp-Sci 2210

Qno5: School wants to record the weight of 80 students when they got admission in school.
And at end of every year their weight will be recorded. So that the difference will be forwarded
to the nutritionist maintain class health chart.

Use 1D arrays to calculate these.

• Input Names of 80 students


• Input height of 80 students
• Input firstweight, secondweight results of 80 students
• Calculate the BMI of 80 Student based on BMI = Weight (kg) / (Height (m) x Height (m))
• In status array If BMI is Less than 20.5 then write “normal” if more than 30.5 than write
“overweight”.
• Calculate difference of weight in an array diffweight =secondweight - firstweight
• And over all calculate the average of diffweight

Navid Saqib: 03334259882 Page 223


2210 O-level Comp-Sci 2210

Question no 6

Write a code for National Traffic Department. In this code first input the number of 5 cars
(using an array), then according to the cars input their speed. IF their speed is exceeding
150km/h then fine that specific car.

Output should be like this

Navid Saqib: 03334259882 Page 224


2210 O-level Comp-Sci 2210

Question no 7

Using array write a python code, this inputs the angle of 3 corners. IF the angle is smaller than
90 then output, it’s a acute angle else if angle is greater than 90 but smaller than 180 then
output it’s a obtuse angle and if angle is greater than 180 but smaller than 360 then output it’s
an reflex angle.

The output should be like this:

Navid Saqib: 03334259882 Page 225


2210 O-level Comp-Sci 2210

Question no 8

The code you have written on question 3, made amendments to it, now add tax to the total
value. If $150<total value > $500 then tax it 10%

$500< total value> $1000 then tax 16%

$1000< total value >1500 or greater then tax 20%

The Output should look like this:

Navid Saqib: 03334259882 Page 226


2210 O-level Comp-Sci 2210

QUESTION NO 9

Write a python code for a transport company. First ask the name of 2 customer store it in a
variable (USING AN ARRAY). Secondly ask how many seats they want. Then ask the distance
they want to travel. IF distance = 2, 3, 4 then price of 1 seat is 20 RS, IF distance = 5, 6, 7 then
price per seat = 30 RS, IF distance = 8, 9, 10 then price of one seat = 40RS ELSE IF distance > 10
then price per seat = 45RS

OUTPUT

NAMES_____ Seats_______ Distance_______ TOTAL___________

Navid Saqib: 03334259882 Page 227

You might also like