0% found this document useful (0 votes)
7 views6 pages

Library Routine, String Handling, File Handling

The document provides an overview of library routines in computer programming, detailing their definitions, features, benefits, and drawbacks. It includes specific pseudocode examples for mathematical operations using library routines such as MOD, DIV, ROUND, and RANDOM, as well as string handling functions like LENGTH, UCASE, LCASE, and SUBSTRING. Additionally, it covers file handling operations, explaining how to read from and write to files using pseudocode commands.

Uploaded by

dasishaan99
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)
7 views6 pages

Library Routine, String Handling, File Handling

The document provides an overview of library routines in computer programming, detailing their definitions, features, benefits, and drawbacks. It includes specific pseudocode examples for mathematical operations using library routines such as MOD, DIV, ROUND, and RANDOM, as well as string handling functions like LENGTH, UCASE, LCASE, and SUBSTRING. Additionally, it covers file handling operations, explaining how to read from and write to files using pseudocode commands.

Uploaded by

dasishaan99
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

Computer Science

Teacher: Maruf Ahmed


Library routines, String handling, File handling
Program libraries / Library routines
Definition
• A library routine is a pre-written and tested block of code that performs a common task.
• These routines are stored in a program library and can be called when needed.
• They allow programmers to reuse code without rewriting it.
Features of program libraries / library routines
• A library contains pre-written, pre-tested subroutines.
• These routines can be imported or used directly in a program.
• They perform common tasks that programmers often need.
Benefits of using library routines
• Saves programming time because the code does not need to be written from scratch.
• The routines are already tested, so they are more reliable.
• Provides useful functions (such as mathematical or string routines) that are ready to use.
Drawbacks of using library routines
• A routine may not match the exact needs of the program.
• The program depends on the availability of the library.
Library routines in pseudocode:
You are allowed to use the following mathematical library routines in pseudocode:
MOD (x, y):
• Used to perform integer division and return the remainder.
• Example:
o MOD(7, 2) returns 1.
Purpose: To return the remainder when x is divided by y using integer division.
DIV (x, y):
• Used to perform integer division and return the quotient, discarding any decimal part.
• Example:
o DIV(9, 4) returns 2.
Purpose: To return the whole-number part (quotient) of x / y, discarding any decimal part.
ROUND (v, d):
• Returns a value rounded to a specified number of decimal places.
• Rounds up if the next digit is >=5; otherwise, it keeps the digit the same.
• Example:
o ROUND (4.567, 1) returns 4.6.
Purpose: To return the number rounded to the specified number of decimal places.
RANDOM ( )
• Generates a pseudo-random real number between 0 and 1.
• To get a random number in a bigger range, you simply multiply it.
• Example:
o RANDOM () * 10 returns a real value between 0 and 10
Purpose: To return a random real number within a given range.

N.B. Every library routine must have brackets ( ) after its name. It must receive the correct number of
parameters, and each parameter must be of the correct data type required by that routine.

Page 1 of 6
Here are some examples of the use of these library routines in pseudocode:
Value1 ← MOD (10, 3)
This returns the remainder after dividing 10 by 3. The result is 1, so Value1 becomes 1.
Value2 ← DIV (10, 3)
This returns the integer quotient (whole-number part) of 10 / 3. The result is 3, so Value2 becomes 3.
Value3 ← ROUND (6.97654, 2)
This returns the number rounded to 2 decimal places. The result is 6.98, so Value3 becomes 6.98.
Value4 ← RANDOM ()
RANDOM() always returns a random real number between 0 and 1, including 0 and 1. It does not take any
parameters. It automatically generates values in decimal form such as: 0.12, 0.85, 0.001, 0.02, etc.

N.B. MOD and DIV may also be used as operators in pseudocode.

Problem: Using library routine, RANDOM (), generate 20 whole numbers in the range from 0 to 50.
Display each generated number. Write down the required pseudocode.
DECLARE Count : INTEGER
DECLARE Rnd, WholeRnd : REAL
FOR Count ← 1 TO 20
Rnd ← RANDOM () * 50 //This will multiply each generated random number by 50
WholeRnd ← ROUND (Rnd, 0) //This will round each generated number to nearest integer to 0 decimal
//place
//The above two statements can be written as one statement like
//WholeRnd ← ROUND (RANDOM() * 50, 0)
OUTPUT WholeRnd
NEXT Count
N.B. If the range of random values starts from 0 then multiply the RANDOM () function with the end of the
range value and use round function to 0 decimal place to be regarded as a whole number. In this case
multiply the RANDOM () function by 50.
Problem: Using library routine RANDOM (), generate 10 whole numbers in the range from 10 to 40.
Display each generated number. Write down the required pseudocode.
DECLARE Count : INTEGER
DECLARE Rnd, WholeRnd : REAL
FOR Count ← 1 TO 10
Rnd ← RANDOM () * 30 //This will multiply each generated random number by 30
WholeRnd ← ROUND (Rnd, 0) + 10 //This will round each generated number to nearest integer to 0
//decimal place and add each value by 10
//The above two statements can be written as one statement like
//WholeRnd ← ROUND (RANDOM() * 30, 0) + 10
OUTPUT WholeRnd
NEXT Count
Process to generate a range of whole numbers that do not start from zero:
• Use RANDOM ()
o This gives a real number between 0 and 1.
• Multiply by the size of the range
o Range size = end value − start value
• Add the start value
o This shifts the number into your desired range.
Page 2 of 6
• Round to 0 decimal places
o Use ROUND (..., 0) to make it a whole number.

