You are on page 1of 12

Arrays:

Store multiple items/elements of same data type.


It acts as a list at the backend. (Data Structures) (Stacks, Queues, Linked Lists)
1 “Haseeb”
2 “…”
3 “…”
4 “…”
5 “…”
6 “…”
7 “…”
8 “…”
Positions Content/Data
Index DIMENSION

DECLARE AN ARRAY:
Identifiers.
1) Constants
2) Variables
3) Arrays
CONST pi=3.14.16 : REAL
DECLARE num : INTEGER
1D:
DECLARE Names[1:10] : STRING

1 “Haseeb” “Gilani”
2
3
4
5
6
7 Kazmi
8

2D:
DECLARE Names[1:2,1:8] : STRING

Set up an array and put some programmer defined data and then I wanna output the data.

DECLARE Games[1:5] : STRING


//input programmer defined data
Games[1]=”Unchartered”
Games[2]=”FIFA”
Games[3]=”Tekken”
Games[4]=”Assassins”
Games[5]=”COD”
//output data
PRINT Games[3],Games[5]
//I wanna take a value from the user and output that. (Output user-defined value)
PRINT “please enter a number between 1 TO 5, I will tell you which game it is”
INPUT num
//Validation
WHILE num<1 OR num>5
PRINT “bhai tjhay kaha b hai 1 say 5 k darmayan enter kr, ni to laga reh”
INPUT num
ENDWHILE
PRINT Games[num]

//Lets 😊 Input user defined values.


//take 500 marks from the user and input in array and then output the marks between 333-457
from that array.
DECLARE Marks[1:500] : INTEGER
FOR count = 1 TO 500
PRINT “please enter marks”
INPUT Marks[count]
NEXT
FOR count= 333 TO 457
PRINT Marks[count]
NEXT

1) Set up a data structure to store 500 marks of students. Take all values from the user and store
them into your 1D array. Output only the Marks those which were greater than 90. Put a
validation check to ensure the marks are between 1 to 100 only.

DECLARE Marks[1:500] : INTEGER


FOR count= 1 TO 500
PRINT “please enter marks”
INPUT Marks[count]
WHILE Marks[count]<1 OR Marks[count]>100
PRINT “please enter marks in between 1 TO 100”
INPUT Marks[count]
IF Marks[count]>90 THEN
PRINT Marks[count]
NEXT
I wanna search through the array… 😊 (Mr. Linear Search)
DECLARE Names[1:250] : STRING
DECLARE size : INTEGER
DECLARE count : INTEGER
DECLARE name : STRING
DECLARE found : BOOLEAN
Found=False
Size=250
//input user defined data (if required by the examiner)
FOR count= 1 TO 250
PRINT “please enter a name”
INPUT Names[count]
NEXT
PRINT “please enter a name to search”
INPUT name
count=1
REPEAT
IF Name=Names[count] THEN
Found=True
ELSE
Count=count+1
ENDIF
UNTIL Found OR count>size
IF Found THEN
PRINT “name found at this position”, count
ELSE
PRINT ”name not found”
ENDIF
//if you wanna output the item you got as well
IF Found THEN
PRINT “name found at this position”, count, “and the name is”, Names[count]
ELSE
PRINT ”name not found”
ENDIF

P2 Pseudocode
1) Basics
2) Selection
3) Repetition (Max, Min, Average)
4) Arrays
i) Input Prog defined
ii) Output Prog defined
iii) Input User defined
iv) Output User defined
v) Array manipulation (output selective data)
vi) Validation
vii) Linear Search
viii) Global Subroutines
ix) Bubble Sort
1) DIV
2) MOD
3) Round
4) Random
5) String Handlers
i) Left
ii) Right
iii) Mid (Sub String)
iv) Length
v) Ucase
vi) Lcase
6) Array Handling
7) 2D Arrays
8) Validation (Pseudocode)
9) Filing
10) Structured Charts
11) Program Development Life Cycle
12) Test Data
13) Validation & Verification
14) Sub routines (Procedures & Functions)
15) Local & Global Variables

Global Subroutines:
1) Length
2) Left
3) Right
4) Mid (Sub String)
5) UCase
6) LCase

Let’s Try 😊
Length(Haseeb)=6
Left(Haseeb,3)=Has
Right(Haseeb,4)=seeb
Mid(Haseeb,3,3)=see
UCASE(Haseeb)=HASEEB
LACASE(HASEEB)=Haseeb
9DIV2=4
17MOD3=2 (Remainder)
Round(7.9,0)=8
Round(7.9,2)=8.00
INT(7.9)=7
Bubble Sort:
Array has been set/declared, data is stored. Array contains 500 Numbers and there is a need to
arrange that array in ascending order.
DECLARE Nums[1:500] : INTEGER
DECLARE count,upperbound,lowerbound,top,temp : INTEGER
DECLARE swap : BOOLEAN
lowerbound=1, upperbound=500, top=upperbound
REPEAT
FOR count= lowerbound TO Top-1
Swap=False
IF Nums[count]>Nums[count+1] THEN
Temp=Nums[count]
Nums[count]=Nums[count+1]
Nums[count+1]=temp
Swap=TRUE
ENDIF
NEXT
Top=top-1
UNTIL (NOT swap) OR top=0

