0% found this document useful (0 votes)
15 views45 pages

Pseudocode ISL

The document provides an overview of algorithms, flowcharts, and pseudocode, detailing their components and structures. It covers basic programming concepts such as input/output, data storage (variables, constants, arrays), arithmetic and logical operators, conditional statements, loops, and subroutines. Additionally, it includes examples of pseudocode for various scenarios and emphasizes the importance of proper identifier naming and declaration.

Uploaded by

waqar.mmurtaza
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)
15 views45 pages

Pseudocode ISL

The document provides an overview of algorithms, flowcharts, and pseudocode, detailing their components and structures. It covers basic programming concepts such as input/output, data storage (variables, constants, arrays), arithmetic and logical operators, conditional statements, loops, and subroutines. Additionally, it includes examples of pseudocode for various scenarios and emphasizes the importance of proper identifier naming and declaration.

Uploaded by

waqar.mmurtaza
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/ 45

Algorithms:

Set of instructions designed to solve a specific problem.

Flow Charts:
Pictorial Representation of algorithms.
Structured English of an algorithm using proper syntax is pseudocode

Pseudocode:
Representation of an algorithm using keywords is pseudocode.

Lesson 1:

Pseudocode Basics.

NOTE : All the keywords in pseudocode are written in uppercase.

1) Input & Output

Input: taking a value and then storing it into a storage location(identifiers)

Output: PRINT: To display something on user’s screen.

2) Identifiers(Store Data)
i) Variables
Any storage locations where a value may change within the program.
ii) Constants
Any storage locations where a value will never change within the program.
iii) Arrays
Storage locations that can hold multiple data items of SAME DATA TYPE.
3)Arithmetic Operators:
i. +
ii. -
iii. *
iv. /
v. ^
vi. <
vii. >
viii. >=(greater than equal to)
ix. >=(Less than equal to)
x. <> (unequal to)

Logical operators
AND(Both conditions satisfied), OR(One Condition satisfied) , NOT
Built in Functions:
DIV(DIVIDER), MOD(REMINDER),
INT(it always removes the fractional part), ROUND, ROUNDUP,

ROUNDDOWN, RANDOM( we will discuss it with arrays)

5) String Handlers(any sentence or any alphanumeric data) try these eg in excel


sheet
LEFT, RIGHT, MID, LENGTH(returns the number of characters within a
string)

3) Selections/Conditional Statements: Many possible outcomes from one condition


i) IF…THEN…ELSE…ENDIF
ii) CASE…OF…OTHERWISE…ENDCASE

4) Loops/Iterations/Repetitions: Multiple Inputs/Program would end at a user-defined


condition. Ghlata structure py -1 hota hai
i) While loop (STRUCTURE) WHILE…ENDWHILE

ii) repeat loop (STRUCTURE) REPEAT…UNTIL


iii) For loop(Structure) FOR…TO…NEXT

6) Arrays Searching & Sorting (Discuss it with array)


i) Linear Search
ii) Bubble Sort

7) Subroutines
i) Procedures (A procedure is a subroutine that performs a task but
does not return a value eg print statement)
ii) Functions (A function is a subroutine that performs a task and
returns a value. Eg operation performed)

8) File Handling

9) Scenario Based Questions [15 marks]

FLOWCHART:

Process Box(Rectangle): Any arithmetic operation or perform any assigned value to any
variable

Start or End the flow chart (OVAL)

Paralellogram (I/o Box): user ko store krna hai ya display krna hai we will use this box
Diamond/Rhombus (Decision Box) whenever you are using a comparison
operator(>,<.>=,<=) , you will use a decision box

Subroutine box (Call Box)

All boxes are connected with each other through flow lines and they must be labelled (IT
IS IMP otherwise -1 srf yes or no sy label krni hai)

Sum of two numbers:


Loop Statement: M>100 or M<0 input again

M<90 toh grading A* krdo agr no hai toh grade A krdo output grade
Lesson 2:

Rules & Identifiers:

Identifiers: Store & identify data.

1) Variables
Any storage locations where a value may change within the program.

2) Constants

Any storage locations where a value will never change within the program.

NOTE: All identifiers must be declared at the start of the program.

Declaration:
Every identifier must be declared, have a suitable/understandable name followed by the
data type.

Rules to name identifiers.

All identifier names must be meaningful.


1) None of the identifier’s names should have spaces.
totalmarks (not understandable)
total_marks(underscores are not in pseudocode guide provided by examiner)
CAPITILIZE 1st letter of each word
TotalMarks (acceptable)
2) None of the identifier names can start with an integer.
1num (wrong)
num1 (acceptable)

Pseudocode for declaration:


Variables:
DECLARE TotalMarks : INTEGER
Constant:
CONSTANT pi : REAL
pi=3.1416
NOTE : Please do not initialize within declaration, i.e., initialize separately.

Let’s try Pseudocode:


NOTE : Examiner says; use appropriate prompts for data entry.

Sum of two Numbers


