| «‹ July 2026 ›» | | S | M | T | W | T | F | S | | | | 1 | 2 | 3 | 4 | | 5 | 6 | 7 | 8 | 9 | 10 | 11 | | 12 | 13 | 14 | 15 | 16 | 17 | 18 | | 19 | 20 | 21 | 22 | 23 | 24 | 25 | | 26 | 27 | 28 | 29 | 30 | 31 | |
|
2009 年 9 月 11 日 星期五  |
| Arrays |
分類: Programming Language |
High-level Programming Language (Pascal)
Arrays
- structured data type (e.g. array and record)
- can store a collection of data items of the same type into a table-like structure but the variables of simple data type (e.g. integer, real, char , Boolean and string) can only hold a single value at a time
- each data item stored in an array is called array element
- each element can be accessed by the array name followed by a subscript (or an index) enclosed in square brackets
for example,
StudentName[3] which access the third element of the array StudentName
Declaration of a One Dimensional Array
var ArrayName : array [<Index>] of <Element_type>
- <Index>
- can be any ordinal types (char, integer, Boolean) or subrange type (2..10 or 'a' .. 'e')
- the range of <Index> indicates the size of one dimensional array , for example
var StudentName : array [LowerIndex .. UpperIndex] of string;
- the array StudentName contains (UpperIndex - LowerIndex + 1) elements
- <Element_type>
- represents the type of each element in an array
- for example,
- var StudentName : array [1..10] of string ; {string array}
- TestScore : array [1..ClassSize] of real ; {real number array}
Copying an Array
Initialization of Array
Input Data in an Array
Declaration of a Two Dimensional Array
var ArrayName : array [<Row_Index>, <Col_Index>] of <Element_type>
- <Row_Index> and <Col_Index>
- can be any ordinal types (char, integer, Boolean) or subrange type (2..10 or 'a' .. 'e')
- the product of the range of <Row_Index> and <Col_Index> indicates the size of two dimensional array , for example
var StudentActivity : array [1 .. 40, 1..3] of string;
the array StudentActivity contains (40 - 1 + 1) ’ (3 - 1 + 1) = 120 elements
- <Element_type>
- represents the type of each element in an array
- for example,
var StudentName : array ['A'..'E', 1..40] of string ; {string array}
TestScore : array ['A'..'E', 1..40] of real ; {real number array}
Using nested for loop to initialize, copy, input and output two dimensional array
|
|