Generate 500 Random Numbers and store them in an array. (random numbers should be in
between 1 TO 2000)
DECLARE Random[1:500] : INTEGER
FOR count = 1 TO 500
Random[count]=INT(RAND(2000)+1)
NEXT

Set an array to store 500 numbers. Generate 500 random numbers in between 7500 to 8500 and
store in that array.
Output the numbers those between 8000 to 8200.
IF a number is in between 7500 to 8000 output accepted.
IF a number is in between 8000 to 8300 output okay.
IF a number is in between 8300 to 8500 output rejected.
Find the average of the numbers.
The highest and lowest number as well.
Sort the numbers into ascending order. [25]

DECLARE Numbers[1:500] : INTEGER


DECLARE count,highest,lowest,total : INTEGER
DECLARE AverageArray : REAL
Total=0,highest=0,lowest=8600
FOR count = 1 TO 500
Numbers[count]=INT(RAND(7500,8500)+1)
IF Numbers[count]>=8000 AND Numbers[count]<=8200 THEN
PRINT Numbers[count]
ENDIF
IF Numbers[count]>=7500 AND Numbers[count]<=8000 THEN
PRINT “accepted”
ELSEIF Numbers[count]>=8000 AND Numbers[count]<=8300 THEN
PRINT “okay”
ELSE
PRINT “rejected”
ENDIF
Total=total+Numbers[count]
IF Numbers[count]>highest THEN highest=Numbers[count]
IF Numbers[count]<lowest THEN lowest=Numbers[count]
NEXT
AverageArray=total/500
PRINT AverageArray,highest,lowest

Validation & Verification:


Verification:
A method to check data entry for errors i.e., data as been copied correctly from one source to
another.

1) Double Data Entry


2) Visual Check

i) Double Data Entry:


Data is entered twice and both the sets of data are compared.
ii) Physical check performed by user to check that data has been entered correctly. E.g.,
data is displayed on users screen twice.

Validation:
An automatic check that makes sure data entered by the user is reasonable before it is accepted
by the system.
i) Range Check:
The value being entered is within a specific range i.e., it is between a lower and upper
value.
PRINT “Please enter marks” PRINT “Please enter marks”
INPUT Marks INPUT Marks
WHILE Marks < 0 AND Marks > 100 REPEAT
PRINT “please enter valid marks” IF Marks < 0 OR Marks > 100 THEN
INPUT Marks PRINT “please re-enter your marks”
ENDWHILE INPUT Marks
UNTIL Marks>=0 OR Marks <=100
ii) Length Check:
Data contains exact number of characters.

PRINT “Please enter your password of 8 PRINT “Please enter your password of 8
chars” chars”
INPUT Password INPUT Password
WHILE LENGTH(Password) <> 8 REPEAT
PRINT “please enter valid password” IF LENGTH(Password) <> 8 THEN
INPUT Password PRINT “please re-enter your password”
ENDWHILE INPUT Password
UNTIL LENGTH(Password)=8

Range Check Using Length.


PRINT “please enter family name based on PRINT “please enter family name based
10 to 30 characters” on 10 to 30 characters”
INPUT FN INPUT FN
WHILE LENGTH(FN)<10 OR REPEAT
LENGTH(FN)>30
PRINT “Invalid name entered” IF LENGTH(FN)<10 OR LENGTH(FN)>30
INPUT FN PRINT “Invalid name entered”
ENDWHILE INPUT FN
UNTIL LENGTH(FN)>10 AND
LENGTH(FN)<30

iii) Type Check:


Data being entered is of same data type e.g., number of siblings should be 3 or 4.
PRINT “please enter number of siblings in PRINT “please enter number of siblings
whole number” in whole number”
INPUT Sib INPUT Sib
WHILE Sib <> DIV(Sib,1) REPEAT
PRINT “please renter in whole numbers” IF Sib <> DIV (Sib,1) THEN
INPUT Sib PRINT “please renter in whole numbers”
ENDWHILE INPUT Sib
UNTIL Sib=DIV(Sib,1)

iv) Presence Check:


It checks some data has been entered and the value isn’t blank.
PRINT “please enter you email” PRINT “please enter you email”
INPUT EA INPUT EA
WHILE EA <> “” REPEAT
PRINT “please enter email, don’t leave it IF EA = “” THEN
blank”
INPUT EA PRINT “please enter email, don’t leave it
blank”
ENDWHILE INPUT EA
UNTIL EA<> ””

