|
To access the contents, click the chapter and section titles.
Learn Pascal in a Three Days (2nd Ed.)
5-3 Arrays As Data StructuresIf you would like to represent the names of the players on a football team using only simple data types, you would need to use one variable for each players name. In such a case, you would need too many variables, such as: FirstPlayer SecondPlayer ThirdPlayer ... This is not a good idea. Now imagine the case if you were dealing with a class of one hundred students. It would be almost impossible to use one hundred variables to store names. The practical way to store this kind of data is to use the array data structure, which is useful for storing a collection of related data items. In the case of the football team you would need to declare only one subscripted variable, and you would represent your data like this: Player[1] Player[2] Player[3] ... The name of the variable is Player, and the number between the brackets is called the subscript or index. Changing the index gives you a new memory location in which to store a new name. This type of data structure is called one-dimensional array. It is useful to represent data such as names of a group of people, scores of one student in several classes, or any similar set of related items (see Table 5-1).
In Chapter 2 you met a special type of one-dimensional array (the PACKED ARRAY OF CHAR), which is used to store a string of text in standard Pascal, and you already know that each element (character) in this array is referred to by a number (index). In other applications you may need a two-dimensional array, which is capable of handling more complicated structures. For example, suppose that you want to store the scores of a group of students in different classes, as represented in Table 5-2.
Each element in this table is related to a row (the student number) and a column (the class number); these are the two dimensions of the array. The data item itself is a real number. To represent the data in this table your variables will look something like this: StudentScore[3][4] This variable represents the score of student #3 in class #4; in other words, the number at the intersection of row #3 and column #4. You may assign a numeric value which represents a score to this variable, thus: StudentScore[3][4]:= 45.1; Compare now the following assignment statements to the values in the table: StudentScore[1][1]:= 55.5; { the score of student #1 in class #1 } StudentScore[1][2]:= 60.9; { the score of student #1 in class #2 } StudentScore[3][5]:= 66.9; { the score of student #3 in class #5 } StudentScore[100][2]:= 87.2; { the score of student #100 in class #2 } Arrays are classified as structured data types (as opposed to the simple [or unstructured] types you have used thus far). There are many other structured data types in Pascal which are useful for different applications. As a matter of fact, the quality of a program is mainly measured by two criteria:
5-4 One-Dimensional ArraysA one-dimensional array is declared using the following form: VAR array-name: ARRAY[index-range] OF element-type; If you want, for example, to declare an array to store test scores of ten students as real numbers, you can declare your array like this: VAR Score : ARRAY[1..10] OF REAL; This array (named Score) can hold up to ten real numbers. The index range [1..10] indicates that the indexes of the array elements start from 1 and end at 10. The index range, which is a subrange (of integers in this example), can be of any ordinal type, but the array elements can be of any data type. The above declaration, then, reserves a sequence of ten memory locations in which to store ten REAL values of ten array elements.
|
Products | Contact Us | About Us | Privacy | Ad Info | Home
Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc. All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement. |