home account info subscribe login search My ITKnowledge FAQ/help site map contact us


 
Brief Full
 Advanced
      Search
 Search Tips
To access the contents, click the chapter and section titles.

Learn Pascal in a Three Days (2nd Ed.)
(Publisher: Wordware Publishing, Inc.)
Author(s):
ISBN: 1556225679
Publication Date: 07/01/97

Bookmark It

Search this book:
 
Previous Table of Contents Next


Drill 5-2

Which of the following declarations are valid if they all come in one program?

TYPE
{1}  Football  = (Saints, Cowboys);
{2}  Games      = (Football, Baseball, Basketball)
{3}  Week      = (Mon, Tue, Wed, Thu, Fri, Sat, Sun);
{4}  Weekend    = Sat..Sun;
{5}  Compiler  = (C, Pascal, Fortran, Ada, Basic);
VAR
{6}  WholeWeek  :Week;
{7}  WorkingDay :(Mon, Tue, Wed, Fri);
{8}  Weekday   :Mon..Fri;
{9}  SW     :(Compiler, OperatingSystem, ApplicationProgram);
{10} DpTools  :(Hardware, Software, PeopleWare);
{11} DpTool   :(HW, SW, PW);
{12} C      :(TurboC, QuickC);
{13} Margin    : -10..+10;

5-3 Arrays As Data Structures

If 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 player’s 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).

Table 5-1 Example of a one-dimensional array
Player[1] Player[2] Player[3] Player[4] Player[5]
Able Baker Charlie John Sam

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.

Table 5-2 Example of a two-dimensional array
  Class # (second index)
1 2 3 4 5
Student # (first index)          
1 55.5 60.9 66.5 80.3 70.5
2 89.1 77.6 99.9 88.7 50.3
3 40.5 67.4 90.5 45.1 66.9
100 68.8 87.2 90.4 60.1 60.4

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:

1.  The structural efficiency of the program; that is, how readable, easy to debug, and prone to errors it is
2.  The use of the most efficient data structures applicable, to save time and enable the program to manipulate data in the most efficient way


NOTE ON TERMINOLOGY:  An array variable may be called either a subscripted variable or an indexed variable. The array elements referred to by the array variables are also called array components. In mathematics, a one-dimensional array is called a vector, while a two-dimensional array is called a matrix. You may come across these names in mathematical applications.

5-4 One-Dimensional Arrays

A 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.


Previous Table of Contents Next


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.