DECLARE num1,num2,sum : INTEGER
PRINT “please enter a number”
INPUT num1
PRINT “please enter second number”
INPUT num2
sum=num1+num2
PRINT sum

Average of three numbers


DECLARE num1,num2,num3,sum : INTEGER
DECLARE avg : REAL
PRINT “please enter three numbers”
INPUT num1,num2,num3
sum=num1+num2+num3
avg=sum/3
PRINT avg

03-Oct-2023

Pseudocode Topical : Topic Chapter 9

Pseudocode.pdf : Example 1,2 & 3


Lesson 3:
Topic 1:
1) Selection Statements/Conditional Statements:
i) IF…THEN…ELSE…ENDIF
Two possible outcomes for one condition
ii) CASE…OF…OTHERWISE…ENDCASE
Many possible outcomes for one condition

Larger of two numbers

IF CASE

DECLARE num1,num2,Larger : DECLARE num1,num2,Larger :


INTEGER INTEGER

PRINT “please enter 2 numbers” PRINT “please enter 2 numbers”

INPUT num1,num2 INPUT num1,num2

IF num1>num2 THEN CASE Larger OF

Larger=num1 num1 : num1>num2

ELSE num2 : num2>num1

Larger=num2 OTHERWISE

ENDIF PRINT “error”

PRINT Larger ENDCASE

PRINT Larger
Largest of three numbers [nested if]

DECLARE num1,num2,num3,Larger : DECLARE num1,num2,num3,Larger :


INTEGER INTEGER

PRINT “please enter three numbers” PRINT “please enter three numbers”

INPUT num1,num2,num3 INPUT num1,num2,num3

IF num1>num2 AND num1>num3 THEN CASE Larger OF

Larger=num1 num1 : num1>num2 AND num1>num3

ELSEIF num2>num1 AND num2>num3 num2 : num2>num1 AND num2>num3


THEN

Larger=num2 num3 : num3>num1 AND num3>num2

ELSE ENDCASE

Larger=num3 PRINT Larger


ENDIF

PRINT Larger

Grades Table

Students should not be able to enter invalid marks i.e., less than zero or greater
than 100

[Validation]

DECLARE marks : INTEGER DECLARE marks : INTEGER

DECLARE Grade : STRING DECLARE Grade : STRING

PRINT “please enter your marks” PRINT “please enter your marks”

INPUT marks INPUT marks

IF marks < 0 OR marks > 100 THEN IF marks < 0 OR marks > 100 THEN

PRINT “invalid marks entered, please PRINT “invalid marks entered, please
re-enter” re-enter”

INPUT marks INPUT marks

ELSEIF marks>=90 THEN CASE marks OF

Grade=”A*” >=90 : Grade=”A*”

ELSEIF marks>=80 THEN >=80 : Grade=”A”

Grade=”A” >=70 “ Grade=”B”

ELSEIF marks>=70 THEN OTHERWISE

Grade=”B” Grade=”U”

ELSE ENDCASE

Grade=”U” PRINT Grade

ENDIF
PRINT Grade

A simple calculator, to +, -, *, / two numbers dependent on user’s choice

DECLARE choice : CHAR DECLARE choice : CHAR

DECLARE num1,num2,Result : DECLARE num1,num2,Result :


INTEGER INTEGER

PRINT “please enter two numbers” PRINT “please enter two numbers”

INPUT num1,num2 INPUT num1,num2

PRINT “please select your choice” PRINT “please select your choice”

INPUT choice INPUT choice

IF choice=’+’ THEN CASE choice OF

Result=num1+num2 ‘+’ : Result=num1+num2

ELSEIF choice=’-‘ THEN ‘-‘ : Result=num-num2

Result=num1-num2 ‘*’ : Result=num1*num2

ELSEIF choice=’*’ THEN ‘/’ : Result=num1/num2

Result=num1*num2 ENDCASE

ELSE choice=’/’ PRINT Result

Result=num1/num2

ENDIF

PRINT Result

Pseudocode.pdf: Example 15 to 20
Topicals Topic 10: Question Number : 2,3,5,6,7,10,16,20

Lesson 4: Chap 11

Loops/Iteration/Repetition:

We need loops, when there are infinite or huge number of conditions.

We need loops, when program runs on a user-defined value.

1) WHILE…ENDWHILE

2) REPEAT…UNTIL

3) FOR…TO…NEXT

WHILE REPEAT FOR

Pre-conditioned loop. Post-conditioned loop. Counter-Control Loop

Condition is given at the Condition is given at the It works on a given range.


start of the loop. end of the loop.

The loop won’t run if the The loop would run at


condition isn’t justified. least once, even if the Set number of times.
condition isn’t justified.

TRUE
FALSE
=,>
<>,< 1 TO 10

1 TO 20

Basic Algorithms to use in loops.

Counting: [increment or decrement a value]

Count=5
Count=Count+1

6….

7….

8….

Totaling: [sum of several numbers]

Total=0

Total=Total + num //num is the number being entered by the user

