Vous êtes sur la page 1sur 28

Computer and Information Technology for (HKCEE) Module A2

6.1Arrays and Strings


6.2 Text files
6.3 Procedures
6.4 Functions
Computer and Information
Technology for (HKCEE)
Module A2: Part B

6.1 Array and Strings


Array
a collection of values of same type
each element in the array is accessed by an
index
array name index

number [1] number [2] number [3] number [n]

1st element 2nd element 3rd element nth element

© Longman Hong Kong Education Page2


Computer and Information
Technology for (HKCEE)
Module A2: Part B

6.1 Array and Strings


One dimensional array
e.g. an integer array of 3 integers:
two methods of declaration:
Method 1
VAR
number : ARRAY[1..3] OF integer;
Method 2
TYPE
arr3 = ARRAY[1..3] OF integer;
VAR
mark : arr3;

© Longman Hong Kong Education Page3


Computer and Information
Technology for (HKCEE)
Module A2: Part B

6.1 Array and Strings


Two dimensional array
e.g. two-dimensional array with 2 rows and 3
columns
three methods of declaration:
Method 1
TYPE
rowtype = ARRAY[1..2] OF integer;
matrix = ARRAY[1..3] OF rowtype;
VAR
image : matrix;

© Longman Hong Kong Education Page4


Computer and Information
Technology for (HKCEE)
Module A2: Part B

6.1 Array and Strings


Two dimensional array
e.g. two-dimensional array with 2 rows and 3
columns
three methods of declaration:
Method 2
TYPE
matrix = ARRAY[1..2, 1..3] OF integer;
VAR
image : matrix;

© Longman Hong Kong Education Page5


Computer and Information
Technology for (HKCEE)
Module A2: Part B

6.1 Array and Strings


Two dimensional array
e.g. two-dimensional array with 2 rows and 3
columns
three methods of declaration:
Method 3
TYPE
image : ARRAY[1..2, 1..3] OF integer;

© Longman Hong Kong Education Page6


Computer and Information
Technology for (HKCEE)
Module A2: Part B

6.1 Array and Strings


String
an array of characters
e.g. declare and assign the string “hello”
TYPE string5 = ARRAY[1..5] OF char;
VAR a : string5
the string “hello” consists of 5 characters:
‘h’,‘e’,‘l’,‘l’and‘o’

h e l l 0
a[1] a[2] a[3] a[4] a[5]

© Longman Hong Kong Education Page7


Computer and Information
Technology for (HKCEE)
Module A2: Part B

6.1 Array and Strings


String
two methods to initialise the string:
a := ‘hello’;
a[1] := ‘h’; a[2] := ‘e’; a[3] := ‘l’
a[4] := ‘l’; a[5] := ‘o’;
Turbo Pascal string type
declare a variable MyName of string type with
maximum length 25
VAR
MyName : string[25];
© Longman Hong Kong Education Page8
Computer and Information
Technology for (HKCEE)
Module A2: Part B

6.1 Array and Strings


String handling functions
length
count the number of characters of a string
i := length(x);
str
convert an integer or a long integer into a string
str(old,new);
pos
display the position of a substring in its string in number
leftmost position number = 1
i := pos(substring, string);
© Longman Hong Kong Education Page9
Computer and Information
Technology for (HKCEE)
Module A2: Part B

6.1 Array and Strings


String handling functions
concat
join strings to form a new string
new_str := concat(a,b,c);
copy
copy a particular portion in a string to make a new string
new_str := copy(old_str, position, length);

© Longman Hong Kong Education Page10


Computer and Information
Technology for (HKCEE)
Module A2: Part B

6.1 Array and Strings


String handling functions
val
convert a numeric string into an integer
val(old_str, value, code);
– value = 0 when fail to convert a non-numeric input
– value = m when convert numeric input into an integer
number m
– code = 0 when all characters are numeric
– code = n when nth character is NOT numeric

© Longman Hong Kong Education Page11


Computer and Information
Technology for (HKCEE)
Module A2: Part B

6.2 Text files


Text file (ASCII file)
a file that holds text
stored in secondary storage device
Reasons of using text file
Values stored in variables will be lost after exiting
the program.
Applications need more data than the main
memory can fit .

© Longman Hong Kong Education Page12


Computer and Information
Technology for (HKCEE)
Module A2: Part B

6.2 Text files