v) Format Check:
Data entered should be equal to pre-defined structure e.g., A school ID ISL123 based 3
alphabets and 3 numbers should follow the same pattern.
vi) Check Digit:
Final Digit of a code that is calculated from other digits within a code.
E.g., Barcodes, product codes, ISBN, VIN
Error Detections of Check Digits:
1) Incorrect Digit. E.g., 45094589
2) Transposition Errors. E.g., 45094590
3) Omitted Digits. E.g., 4509450/459
4) Extra Digits. E.g., 450945009/45091
5) Phonetic Errors. E.g., 13Thirty/30
Test Data:
Data to check the system responds/outputs correctly.
1) Normal data(within the range and acceptable)
2) Abnormal/Erroneous Data (outside the rage and rejected)
3) Extreme (highest and lowest acceptable values)
4) Live (with known outcomes)

Range 10-200
Normal Abnormal Extreme
10 ok
0 Ok
9 Ok
19 Ok
21 ok
Thirteen ok
13 Ok
200 ok
197 Ok

5) Boundary (sets) (two values) (accepted/rejected)


Range 10-200
i) (10,9)
ii) (200,201)

2D ARRAYS:

1,1 1,2 1,3 dgahgdaw ajds@ BGTL ISL LGS KAI BTSC
2,1 2,2

DECLARE Haseeb[1:8,1:10] : STRING


//Vertically
FOR col = 1 TO 10
FOR row = 1 TO 8
PRINT “please enter data”
INPUT Haseeb[row,col]
NEXT row
NEXT col
//Horizontally
//Vertically
FOR row = 1 TO 8
FOR col = 1 TO 10
PRINT “please enter data”
INPUT Haseeb[row,col]
NEXT col
NEXT row

Structure Charts:
Pictorial representation/Decomposition of a system into sub-systems(Modules) until one system
performs its own task.
Module is :
Sub Routine :
1) Function (it returns a value)
2) Procedure (never returns a value)

2D Continued:
Set up a 2D array to hold 100 names and emails.
Take data from the user and store that in your array.
DECLARE Data[1:100,1:2] : STRING
//SEPERATELY
Col=1
FOR row= 1 TO 100
PRINT “please enter names”
INPUT Data[row,col]
NEXT
Col=2
FOR row= 1 TO 100
PRINT “please enter emails”
INPUT Data[row,col]
NEXT
//together
Col=1
FOR row = 1 TO 100
PRINT “please enter names and emails”
INPUT Data[row,col]
INPUT Data[row,col+1]
NEXT

Subroutines:

Structured Programming:

PROCEDURE

Let’s Print Stars:


PROCEDURE Stars (X : INTEGER)
DECLARE count : INTEGER
FOR count = 1 TO X
PRINT “*”
NEXT
END PROCEDURE
DECLARE num
PRINT “please enter a number to show stars”
INPUT num
Call Stars(num) //argument
PRINT “please enter another number”
INPUT num
Call Stars(num)
FUNCTION:
KGS TO GRAMS:
FUNCTION MyConverter(X:REAL) RETURNS REAL
DECLARE Grams : REAL
Grams=X*1000
RETURNS Grams
END FUNCTION
DECLARE KGS : REAL
PRINT “please enter weight in kgs”
INPUT KGS
CALL MyConverter(KGS)

Local and Global Variables:

KGS TO GRAMS:
FUNCTION MyConverter(X:REAL) RETURNS REAL
DECLARE Grams : REAL
Grams=X*1000
RETURNS Grams
END FUNCTION
DECLARE KGS : REAL
PRINT “please enter weight in kgs”
INPUT KGS
CALL MyConverter(KGS)

Gram is a part of a sub routine i.e., MyConverter, so its user is restricted to that particular sub routine, it
can’t be used within the actual program.
On the other hand KGS is Declared within actual program, so it can be used anywhere required.
Gram : Local Variable
(scope restricted to MyConverter)
KGS : Global variable
(scope not restricted)

File Handling:
3 Modes:
1) Write (overwrites)
2) Read (to read)
3) Append (to add data at the end of the file)
NOTE: For every mode you’ve to open and close the file.
Up to you if you want to directly use the file name or you want to save the file name in an identifier and
then use that identifier in rest of your program.

DECLARE TextLine : STRING


DECLARE MyFile : STRING
MyFile=”Arrays BGTL.docx”
//Writing into the file//Ovewrite
OPEN MyFile FOR WRITE
PRINT “please enter line of text to write in the file”
INPUT TextLine
WRITEFILE, TextLine
CLOSEFILE (MyFile)
//Append//adds to the end of the file
OPEN MyFile FOR APPEND
PRINT “please enter line of text to write in the file”
INPUT TextLine
APPENDFILE, TextLine
CLOSEFILE (MyFile)
//READ//OUTPUT
OPEN MyFile FOR READ
READFILE,TextLine
PRINT TextLine
CLOSEFILE (MyFile)

STATEMENT : EOF (End of File)


To read complete file.
WHILE NOT EOF
OPEN MyFile FOR READ
READFILE,TextLine
PRINT TextLine
CLOSEFILE (MyFile)
ENDWHILE

You might also like