0=0+10

10=10+30=40

40=40+10=50

Highest:

Highest=0 //initialize highest to lowest possible logical value

IF num>Highest THEN highest=num

56>0…56

103>56…103

47>103…103

Lowest:

Lowest=99999// initialize lowest to highest possible logical value

IF num<Lowest THEN Lowest=num

126<99999…126

11<126…11

65<11…11

Lesson 5
Print 1st 100 Whole Numbers (0-99)

WHILE REPEAT FOR

DECLARE count : DECLARE count : DECLARE count :


INTEGER INTEGER INTEGER

count=0 count=0 FOR count = 0 TO 99

WHILE count<100 REPEAT PRINT count

PRINT count PRINT count NEXT

count=count+1 count=count+1

END WHILE UNTIL count=99

There is no need to initialize the counter within for LOOP, as it is already given in range.

There is no need for increments of 1 within for loop, as NEXT acts as built in counter.

Print 1st 50 Even Numbers(2-100)

DECLARE count : DECLARE count : DECLARE count :


INTEGER INTEGER INTEGER

count=2 count=2 FOR count = 2 TO 100

WHILE count < 101 REPEAT PRINT count

PRINT count PRINT count count=count+1

count=count+2 count=count+2 NEXT

ENDWHILE UNTIL count=100


Print 1st 50 ODD Numbers(1-99)

DECLARE count : DECLARE count : DECLARE count :


INTEGER INTEGER INTEGER

count=1 count=1 FOR count = 1 TO 99

WHILE count < 100 REPEAT PRINT count

PRINT count PRINT count count=count+1

count=count+2 count=count+2 NEXT

ENDWHILE UNTIL count=99

IMPORTANT IMPORTANT:

Take 50 numbers from user, and find out the average, highest and lowest of the numbers.

DECLARE num, count, DECLARE num, count, DECLARE num, count,


total, highest, lowest : total, highest, lowest : total, highest, lowest :
INTEGER INTEGER INTEGER

DECLARE avg : REAL DECLARE avg : REAL DECLARE avg : REAL

count=0, total=0, count=0, total=0, total=0, highest=0,


highest=0, lowest=9999 highest=0, lowest=9999 lowest=9999

WHILE count<50 REPEAT FOR count = 1 TO 50

PRINT “please enter a PRINT “please enter a PRINT “please enter a


number” number” number”

INPUT num INPUT num INPUT num

Total=total+num Total=total+num Total=total+num

IF num>highest THEN IF num>highest THEN IF num>highest THEN


highest=num highest=num highest=num

IF num<lowest THEN IF num<lowest THEN IF num<lowest THEN


lowest=num lowest=num lowest=num
Count=count+1 Count=count+1 Count=count+1

ENWDWHILE UNTIL count=49 NEXT

avg=total/50 avg=total/50 avg=total/50

PRINT avg,highest,lowest PRINT avg,highest,lowest PRINT avg,highest,lowest

Pseudocode.pdf

Example 5 to 14 [Please Solve]

Lesson 6

Exam Style Questions:

Write pseudocode to print average of numbers ending with -1.

(We can’t do this with FOR as there is no range given.)

DECLARE count, total, num : INTEGER DECLARE count, total, num : INTEGER

DECLARE avg : REAL DECLARE avg : REAL

count=0, total=0 count=0, total=0

PRINT “please enter a number” REPEAT

INPUT num PRINT “please enter a number”

WHILE num <> -1 INPUT num

total=total+num IF num <> -1 THEN

count = count +1 total=total+num

PRINT “please enter a number” count = count +1

INPUT num UNTIL num = -1


ENDWHILE avg=total/count

avg=total/count PRINT avg

PRINT avg

This is an issue using pre-


conditioned loop, that we need to This is an issue with post-conditioned
take user’s input twice when loop, as the condition is at the end of
working on a user defined value. the loop, so, for a user-defined value
there is always a need to validate the
1) To justify the while’s condition data being input.

2) For the loop to run again

Lesson 6:

Loops Practice

Duration : 1 Year

Number of Readings Per Day : 10

Input Temperatures

Highest Temperature Yearly

Lowest Temperature Yearly

Average Temp Per Day

Average Temp Yearly

DECLARE temp,HighTemp,LowTemp : INTEGER


DECLARE AverageDay,AverageYearly : REAL
DECLARE totalDay,totalYear : INTEGER
HighTemp=0,LowTemp=9999, totalYear=0

FOR day = 1 TO 365

totalDay=0

FOR readings=1 to 10

PRINT “please enter temperature”

INPUT temp

totalDay=totalDaty+temp

totalYear=totalYear+temp

IF temp>Highest THEN Highest=temp

IF temp<Lowest THEN Lowest=temp

Next readings

AverageDay=totalDay/10

PRINT AverageDay

NEXT day

AverageYear=totalyear/3650

PRINT AverageYear,Highest,Lowest

DECLARE time : INTEGER


DECLARE FS : INTEGER