Text file handling functions
assign
establish relationship
reset
open file for reading
rewrite
open file for writing
append
add data into file

© Longman Hong Kong Education Page13


Computer and Information
Technology for (HKCEE)
Module A2: Part B

6.2 Text files


Preparing a text file for input or output
Heading
PROGRAM name(input, output, file_variable);
Declaration
VAR
file_variable : text;

© Longman Hong Kong Education Page14


Computer and Information
Technology for (HKCEE)
Module A2: Part B

6.2 Text files


Steps of reading from a text file
1. Assign
establish relationship between file variable and the data
stored in text file
assign(file_variable, ‘text_file.txt’);
2. Reset
open file for reading
reset(file_variable);

© Longman Hong Kong Education Page15


Computer and Information
Technology for (HKCEE)
Module A2: Part B

6.2 Text files


Steps of reading from a text file
3. Read from text file
read(file_variable, input_list);
readln(file_variable, input_list);

4. Close the text file


close(file_variable);

© Longman Hong Kong Education Page16


Computer and Information
Technology for (HKCEE)
Module A2: Part B

6.2 Text files


Steps of writing to a text file
1. Assign
assign(file_variable, ‘text_file.txt’);
2a. Rewrite
open file for writing
rewrite(file_variable);
2b. Append
add data into an existing file
append(file_variable);

© Longman Hong Kong Education Page17


Computer and Information
Technology for (HKCEE)
Module A2: Part B

6.2 Text files


Steps of writing to a text file
3. Write to text file
write(file_variable, list_of_values);
writeln(file_variable, list_of_values);

4. Close the text file


close(file_variable);

© Longman Hong Kong Education Page18


Computer and Information
Technology for (HKCEE)
Module A2: Part B

6.3 Procedures
Procedures
extension of the declaration section of the program
placed after the variable declaration subsection
Advantages of using procedures
enhance top-down design
avoid repeating
increase readability

© Longman Hong Kong Education Page19


Computer and Information
Technology for (HKCEE)
Module A2: Part B

6.3 Procedures
PROGRAM <program name>;
CONST
VAR
PROCEDURE <procedure name>
CONST
VAR procedure
BEGIN
...
END;
BEGIN
... main program
END.

© Longman Hong Kong Education Page20


Computer and Information
Technology for (HKCEE)
Module A2: Part B

6.3 Procedures
Two forms of procedures
without parameters
PROCEDURE <name>;
BEGIN
...
END;
with parameters
PROCEDURE <name>(<parameter list>);
BEGIN
...
END;
© Longman Hong Kong Education Page21
Computer and Information
Technology for (HKCEE)
Module A2: Part B

6.3 Procedures
Value parameters
allow the value to be passed from the main program
to the procedure only
Variable parameters
any changes of values in the procedure produces a
corresponding change in the main program
variables are passed by reference

© Longman Hong Kong Education Page22


Computer and Information
Technology for (HKCEE)
Module A2: Part B

6.3 Procedures
Value parameters and variable parameters

PROCEDURE DemoVar( VAR x : real; y : real );


variable value
parameters parameters

© Longman Hong Kong Education Page23


Computer and Information
Technology for (HKCEE)
Module A2: Part B

6.4 Functions
Function
return a single value to the calling program
function name

y := sqrt(x);
obtain return argument
value of the
sqrt function
© Longman Hong Kong Education Page24
Computer and Information
Technology for (HKCEE)
Module A2: Part B

6.4 Functions
Standard functions
e.g. sqr, sqrt, round, trunc
argument is required
can be used in expressions
x := sqrt(y) + sqrt(z);
can be used in output statements
writeln(sqr(3) : 8);

© Longman Hong Kong Education Page25


Computer and Information
Technology for (HKCEE)
Module A2: Part B

6.4 Functions
User-defined functions
e.g. finding a volume of a cube

FUNCTION cube(a : real): real;


BEGIN
cube := a * a * a
END;

© Longman Hong Kong Education Page26


Computer and Information
Technology for (HKCEE)
Module A2: Part B

6.4 Functions
Recursive functions
a process of a subprogram calling itself
e.g. finding factorial of non-negative integer
FUNCTION factorial(n:integer):integer;
BEGIN
IF n=0 THEN
factorial := 1
ELSE
factorial := n * factorial(n-1)
END;

© Longman Hong Kong Education Page27


Computer and Information Technology for (HKCEE) Module A2

END

Vous aimerez peut-être aussi