HCI 7. Array, List and Dictionary (2022) revised
Uploaded by adrianwang2003 · 28 May 2024
Preview
Hwa Chong Institution H2 Computing 1 7 Array, List and Dictionary Learning Outcome 7.1 Array An array provides a convenient structure for storing data items of the same data type. As you will see, arrays allow for both easy reading of the data into memory and efficient accessing of the data for processing. We will consider two standard types of situations in which arrays are useful: (1) when a data list must be processed more than once and (1) when a large number of related summing or counting variables are needed. 7.1.1 One-Dimensional Array So far, all the variables that we have considered have been a simple data type. A variable of simple data type consists of a single memory cell that can hold only one value at a time. By contrast, a variable of structured data type consists of a collection of memory cells. An array consists of a collection of memory cells for storing a list of values that are all of the same data type -- for example, a list of integers, a list of real numbers, a list of characters, or a list of boolean values. The entire list is given a name. An array can be compared to an apartment building. The name of the array represents the name of the building. The elements of the array represent the building's apartments, each of which has a subscript or index, which corresponds to an apartment number. Assuming that the index starts with 1, Riser[1] is used to specify the first apartment in the building called Riser, while Riser[4] refers to apartment 4 in the building called Riser Riser[1] Riser[2] Riser[3] Riser[4] In order to specify an individual element of an array, you must give both the name of the array and the subscript. For example, for an array called scores, the statement scores[4] ← 86 assigns the value 86 to the memory cell scores[4]. Similarly, OUTPUT scores[6] outputs the contents of the memory cell scores[6]. The following pseudocode assigns values to be stored in an array's elements. Programming Elements and Constructs Understand the different data structures: array, list and dictionary; initialize arrays (1 - dimensional and 2-dimensional)
Hwa Chong Institution H2 Computing 2 // program drill BEGIN DECLARE X: ARRAY[1:4] of INTEGER x[1]←83 x[2]←59 x[3]←88 x[4]←72 OUTPUT x[4] OUTPUT x[2] OUTPUT x[2+1] OUTPUT x[2]+1 END In programming languages where static arrays are used, a declaration of the array would require the data -type, and the number of elements you want in the array. By specifying these information, memory cells memory cells are set aside for the array declared. In the above example, 4 memory cells, x[1], x[2], and x[3], x[4] are set aside, each enough storage for an integer value. By the time the four assignment statements have been executed, four numbers will be stored in array x as follows: x[1] x[2] x[3] x[4] 83 59 88 72
Content continues in the PDF.
Related notes
- VJC Chapter 21 SQLite with PythonNotes/Practices · 2025
- VJC Chapter 23 Web Applications PrinciplesNotes/Practices · 2025
- VJC Chapter 10 RecursionNotes/Practices · 2025
- VJC Chapter 20 SQLNotes/Practices · 2025
- VJC Chapter 16 Hash TableNotes/Practices · 2025
- VJC Chapter 22 NoSQLNotes/Practices · 2025