DECLARE fastest,slowest,totalspeed : INTEGER

DECLARE avgspeed : REAL

fastest=0,slowest=999,totalspeed=0

FOR count = 1 TO 500

PRINT “please input time”

INPUT time

FS=200/time

PRINT FS

IF FS>fastest THEN fastest=FS

IF FS<slowest THEN slowest=FS

totalspeed=totalspeed+FS

NEXT

avgspeed=totalspeed/500

PRINT fastest,slowest,avgspeed

DECLARE item,cost : INTEGER

DECLARE itemcost,totalcost : INTEGER

DECLARE avgcost : REAL

totalcost=0

FOR count = 1 TO 1000

PRINT “please enter item type and cost of each item”

INPUT item,cost
IF item=1 THEN itemcost=cost*1.5

ELSEIF item=2 THEN itemcost=cost*2.5

ELSE item=3

itemcost=cost*5.0

ENDIF

PRINT itemcost

totalcost=totalcost+itemcost

NEXT

avgcost=totalcost/1000

PRINT avgcost

DECLARE distance,fuel,FEconomy : INTEGER

DECLARE best,worst,total : INTEGER

DECLARE average : REAL

best=0,worst=9999,total=0

FOR count = 1 TO 1000

PRINT “please enter distance travelled and fuel used?”

INPUT distance,fuel

FEconomy=distance/fuel

PRINT FEconomy

total=total+FEconomy

IF FEconomy>best THEN best=FEconomy

IF FEconomy<worst THEN worst=FEconomy


NEXT

average=total/1000

PRINT average,best,worst
Data Types:
INTEGER : whole numbers
REAL : fractional numbers
STRING : alphabets/name, alphanumeric/password, alphanumeric +
symbols/email, non-calculative data/IDs, Phone Numbers, Serial Numbers etc.
NOTE : Strings are encapsulated in double quotations. (“”)
CHAR : whenever there is a need to store one character only.
NOTE : Chars are encapsulated in single quotations.(‘’)
BOOLEAN : Two states/discrete behavior.
DATE & TIME : Data & Time.

NOTE : ALL in upper case.

Resords: A record is a collection of related fields, each of which can hold a


specific type of data. It is similar to a row in a database table or a struct in programming.

Arrays and records are both data structures used to store multiple values, but they serve different
purposes and have distinct characteristics.

Feature Array Record


A collection of elements A collection of fields, which
Definition
of the same data type. can be of different data types.
All elements must be of Each field can have a different
Data Type the same type (e.g., all type (e.g., integer, string,
integers, all strings). float).
Indexed (each element is Named fields (each field is
Structure
accessed using an index). accessed using its name).
Access elements using Access fields using a field
Access Method
an index (e.g., arr[0]). name (e.g., record.name).
Used when working with
Used when working with
heterogeneous data (e.g., a
Usage homogeneous data (e.g.,
student record with name, age,
a list of student ages).
and GPA).
Elements are stored in
Fields are stored together as a
Storage contiguous memory
unit in memory.
locations.
Iteration, searching, and
Field-based access and
Operations sorting are common
modification are common.
operations.
Storing test scores, Storing information about a
Example Use Case
temperatures, or names. person, book, or employee.
Feature Array Record

• Use an array when you need to


store multiple values of the same type
(e.g., a list of numbers).
• Use a record when you need to
store a structured collection of
related but different types of data
(e.g., an employee’s details: name, ID,
salary).

Lesson 7:

Arrays:

A list that can hold multiple data items of SAME DATA TYPE.
I want to store 10 numbers
10 variables : num1, num2, num3, num4, num5….num10
This is not a good approach, we can store these 10 numbers in only 1 data structure
DECLARE Nums: ARRAY[1:10] OF INTEGERS
1 REPRESNTS COLUMN AND 10 REPRESNTS ROWS

1 852

2 5250

3 0024

4 424

5 32543

6 041

7 04

8
9

10

Where the data item is being stored, The actual data being stored is the
that is the memory location, that’s dimension.
index. POSITION: INDEX
You may refer to dimensions as number of
YEH LOGICAL HOTA THEY DONOT columns.
EXIST PHYSICALLY
HAI(PROGRAMMING MEIN INDEX
STARTS FROM 0 BUT IN
PESUDOCODE IT DEPENDS UPON
US)

ARRAYS ARE ALWAYS


ACCESSED/MANIPULATED USING
THE INDEXES NOT THE VALUES, AS
BEING PROGRAMMERS WE WOULD
HAVE NO IDEA WHAT THE VALUE IS.

There is no need to make separate varisables we can simply use:


name of the array[x]
[x]: index od the array

Variable aur array mein difference yehi hai k hum arrays k sath indexes detay hai to
manipulate

Declare Arrays:

1D ARRAYS:

DECLARE Names : ARRAY[1:7] OF STRING

Practice: Declare a 1D array named students of type integer to store marks of 200
students.
DECLARE Students : ARRAY[1:200] OF INTEGER
How to Input or Output Data from the Array : Programmer Defined Data:

Declare a 1D array named Games of type String to store 5 Game names into the array.

Populate the array with game names and output the game names stored at the 3rd and 5th
index of the array.
DECLARE Games : ARRAY[1:5] OF STRING
Games[1]=”COD”
Games[2]=”Tekken 7”
Games[3]=”FIFA”
Games[4]=”Unchartered 4”
Games[5]=”Cricket”
PRINT Games[3], Games[5]

How to Input or Output Data from the Array: User Defined Data:

Declare a 1D array named Games of type String to store 5000 Game names into the array.
Populate the array with game names given by the user and output the game names
stored from 100 to 367th index.
DECLARE Games : ARRAY[1:5000] OF STRING
FOR count = 1 TO 5000
PRINT “please enter game name”
INPUT Games[count]
NEXT
FOR count = 100 to 367
PRINT Games[count]
NEXT

Practice: Set up a 1D array Marks to store marks for 300 students, output the marks
stored from 215th index to 278th index.
Manipulating 1D Arrays:

Set up a 1D array Marks to store marks for 300 students, find out the average, highest
and lowest marks.
DECLARE Marks : ARRAY[1:300] OF INTEGER
DECLARE total,highest,lowest : INTEGER
DECLARE avg : REAL
total=0,highest=0,lowest=101
FOR count = 1 TO 300
total=total+Marks[count]
IF Marks[count]>highest THEN highest=Marks[count]
IF Marks[count]<lowest THEN lowest=Marks[count]
NEXT
avg=total/300
PRINT avg,highest,lowest

Please solve: Set up a 1D array Info to hold info about the salaries of 800 employees,
take all the values from user and populate the array, output the average salary, highest
and lowest salaries, do find out the range as well where range=highest salary paid –
lowest salary paid.
DECLARE Info : ARRAY[1:800] OF INTEGER
DECLARE highest,lowest,average,range,total : INTEGER
total=0,highest=0,lowest=9999
FOR count = 1 TO 800
PRINT “please enter salaries”
INPUT Info[count]
total=total+Info[count]
IF Info[count]>highest THEN highest=Info[count]
IF Info[count]<lowest THEN lowest=Info[count]
NEXT
average=total/800
range=highest-lowest
PRINT average,range,highest,lowest

VERY IMPORTANT : STUDENTS OFTEN MIX UP THE CONCEPTS:

Take 50 numbers, output the average of Take 50 numbers, store them and output
the numbers. the average.
DECLARE num,total,average : INTEGER DECLARE num : ARRAY[1:50] OF
INTEGER
total=0 DECLARE total,average : INTEGER
FOR count = 1 TO 50 total=0
PRINT “please enter a number” FOR count = 1 TO 50
INPUT num PRINT “please enter a number”
Total=total+num INPUT num[count]
NEXT Total=total+num[count]
average=total/50 NEXT
PRINT average average=total/50
PRINT average

Linear Search TO FIND A DATA ITEM THROUGH THE ARRAY….

An algorithm that searches through the array index after index, until the record being
searched is found, or the whole array has been searched through.
1) Take a search value from user.
2) Start searching from 1st index.
3) Compare the values.
4) If they match, break the loop.
5) If they don’t match, jump to the next index.
6) Keep repeating until the value is found, or whole
7) searched.

PSEUDOCODE:
Vb code for linear search: keep one thing in mind, that I populated the array myself.
Print : console.writeline
Input : console.readline
Ending : console.readkey()
Declare : dim
Module Module1

Sub Main()
Dim isl(4) As Integer
Dim found As Boolean
found = False
Dim index As Integer
index = 0
Dim searchvalue As Integer
For count = 0 To 4
Console.WriteLine("please enter isl numbers")
isl(count) = Console.ReadLine
Next
Console.WriteLine("please enter search value")
searchvalue = Console.ReadLine
Do
If isl(index) = searchvalue Then
found = True
Else
index = index + 1
End If
Loop Until found Or index > 4
If found Then
Console.WriteLine("value found at: " & index)
Else
Console.WriteLine("value not found")
End If
Console.ReadKey()
End Sub

End Module
Lesson 9:

BUBBLE SORT:
It is an algorithm to arrange the array in an order either ascending or descending
• Compare the first two elements of the list.
• If the first element is greater than the second, swap them.
• Move to the next pair and repeat the comparison and swap if necessary.
• Continue this process for the entire list.
• After the first pass, the largest element is at the end.
• Repeat the process for the remaining elements, excluding the last sorted ones.
• Continue until no swaps are needed.
PSEUDOCODE:
DECLARE Names : ARRAY[1:5] OF STRING
DECLARE swap : BOOLEAN
DECLARE temp,top : INTEGER
top=5
REPEAT
swap=False
FOR count = 0 to top-1
IF Names[count]>Names[count+1] THEN
temp=Names[count]
Names[count]=Names[count+1]
Names[count+1]=temp
Swap=TRUE
NEXT
top=top-1
UNTIL NOT swap OR top=0

