0% found this document useful (0 votes)
11 views

Library Routine, String Manipulation and Files

Uploaded by

Tleance
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

Library Routine, String Manipulation and Files

Uploaded by

Tleance
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Computer Science

Teacher: Maruf Ahmed


Library routine, string manipulation, File handling & Maintainable program

Library routines
A library routine is a debugged block of code, often designed to handle commonly occurring problems or
tasks. Library routines are stored in a program library and given names. This allows them to be called
into immediate use when needed, even from other programs. They are designed to be used frequently.
Features of library routines:
 Pre-existing / pre-compiled / pre-written modules / code
 Can be linked into a program without amendment
 Performs common / complex tasks
Benefits of using library routines:
 Code does not have to be written/re-written from scratch
 Code is already tested so it is more robust/likely to work/ should be relatively free from errors
 Precompiled
 Saves programming time
 The programmer can use e.g. mathematical functions that s/he may not know how to code
 If there is an improvement in the library routine the program updates automatically
Drawbacks of using library routines:
 Compatibility issues: may not work with the other code/may require changing program for it to
work
 Not guaranteed thorough testing
 may be unknown or unexpected bugs / virus
 Library routine may not meet exact needs
 If library routine is changed there may be unexpected results / errors

You may need to use the following library routines in your pseudocode programs for IGCSE Computer
Science:
MOD( ) – Is used to perform integer division when one number is divided by another number and find
the remainder.
DIV( ) – Is used to perform integer division when one number is divided by another number and only the
whole number part of the answer (quotient) is retained.
ROUND( ) – Is used to return a value rounded to a specified number of digits / decimal places. The result
will either be rounded to the next highest or the next lowest value depending on whether the value of the
preceding digit is >=5 or <5.
RANDOM( ) – Is used to generate random / pseudo numbers within a specified range.
Here are some examples of these library routines in pseudocode:
Value1 ← MOD(10, 3) returns the integer remainder after the division of 10 by 3 and the result 1 will be
assigned to Value1. Data type of Value1 must be integer. Both the parameters that will be passed must be
integer also.
Value2 ← DIV(10, 3) returns the integer quotient after the division of 10 by 3 and the result 3 will be
assigned to Value2. Data type of Value2 must be integer. Both the parameters that will be passed must be
integer also.
Value3 ← ROUND(6.97654, 2) returns the value rounded to 2 decimal places and the result 6.98 will be
assigned to Value3. Data type of Value3 must be real. The first parameter of ROUND must be REAL and
the second parameter must be integer starting from 0.
Value4 ← RANDOM() returns a random number between 0 and 1 inclusive in decimal form. Data type
Page 1 of 4
of Value4 must be real.
N.B. MOD and DIV may also be used as operators in pseudocode.

String manipulation
Strings are used to store text. Every string contains a number of characters, from an empty string, which
has no characters stored, to a maximum number specified by the programming language. The characters
in a string can be labeled by position number. The first character position in a string can start from 1
(one) or 0 (zero). But in pseudocode, it mostly starts from 1. In all the following examples, we will
assume the string position starts from 1.

The following string manipulation library routines are included in your syllabus:
- LENGTH()
- UCASE()
- LCASE()
- SUBSTRING()

LENGTH( ) – Takes STRING as a parameter and returns the number of characters in the string. For
example, the length of the string "Computer Science" is 16 characters as spaces are also counted as a
character. The return value of LENGTH function is integer.
X ← LENGTH("Computer Science")
16 will be assigned to X
X ← LENGTH(MyString)
Whatever the number of characters in MyString will be assigned to X

UCASE( ) – Takes STRING as a parameter and returns the STRING in uppercase. For example, the
string "Computer Science" would become "COMPUTER SCIENCE". The return value of UCASE
function is 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 MyString will be converted into capital letters and then assigned to X

LCASE( ) – Takes STRING as a parameter and returns the STRING in lowercase. For example, the
string "Computer Science" would become "computer science". The return value of LCASE function
is STRING.
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 MyString will be converted into small letters and then assigned to X

