0478 Computer Science
Chpater 1 Number System
1.1 Number systems
1.2 Text, sound and images
1.3 Data storage and compression
Chapter 2 Data transmission
2.1 Types and methods of data transmission
2.2 Methods of error detection
2.3 Encryption
Chapter 3 Hardware
3.1 Computer architecture
3.2 Input and output devices
3.3 Data storage
3.4 Network hardware
Chapter 4 Software
4.1 Types of software and interrupts
4.2 Types of programming language, translators and integrated development environments (IDEs)
Chapter 5 The internet and its uses
5.1 The internet and the World Wide Web (WWW)
5.2 Digital currency
5.3 Cyber security
Chapter 6 Automated and emerging technologies
6.1 Automated systems
6.2 Robotics
6.3 Artificial intelligence (AI)
Chapter 7 Algorithm design and problem solving
7.1 The program development life cycle
7.2 Computer systems, sub-systems anddecomposition
未命名
未命名
Chapter 8 Programming
8.1 Programming concepts
8.2 Arrays
Mr. Theo
-
+
首页
8.2 Arrays
An array is a data structure containing several elements of the same data type; these elements can be accessed using the same identifier name. The position of each element in an array is identified using the array’s index. ## One-dimensional arrays Arrays are considered to be fixed-length structures of elements of identical data type, accessible by consecutive index (subscript) numbers. It is good practice to explicitly state what the lower bound of the array (i.e. the index of the first element) is because this defaults to either 0 or 1 in different systems. Generally, a lower bound of 1 will be used. Square brackets are used to indicate the array indices. One-dimensional arrays are declared as follows (where l is the lower bound and n is the upper bound): ```pseudocode DECLARE <identifier> : ARRAY[<l>:<n>] OF <data type> ``` identifier: the name of the array l: the first index value n: the last index value **Example – array declaration** ```pseudocode DECLARE StudentNames : ARRAY[1:30] OF STRING DECLARE NoughtsAndCrosses : ARRAY[1:3] OF CHAR ``` In the main pseudocode statements, only one index value is used for each dimension in the square brackets. A statement should **not**, however, refer to a group of array elements individually. For example, the following construction should not be used. ~~StudentNames [1 TO 30] ← ""~~ **Example – assigning a group of array elements** ```pseudocode FOR Index = 1 TO 30 StudentNames[Index] ← "" NEXT Index ``` ### Example: To declare a new array called MyList: ```pseudocode DECLARE MyList : ARRAY[0:9] OF INTEGER ``` Each position in the array can be populated in an array by defining the value at each index position. For instance, we can add the number 27 to the fourth position in the array ***MyList*** as follows: ```pseudocode MyList[4] ← 27 ``` To populate the entire array instead we can use a loop: ```pseudocode OUTPUT "Enter these 10 values in order 27, 19, 36, 42, 16, 89, 21, 16, 55, 72" FOR Counter ← 0 TO 9 OUTPUT "Enter next value " INPUT MyList[Counter] NEXT Counter ``` Notice that in this code we have used the variable ***Counter*** as the array index. We can display the data that lies in a particular location in an array as follows: ```pseudocode OUTPUT MyList[1] ``` This would display the value 19. | index | MyList | | | ----- | ------ | ----------------- | | 0 | 27 | <- First element | | 1 | 19 | | | 2 | 36 | | | 3 | 42 | | | 4 | 16 | | | 5 | 89 | | | 6 | 21 | | | 7 | 16 | | | 8 | 55 | | | 9 | 72 | <- Last element | ## Two-dimensional arrays A two-dimensional array can be referred to as a table, with rows and columns. Here is an example of a table with 10 rows and 3 columns, which contains 30 elements. The first element is located at position 0,0. ```pseudocode DECLARE <identifier> : ARRAY[row_l:row_n,column_l:column_n] OF <data type> ``` row_l: the first index value for rows row_n: the last index value for rows column_l: the first index value for columns column_n: the last index value for columns ```pseudocode DECLARE MyTable : ARRAY[0:9,0:2] OF INTEGER ``` Example: Two-dimensional array declaration ```pseudocode DECLARE MyTable : ARRAY[0:9,0:2] OF INTEGER ``` The declared array can then be populated using a loop, just like for one dimensional arrays – however this time there need to be two nested loops, one for each index: ```pseudocode OUTPUT "Enter these values in order 27, 19, 36, 42, 16, 89, 21, 16, 55, 34" OUTPUT "Enter these values in order 31, 67, 98, 22, 35, 46, 71, 23, 11, 76" OUTPUT "Enter these values in order 17, 48, 29, 95, 61, 47, 28, 13, 77, 21" FOR ColumnCounter ← 0 TO 2 FOR RowCounter ← 0 TO 9 OUTPUT "Enter next value " INPUT MyTable[RowCounter, ColumnCounter] NEXT RowCounter NEXT ColumnCounter ```  We can display the data that lies in a particular location in a two-dimensional array as follows: ```pseudocode OUTPUT MyList[2,1] ``` This would display the value 98.
Theo
2025年5月30日 12:14
转发文档
收藏文档
上一篇
下一篇
手机扫码
复制链接
手机扫一扫转发分享
复制链接
Markdown文件
Word文件
PDF文档
PDF文档(打印)
分享
链接
类型
密码
更新密码
有效期