Lesson 10:

Random Numbers & String Handlers:

Random Numbers:

A function that returns a value in between 0 and 1.

Practice: Generate 300 random numbers in between 2000 to 2500.

Store them in a 1D array named numbers.


DECLARE Numbers : ARRAY[1:300] OF INTEGER
FOR count = 1 TO 300
Numbers[count]=INT(RAND(500)+2000))
NEXT

Practice: There is a need to generate 100 random OTPs for a bank. (60-780)

Generate the OTPs and store them into array named password, output the contents of array.
DECLARE Password : ARRAY[1:100] OF INTEGER
FOR count = 1 TO 100
Password[count]=INT(RAND(720)+60))
PRINT Password[count]
NEXT

String Handlers:

NOTE : Always applied on strings.


1) LEFT (Extract the characters from the left side)
2) RIGHT (Extract the characters from the right side)
3) MID (Sub String) (Extract the characters from any position within the string)
4) LOWER
5) UPPER
6) LENGTH (Gives exact number of characters in string, including, alphabets, numbers, symbols,
spaces)

DECLARE X : STRING
DECLARE Y,Z : INTEGER
X=”Programming is fun”
PRINT (Length(X))
Y=16
Z=3
PRINT(SubString(X,Y,Z))

//Please avoid passing values in parameters.

DECLARE Y,Z : INTEGER


PRINT (Length(X))
Y=10
PRINT(Left(X,Y))
PRINT (Right(X,Y))
Y=12
Z=9
PRINT (SubString(X,Y,Z))

Please cover the following topicals by Tuesday : Topic 9,10 and 11

Lesson 11: Array with more than one dimension. Datatypes should be same

2D Arrays:

(1,1)

(2,4)
(6,5)

(7,1)

Dimensions : 5

If an array has more than one dimension, 2D Array.

NOTE : To handle or manipulate 2D Arrays, you need to understand and have a good grip on co-
ordinates.

DECLARE:
DECLARE Students : ARRAY[1:7,1:5] OF STRING

example

Practice: A 2D array Data[] is required to store names, email, address and password for 500 employees.

Write pseudocode to declare the array.


DECLARE Data : ARRAY[1:500,1:4] OF STRING
1:500 represents 500 rows.
1:4 represents 4 columns.

Practice : A 2D array Info[] is required to store information about the clients, the information includes,
their name, address, city, country, occupation and martial status. Write pseudocode to set up an array
to store details for 2000 clients.

DECLARE Info : ARRAY[1:2000,1:6] OF STRING

INPUT & OUTPUT Data from 2D Array:

INPUT:
Store names and addresses of 200 students in a 2D array named students and output all the contents
as well.
DECLARE Students : ARRAY[1:200,1:2] OF STRING
DECLARE row,col : INTEGER
FOR row=1 TO 200
PRINT “please enter student names”
INPUT Students[row,1]
PRINT “please enter student addresses”
INPUT Students[row,2]
NEXT

OUTPUT:
FOR row = 1 TO 200
FOR col = 1 TO 2
PRINT (Students[row,col])
NEXT col
NEXT row

Practice : .
DECLARE Password : ARRAY[1:3000,1:4500] OF STRING
DECLARE row,col : INTEGER
FOR row= 1 TO 3000
FOR col=1 TO 4500
Password[row,col]=”Empty”
NEXT col
NEXT row

Lesson 12:

Manipulation of 2D arrays:

2023 Specimen Scenario

DECLARE total : ARRAY[1:ClassSize] OF INTEGER //Declaration


DECLARE dis,mer,pass,fail : INTEGER
dis=0,mer=0,pass=0,fail=0 //initialize
//Either you are using a variable or an array for totaling, highest, lowest this must be initialized to zero.
FOR count = 1 TO ClassSize //initializing complete array
total[count]=0
NEXT
DECLARE row, col : INTEGER //calculation of total marks
FOR row=1 TO ClassSize
FOR col = 1 TO SubjectNo
total[row]=total[row]+StudentMark[row,col]
NEXT col
NEXT row

FOR count = 1 TO ClassSize


Result=total[count]/SubjectNo
Avg=INT(Result+0.5) //using INT to round off
PRINT StudentName[count]
PRINT total[count]
PRINT Avg
IF Avg>=70 THEN //calculating average and printing
PRINT “Distinction”
dis=dis+1
ELSEIF Avg>=55 THEN
PRINT “merit”
mer=mer+1
ELSEIF Avg>=40 THEN
PRINT “pass”
pass=pass+1
ELSEIF Avg<40 THEN
PRINT “fail”
fail=fail+1
ENDIF
PRINT “Distinctions are: “,dis
PRINT “Merits are: ”,mer
PRINT “Pass are: ”,pass
PRINT “Fail are: ”,fail

Validation & Verification:

Validation:
Automatic checking by a program that data entered is reasonable before it is accepted into a computer
system.
Types:
1) Range Check
2) Length Check
3) Type Check
4) Presence Check
5) Format Check
6) Check Digits