SUBSTRING( ) – It returns the extracted STRING from a given string. It takes 3 parameters to work
with. First parameter is a STRING, second and third parameters are integers. The second parameter value
indicates the position to extract from the string and the third parameter value is used to give the number
of characters to be extracted from the given position. For example, the substring "Science" could be
extracted from "Computer Science". The return value of SUBSTRING function is STRING.
X ← SUBSTRING("Computer Science", 10, 7)
Science will be assigned to X
X ← SUBSTRING(MyString, 5, 4)
Page 2 of 4
Whatever is stored in MyString the extraction will start from the 5th character and 4 characters from that
position will be assigned to the variable X

File handling
Purpose of storing data in a file:
Computer programs store data in a file that will be required again later. While data is stored in RAM will
be lost when the computer is switched off, when data is saved to a file it is stored permanently. Data
stored in a file can thus be accessed by the same program at a later date or accessed by another program.
Data stored in a file can also be sent to be used on other computer(s).

The file needs to be opened before use, stating the mode of operation, before reading from or writing to
it. The syntax is as follows:
OPENFILE <File identifier> FOR <File mode>

The file identifier will be the name of the file with data type string. This will always be text files with .txt
extension. The following file modes are used:
• READ: is used to read / retrieve data from an existing file
• WRITE: is used to write / copy data to a file. You must remember that WRITE mode will create a new
file and any existing data in the file will be overwritten.

N.B. This is to be remembered that reading data from a file is known as INPUT, whereas writing data
into a file is known as OUTPUT.
A file should be opened in only one mode at a time.

Data is read from the file (after the file has been opened in READ mode) using the READFILE command
as follows:
READFILE <File Identifier>, <Variable>
When the command is executed, the data item is read and assigned to the variable.
Data is written into the file after the file has been opened using the WRITEFILE command as follows:
WRITEFILE <File identifier>, <Variable>
When the command is executed, the data is written into the file. Files must be closed when they are no
longer needed using the CLOSEFILE command as follows:
CLOSEFILE <File identifier>

File handling operations:


Every file is identified by its filename. In this section, we are going to look at how to read and write a line
of text or a single item of data to a file. In your syllabus, only a line of text will be used to read from a file
or write into a file.

Here are examples of writing a line of text to a file and reading the line of text back from the file. The
pseudocode algorithm has comments to explain each stage of the task.

DECLARE TextLine : STRING


N.B. Any variable or constant declared to hold a file name must be of STRING
CONSTANT MyFile = "MyText.txt" //"MyText.txt" file has been assigned to MyFile constant
OPENFILE MyFile FOR WRITE // opens file in WRITE mode
The above line could have been written in the following way also by avoiding declaration of MyFile:

Page 3 of 4
OPENFILE "MyText.txt" 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 "MyText.txt" and the data will be stored there
CLOSEFILE MyFile // closes the file

// reading a line of text from a file


OPENFILE MyFile FOR READ // opens file in READ mode
READFILE MyFile, TextLine // reads a line of text from the file in TextLine
OUTPUT "The file contains this line of text:"
OUTPUT TextLine //Displays the line of text on the screen
CLOSEFILE MyFile // 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 FileA.txt to
FileB.txt
DECLARE LineOfText : STRING
OPENFILE “FileA.txt” FOR READ
OPENFILE “FileB.txt” FOR WRITE
READFILE “FileA.txt”, LineOfText
WRITEFILE “FileB.txt”, LineOfText
CLOSEFILE “FileA.txt”
CLOSEFILE “FileB.txt”

Maintainable program
Understand the need to create maintainable program:
Once a program is written, it may need to be maintained or updated by another programmer at a later
date. The programmer may have no documentation other than a copy of the source program. Even a
programmer looking at their own program several years later may have forgotten exactly how all the
tasks in it were completed!
To create a maintainable program it should:
 Always use meaningful identifier names for:
– variables
– constants
– arrays
– procedures
– functions
 Be divided into modules for each task using:
– procedures
– functions
 Be fully commented using programming language’s commenting feature

Page 4 of 4

You might also like