0% found this document useful (0 votes)
2 views16 pages

Programming Pattern Techniques

The document outlines common programming techniques, focusing on pseudocode functions for reading data from strings, arrays, and text files. It includes examples of extracting substrings and searching for specific patterns in variable-length strings. Additionally, it demonstrates how to calculate totals based on specific criteria from data files.

Uploaded by

Sooraj Rajmohan
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)
2 views16 pages

Programming Pattern Techniques

The document outlines common programming techniques, focusing on pseudocode functions for reading data from strings, arrays, and text files. It includes examples of extracting substrings and searching for specific patterns in variable-length strings. Additionally, it demonstrates how to calculate totals based on specific criteria from data files.

Uploaded by

Sooraj Rajmohan
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/ 16

Common Programming

TECHNIQUES
Identify the
datatypes
PSEUDOCODE Functions
PSEUDOCODE
Functions
PSEUDOCODE
Functions
PSEUDOCODE Functions
PSEUDOCODE Operators
Read character by character from a string
FOR count1 TO LENGTH(string)
NextCharMID(string,count,1) String<-”ArabUnity”

Position Values→ 1 2 3 4 5 6 7 8 9

A r a b U n i t y

count
Count=1 Count=2 Count=3 Count=4 Count=5 Count=6 Count=7 Count=8 Count=9
Read data one by one from an integer array
FOR count1 TO SIZE
dataA[count]

Position Values→ A[1] A[2] A[3] A[4] A[5] A[6] A[7] A[8] A[9]

18 22 33 55 78 89 65 54 26

count
Count=1 Count=2 Count=3 Count=4 Count=5 Count=6 Count=7 Count=8 Count=9
Read data from a text file as line by line
WHILE NOT EOF(“sample.txt”)
READFILE “sample.txt”,FileData
EOF Status

EOF → FALSE FileData 1#Apple


EOF → FALSE FileData 2#Mango
EOF → FALSE FileData 3#Orange
EOF → FALSE FileData 4#Grape
EOF → FALSE FileData 5#Pineapple

EOF → TRUE
Case-1
Fixed Number of Characters
Extract substrings from the line of text

Sample line of text from file


Example :- extract line number from the line of text LineNo
LineNoLEFT(FileData,1) 1 1 #Pineapple

Sample line of text from file


Example :- extract five characters from the right Name
NameRIGHT(FileData,5) apple 1 #Pineapple
Case-2
Variable length of characters
Extract substrings from the line of text
An example of one line from the file is:
“GB1234*Kevin Mapunga*07789123456”
The account number string may be six or nine characters in length and is unique for each person. It is made
up of alphanumeric characters only.

Example :- extract account number from line of text


Sample line of text from file
IF MID(FileData,7,1)==‘*’ GB1234*Kevin Mapunga*07789123456
THEN
accNoLEFT(FileData,6) Sample line of text from file
ELSE
GB1234567*Kevin Mapunga*07789123456
accNoLEFT(FileData,9)
ENDIF
Case-2
Variable length of characters
Extract substrings from the line of text
An example of one line from the file is:
“GB1234*Kevin Mapunga*07789123456”
The account number string ma be six or nine characters in length and is unique for each person. It is made up
of alphanumeric characters only.
Sample line of text from file

Example :- searching a particular account number GB1234*Kevin Mapunga*07789123456

IF accno&’*’=LEFT(FileData,LENGTH(accno)+1)
Suppose accno=GB1234
THEN
LENGTH(accno)=6
FoundTRUE
ENDIF

What is the reason for using ‘*’ in comparison?


Case-2
Variable length of characters
Extract substrings from the line of text
Array contains 100 elements. Each element format is as follows
<Name>‘:’<EmailAddress>
Sample line of text from file
Name and Email addresses are variable length strings.
Example :- Extract only name from the data element Rafreen:[email protected]
index1
Name<“ ”
WHILE MID(data,index,1)<>’:’ R a f r e e n :
NameName&MID(data,index,1)
indexindex+1
ENDWHILE Index1
Name Rafreen WHILE MID(data,index,1)<>’:’
indexindex+1
ENDWHILE
EmailRIGHT(data,LENGTH(data)-index)
Write pseudocode to extract email address.
Case-2
Variable length of characters
Extract substrings from the line of text
Each time a boat is hired out, details of the hire are stored in the file hire.txt. Each line of text )details of hire)
is as follows(Boat number – 2 digits, Date – 6 digits)
<BoatNumber><Date><AmountPaid>
Example :- Find the total amount paid for a particular boat
INPUT boatNum
Total0
WHILE NOT EOF(“hire.txt”) R a f r e e n :
READFILE “Hire.txt”,FileData
IF boatNum=STRING_TO_NUM(LEFT(FileData,2))
THEN Name
TotalTotal+STRING_TO_NUM(RIGHT(FileData,LENGTH(Filedata)-8))
ENDIF
ENDWHILE
Case-2
Variable length of characters
INPUT boatNum
Total0
WHILE EOF(“hire.txt”)
READFILE “Hire.txt”,FileData
IF boatNum=STRING_TO_NUM(LEFT(FileData,2))
THEN
TotalTotal+STRING_TO_NUM(RIGHT(FileData,LENGTH(Filedata)-8))
ENDIF
ENDWHILE
Sample data 73→ BoatNumber
73121298215 121298→ Date

Total=0+215
LENGTH(FileData) = 11 215
LENGTH(Filedata)-8 =3 Total=215
RIGHT(FileData,LENGTH(FileData)-8) TotalTotal+STRING_TO_NUM(RIGHT(FileData,LENGTH(FileData)-8))

You might also like