Range Check:

Definition: Checks that the value of a number is in between an upper value and lower value.

Example: The marks entered by the student should be in between 0 and 100 incase max marks are 100.

Pseudocode:

While Repeat

PRINT “please enter marks” PRINT “please enter marks”

INPUT marks INPUT marks

WHILE marks<0 OR marks>100 REPEAT

PRINT “invalid marks, re-enter” IF marks<0 OR marks>100 THEN

INPUT marks PRINT “invalid marks, re-enter”

ENDWHILE INPUT marks

UNTIL marks>=0 AND marks<=100

Length Check:

Definition: Checks the data contains exact number of characters.

Example: The password should be 8 characters in length, so the password less than 8 or greater than 8
would be rejected, i.e., password with 7 or less, 9 or more will be rejected.

Pseudocode:

While Repeat

PRINT “please enter your password” PRINT “please enter your password”

INPUT Password INPUT Password


WHILE Length(Password)<>8 REPEAT

PRINT “invalid password, re-enter” IF Length(Password)<>8 THEN

INPUT Password PRINT “invalid password, re-enter”

ENDWHILE INPUT Password

UNTIL Length(Password)=8

Type Check:

Definition: Checks that the data entered is of sensible data type.

Example: Number of brothers or sisters should be an integer value i.e., Whole Number.

Pseudocode:

WHILE REPEAT

PRINT “Enter number of siblings” PRINT “Enter number of siblings”

INPUT Siblings INPUT Siblings

WHILE Siblings<>Div(Siblings,1) REPEAT

PRINT “invalid data, re-enter” IF Siblings<>Div(Siblings,1)

INPUT Siblings PRINT “invalid data, re-enter”

ENDWHILE INPUT Siblings

UNTIL Siblings=Div(Siblings,1)
Presence Check:

Definition: Checks that some data has been entered and field is not left blank.

Example: An email field for online transaction isn’t left empty.

Pseudocode:

WHILE REPRAT

PRINT “please enter your email” PRINT “please enter your email”

INPUT Email INPUT Email

WHILE Email=”” REPEAT

PRINT “enter some data” IF Email=””

INPUT Email PRINT “enter some data”

ENDWHILE INPUT Email

UNTIL Email<>””

Format Check:

Definition: Checks for a predefined pattern.

Example: For an id, the 1st 4 character should be alphabets, last 4 should be numbers.

WHILE REPEAT

PRINT “please enter id” PRINT “please enter id”

INPUT id INPUT id

WHILE Left(id,4)<>’a’ TO ‘z’ OR Right(id,4)<> REPEAT


‘0’ TO ‘9’

PRINT “invalid id, re-enter” IF Left(id,4)<>’a’ TO ‘z’ OR Right(id,4)<> ‘0’ TO


‘9’

INPUT id PRINT “invalid id, re-enter”

ENDWHILE INPUT id

UNTIL Left(id,4)=’a’ TO ‘z’ AND Right(id,4)= ‘0’


TO ‘9
Check Digit:

Definition: It is the final digit included in the code, it is calculated from all the other digits in the code.

Uses: Barcodes, Product Codes, ISBN(International Standard Book Numbers), VIN(Vehicle


Identification Numbers).

Detection of Errors:
1) An incorrect digit entered. E.g., 5327 entered instead of 5307.
2) Transposition errors where two numbers have changed order E.g., 5037 instead of 5307.
3) Omitted or extra digits: 537 instead of 5037, 53107 instead of 5307.
4) Phonetic errors, E.g., 13, thirteen instead of 30 or thirty.

Verification:
Definition: Checks data has been accurately copied from one source to another, checks data entry
for errors.
Types;
1) Double Data Entry
2) Visual Check
Double Data Entry:
Definition: Data is entered twice and then both the sets of data are compared.
Example: Password Re-confirmation
Visual Check:
Definition: Physical or manual checking of data once it is entered from another source into a computer.
Example: Comparing the data entered in a computer from original document or a visual screen
appears when ordering online that contains address and phone number.

Subroutines :
Set of instructions that are used repeatedly in a program.
When required it can be called many times during the execution of a program.

Note: Subroutines are not the part of a program.

1) Re-useable

2) They can be called several times in the program.

3) They enhance the execution time of the program.

Procedures : A subroutine that never returns a value.


Functions : A subroutine that always returns a value.

Sum of Two Numbers

PROCEDURE Sum (num1:INTEGER,num2:INTEGER) //the values being accepted by a


subroutine are parameters
DECLARE sum : INTEGER

Sum=num1+num2

PRINT Sum

END PROCEDURE

//Main Program

DECLARE n1,n2,ISL,Grade11 : INTEGER

PRINT “please enter a number”

INPUT n1

PRINT “please enter a number”

INPUT n2

CALL Sum(n1,n2) //values being passed from the main program to the sub -routine are
called arguments

PRINT “please enter two numbers”