How to convert a number into next integer?


Write down the pseudocode that converts a decimal number into next integer (next whole number)
X ← ROUND (20.43 + 0.5, 0)
Y ← ROUND (20.0 + 0.5, 0)
Explanation:
• Add 0.5 to any number.
• Then use ROUND (..., 0) to round it to 0 decimal places.
• This gives the next whole number for any non-integer value.
String / Character handling
A string is a sequence of characters used to store text. A string may contain: letters, digits, symbols, spaces.
A string may be empty (contains zero characters) or contain many characters, up to the limit allowed by the
language. In pseudocode, any variable that stores zero or more characters must be declared as the STRING
data type. Literal STRING values must be written using double quotation marks " ". However, if a variable
is required to store only one character, then the data type should be CHAR, and literal CHAR values must be
written using single quotation marks ' '.
Character Positions: Each character in a string has a position number. Different programming languages
number characters differently: some start from 0, some start from 1. The first character of the string can be
position zero or one in O Level pseudocode also.
In all the following examples, we will assume the string position starts from 1 in pseudocode.
The following string handling library routines are included in your syllabus:
- LENGTH ()
- UCASE ()
- LCASE ()
- SUBSTRING ()
LENGTH (X) – takes a STRING as its parameter and returns the number of characters in that string. Every
character is counted. Spaces also count as characters. The return value is an integer.
For example, the length of the string “Computer Science” is 16 characters.
X ← LENGTH (“Computer Science”)
16 will be assigned to X
X ← LENGTH (MyString)
Whatever number of characters are stored inside MyString that number will be returned by LENGTH () and
stored in X
For example:
If MyString = "Hello" → length is 5
If MyString = "" (empty string) → length is 0
UCASE (X) – takes a STRING as its parameter and returns the same string converted to uppercase.
Key points:
• All letters become capital letters.
• Any non-letter characters (digits, symbols, punctuation, spaces) remain unchanged.
• The function returns a STRING.
X ← UCASE (“Computer Science”)
Every character will be converted into capital letters so “COMPUTER SCIENCE” would be assigned to X
X ← UCASE (MyString)
Whatever is stored in variable, MyString will be converted into capital letters and then assigned to X
Page 3 of 6
LCASE (X) – takes a STRING as its parameter and returns the same string converted to lowercase.
Key points:
• All letters become lowercase.
• All non-letter characters (spaces, digits, punctuation, symbols) stay the same.
• The return value is a STRING.
For example, the string “Computer Science” would become “computer science”.
X ← LCASE (“Computer Science”)
Every character will be converted into small letters so “computer science” would be assigned to X
X ← LCASE (MyString)
Whatever is stored in variable, MyString will be converted into small letters and then assigned to X
Important note about UCASE () and LCASE ()
The functions UCASE () and LCASE () cannot be used directly inside an INPUT statement. You must first
read the input, and then apply the function to the stored value.
This is because in 2210 pseudocode:
• INPUT can accept only a variable name.
• You cannot call functions inside INPUT.
For example,
OUTPUT “Enter a line of text:”
INPUT Text
Text ← UCASE (Text)
SUBSTRING (X, Y, Z) – It extracts part of a string and returns it as a new string.
It requires three parameters:
• Text (STRING) – The original text you want to extract from.
• Start (INTEGER) – The starting position in the string.
• Quantity/length (INTEGER) – The number of characters to extract from Start
For example, given the string: "Computer Science"
If the exam asks for the substring "Science", then:
"Science" starts at position 10
The word has 7 characters
So:
SUBSTRING ("Computer Science", 10, 7)
This returns: "Science"
X ← SUBSTRING (MyString, 5, 4)
Meaning:
• The extraction begins at the 5th character of MyString.
• Exactly 4 characters starting from that position are taken.
• These 4 characters are returned as a STRING and stored in X.
How two strings are compared?
String comparison is done character by character, starting from the first character of each string.
Internally, the computer uses the ASCII values of characters:
• Characters with lower ASCII values are considered “smaller”
• Characters with higher ASCII values are considered “greater”
Suppose there are two strings "abc" and "acd" to be compared.
The first character of the first string is compared with the first character of the second string. This is known
as comparing corresponding characters. The ASCII value of each character is used internally for the
comparison.
• If the first characters are the same, the second characters of each string are compared.
Page 4 of 6
• This process continues character by character until:
o a difference is found, or
o the end of one or both strings is reached.
In this example:
• 'a' is equal to 'a'
• 'b' is compared with 'c'
• Since the ASCII value of 'b' is less than the ASCII value of 'c', the string "abc" is considered lower
than the string "acd".
File handling
Computer programs often need to store data in a file so that it can be used again later. Data held in RAM is
temporary and will be lost when the computer is switched off. When data is saved to a file, it is stored
permanently on secondary storage.
Purpose of storing data in a file:
• To provide permanent storage of data so it is not lost when the program ends or the computer is
switched off.
• To allow data to be reused or accessed later by the same program or by another program.
• To transfer data between different programs or different computers.
• To create archives or backups for future reference or for recovering data if it is lost.
According to the syllabus, file-handling operations should be able to do the following:
• Open, close, and use a file for reading or writing.
• Read and write single items of data.
• Read and write a line of text.
File Modes
• File modes refer to the purpose for which a file is opened.
The following file mode is used:
READ mode:
• Used to retrieve data from an existing file.
• An error will occur if the program tries to open a file in READ mode when that file does not exist.
WRITE mode:
• Used to send or store data in a file.
• When a file is opened in WRITE mode, a new file is created. If a file with the same name already
exists, its existing contents will be overwritten.
N.B. Reading data from a file is considered INPUT because the file sends data to the computer system, and
the computer receives that data.
Writing data to a file is considered OUTPUT because the computer system sends data to the file, and the file
receives that data.
N.B. A file should be opened in only one mode at a time.
These are the pseudocode commands to be used to work with files
- OPENFILE
- READFILE
- WRITEFILE
- CLOSEFILE
Syntax for using file-handling commands
A file must be opened before it can be used. The programmer must state the mode of operation before
reading from or writing to the file. The basic syntax is:

Page 5 of 6
OPENFILE <File identifier> FOR <File mode>
File Identifier is the name of the file, written as a string.
In this syllabus, the files you work with are text files and typically use the .txt extension.
Data is read from a file (after it has been opened in READ mode) using the READFILE command:
READFILE <File Identifier>, <Variable>
When this command is executed, one data item is read from the file and stored in the variable.
Data is written to a file (after it has been opened in WRITE mode) using the WRITEFILE command:Data is
written into the file after the file has been opened using the WRITEFILE command as follows:
WRITEFILE <File identifier>, <Variable>
When this command is executed, the value of the variable is written into the file.
Every file must be closed when it is no longer needed, using the CLOSEFILE command:
CLOSEFILE <File identifier>
File handling operations:
Every file is identified by its filename. In this section, we focus on how to read and write a single line of text
or a single item of data to a file. According to your syllabus, only one line of text (not multiple lines) will be
used when reading from or writing to a file.
Below are pseudocode examples that show how to write a line of text to a file and how to read that line back
from the file. Each step in the pseudocode includes comments to explain what is happening.

DECLARE TextLine : STRING


N.B. Any constant declared to hold a file name must be of STRING
CONSTANT MyFile ← “[Link]” //[Link] file has been assigned to constant MyFile
OPENFILE MyFile FOR WRITE // opens file in WRITE mode
The above line could have been written in the following way also by avoiding CONSTANT:
OPENFILE “[Link]” FOR WRITE // opens file in WRITE mode
OUTPUT "Please enter a line of text"
INPUT TextLine
WRITEFILE MyFile, TextLine //Whatever has been given as input in the variable TextLine, will be
// passed to the file "[Link]" and the data will be stored there
CLOSEFILE MyFile // closes the file
// reading a line of text from a file
DECLARE TextLine : STRING
OPENFILE “[Link]” FOR READ // opens file in READ mode
READFILE “[Link]”, TextLine // reads a line of text from the file in TextLine
OUTPUT "The file contains this line of text:", TextLine //Displays the line of text on the screen
CLOSEFILE “[Link]” //closes the file
Example – File handling operations using two different modes together
This example uses the READ and WRITE operations together, to copy a line of text from [Link] to
[Link]
DECLARE LineOfText : STRING
OPENFILE “[Link]” FOR READ
OPENFILE “[Link]” FOR WRITE
READFILE “[Link]”, LineOfText
WRITEFILE “[Link]”, LineOfText
CLOSEFILE “[Link]”
CLOSEFILE “[Link]”

Page 6 of 6

You might also like