INPUT ISL,Grade11

CALL Sum(ISL,Grade11)

Sum of Two Numbers

FUNCTION Sum (num1:INTEGER,num2:INTEGER) RETURNS INTEGER //the values being


accepted by a subroutine are parameters

DECLARE sum : INTEGER

Sum=num1+num2

RETURN Sum

END PROCEDURE
//Main Program

DECLARE n1,n2,ISL,Grade11 : INTEGER

PRINT “please enter a number”

INPUT n1

PRINT “please enter a number”

INPUT n2

Result=Sum(n1,n2) //values being passed from the main program to the sub -routine are
called arguments

PRINT Result

PRINT “please enter two numbers”

INPUT ISL,Grade11

Result2=Sum(ISL,Grade11)

PRINT Result 2

Grades of 200 Students

PROCEDURE Grade(numbers: INTEGER)

DECLARE Grade : STRING //local scope

IF numbers>=90 THEN

Grade=”A*”

ELSEIF numbers>=80 THEN

Grade=”A”

ELSE

Grade=”U”

ENDIF

PRINT Grade

END PROCEDURE
//Main Program

DECLARE marks : INTEGER //local variable

FOR count = 1 TO 200

PRINT “please enter your marks”

INPUT marks

WHILE marks<0 OR marks>100

PRINT “invalid marks entered, please re-enter”

INPUT marks

ENDWHILE

CALL Grade(marks)

NEXT

Global Variable : A variable that isn’t the part of any subroutine, and is declared outside all the
subroutines is global.
This variable can be used in any of the subroutines.

Local Variable: A variable that is a part of particular subroutine and can be used in only that
specific subroutine is local.

In this topic:

1) Subroutines
a) Procedures
b) Functions
c) Parameters
d) Arguments
e) Local and Global Variables/Identifiers

FUNCTION doubleValue(ByVal num : INTEGER)


RETURNS INTEGER num = num * 2
RETURN num
ENDFUNCTION

main()
myNumber = 5
result = doubleValue(myNumber)
PRINT "Result:", result // Output: Result: 10
PRINT "myNumber:", myNumber // Output: myNumber: 5 (unchanged)
END

//MAIN
def add_and_double(x, y):
x=x+5 # This change does not affect the original 'a'
y[0] = y[0] * 2 # Modifying an element in a list simulates 'ByRef'
return x

# Main program
a=3
b = [4] # Using a list to simulate 'ByRef' behavior for 'y'
result = add_and_double(a, b)
print("Result:", result) # Output: 8
print("a:", a) # Output: 3 (unchanged)
print("b[0]:", b[0]) # Output: 8 (changed)

File Handling: to store data so it can be accessed again and reused.


Modes:
Write (overwrite the existing data)
Read (read data from the file)
Append (write the data to the end of the file)

Note: you always need an identifier to access the data from the file, either you want to read, write or
append, you always need identifier
1-variable
2- array

Write Operation : (I will take one line/sentence/record from the user and store it into the file)
DECLARE textline : STRING
PRINT “please enter a text line”
INPUT textline
OPEN “ISL.txt” FOR WRITE
WRITEFILE,textline
CLOSEFILE “ISL.txt”
Write Operation : (multiple text lines)
DECLARE textline : STRING
PRINT “please enter a text line, empty to stop”
INPUT textline
OPEN “isl.txt” FOR WRITE
WHILE NOT EOF //JB TK FILE END NA HOJAE TB TK RUN HOGA
WRITEFILE,textline
PRINT “please enter a text line, empty to stop”
INPUT textline
ENDWHILE
CLOSEFILE “isl.txt”

READ OPERATIONS: (Single Line)


DECLARE textline : STRING
OPEN “11A.txt” FOR READ
READFILE,textline
PRINT textline
CLOSEFILE “11A.txt”

READ OPERATIONS: (Multiple Files/Complete File)


DECLARE textline : STRING
OPEN “11A.txt” FOR READ
WHILE NOT EOF
READFILE,textline
PRINT textline
ENDWHILE
CLOSEFILE “11A.txt”

//APPEND AUR WRITE FILE SAME HAI BS JAHA WRITE HAI WAHA APPEND AJAIGA

Program Development Life cycle


Analysis
Design
Coding
Testing
Maintenance

Analysis:
Understanding the problem and looking for required solutions:
Two stages of analysis:
Abstraction and decomposition

Abstraction: That keeps the key elements for the solution and discards any unnecessary elements Example
Google Maps that shows the traveling required and type of vehicle being used

Decomposition: Diving a problem into smaller parts and then into sub parts till it can be solved easily.
Design:
Once done with analysis there is a need to develop the system that is making structured charts, flowcharts and
pseudocodes.

Coding:
Program is developed using a high-level language, that is coding.
Iterative testing: modules are tested, if required the code is amended and tests are repeated until the program
works as required.

Testing:
The program is run many times using different test adat to make sure the program is running fine.
Live data
Normal data
Extreme data
abnormal data

You might also like