Home Random Page


CATEGORIES:

BiologyChemistryConstructionCultureEcologyEconomyElectronicsFinanceGeographyHistoryInformaticsLawMathematicsMechanicsMedicineOtherPedagogyPhilosophyPhysicsPolicyPsychologySociologySportTourism






Single Dimension Arrays


In this chapter we will move on to the data structure called an array. The elements in an array must have identical types, though these types can be any of the types available in Pascal, even other arrays. When an array has an array for its element type it is called a multi-dimensional array. We will look at these later, but first, a look at single-dimensional arrays and a closer look at the type RECORD. If you think back to the description of the type STRING, you might remember that we used the subscript technique to inspect each compartment in the container individually. It might surprise you you learn that the type STRING is internally defined as an array, with the type of each compartment declared to be a character. Meaning; that a string is an array of type CHAR. Since this is the case, you have already been using single-dimension arrays. Unlike a string, though an array can have its declared type consisting of any of the the in-built types as well as a user defined type, such as RECORD or an enumerated type.
To make use of an ARRAY there are two steps:

  • It should be declared by an identifier in the TYPE section of the program. This makes it a type.
  • Then a request to make one of these new types available to the program must go into the variable list.

Single dimension arrays, an analogy


The simplest analogy that I have come up with for a single dimension array is that of a row of books on a shelf. In this analogy, the books are numbered from one to fifteen, using subscripts. The spine of a book carries two pieces of information the title, and the name of the author. So you could ask the question of the program, `What is the ninth book on the shelf'.

Figure 1 - An array of books

The storage device has been identified as shelf, the subscript is [9], and the information required is title. Since the title is one of two pieces of information carried on the book's spine, it must be a field in a RECORD, and we already know about accessing fields in records.

Declaring an array


So, how do we turn this analogy into a data-structure? In the TYPE section of the program:

  • Define a CONST for the size of the array.
  • Define types for fields in the RECORD for the author's name and the book's title.
  • Then define a RECORD containing those types called BOOK.
CONST MAX = 15; TYPE AUTHOR_NAME = STRING[20]; BOOK_TITLE = STRING[50]; BOOK = RECORD author : AUTHOR_NAME; title : BOOK_TITLE; END;


So now we have this new RECORD type called BOOK, which has two fields, one called author, and the other title. So now we just need to invent a storage device called a book shelf, to keep our books on. This is added to the TYPE section of the program, which now looks like:

TYPE AUTHOR_NAME = STRING[20]; BOOK_TITLE = STRING[50]; BOOK = RECORD author : AUTHOR_NAME; title : BOOK_TITLE; END; BOOK_SHELF = ARRAY[1..MAX] OF BOOK;


This new line reads as: `book_shelf is an ARRAY, from one to fifteen elements, of the type BOOK, that's fairly straightforward. Now you need to make a variable of this new TYPE BOOK_SHELF, available to your program, by declaring it in the variable list, so now you have somewhere to store your books, when the data is read into the program.



VAR my_book_shelf : BOOK_SHELF;


Could that be any simpler? If you remember back to the string example, you will be able to figure out how to access an array.

  • Name the container by its declared name in the variable list.
  • Use the subscript technique to identify the element required.
  • If the elements have the type RECORD, use the fieldname to access the required component.


Ok, let's get on to this next program using this book shelf analogy. One important point first, is that when reading files into an array using a WHILE loop the loop condition must contain two parts. One must refer to the end of file (EOF) and the other to the maximum size of the array. This second reference is needed in case the file is larger than the number of elements in the array and prevents the program crashing, which it would do if the program tried to put a value into an array past its last element.

Program 1

PROGRAM book_shelf(INPUT,OUTPUT,booklist); { program reads in data from a file and assigns it to an element in an array, then the user is prompted for a number, and the program writes out that element } CONST MAX = 15; TYPE AUTHOR_NAME = STRING[20]; BOOK_TITLE = STRING[50]; BOOK = RECORD AUTHOR : AUTHOR_NAME; TITLE : BOOK_TITLE; END; BOOK_SHELF = ARRAY[1..MAX] OF BOOK; VAR number_of_books, { contains the number of books loaded into the array } number : INTEGER; author_in : AUTHOR_NAME; title_in : BOOK_TITLE; booklist : TEXT; { data file name } my_book_shelf : BOOK_SHELF; BEGIN number_of_books := 0; WRITELN; RESET(booklist); { reset file for reading } WHILE (number_of_books <= MAX) AND (NOT EOF(booklist)) DO BEGIN number_of_books := number_of_books + 1; READLN(booklist,title_in); READLN(booklist,author_in); my_book_shelf[number_of_books].title := title_in; my_book_shelf[number_of_books].author := author_in; END; CLOSE(booklist); { all file data in array } WRITE('Please enter number of book : '); READLN(number); WRITELN; IF (number <= number_of_books) AND (number >= 1) THEN WITH my_book_shelf[number] DO BEGIN WRITE('Book number ',number:0,' is ',title); WRITELN(' by ',author); END; END. { program book_shelf }


Note 1: The WITH can be used on an array because arrayname[index] is in reality the name of a record which is an element of an array. Also notice the use of an IF statement to check that the range of the identifier number is between one and number_of_books inclusively.
Note 2: If you have found this program fairly easy to understand, this is partly due to the fact that I have chosen the variable names well. Let that be a lesson to you. People are lazy, and don't want to take the time to write out these fairly long identifiers, but you should be able to see, just how important well chosen identifiers are when it comes to figuring out how a program works. Remember to keep your indentation consistent. We can write out all the values in the array using a simple FOR loop in the program. The variable number_of_books contains the number of data items in the array so we can use that as the termination-value of the FOR loop.

FOR I := 1 TO number_of_books DO WITH my_book_shelf[number] DO BEGIN WRITE('Book number ',number:0,' is ',title); WRITELN(' by ',author); END;


This FOR loop can be put in the program anywhere once the initial reading in from the file is done and the number of occupied elements is assigned to number_of_books.
Now we have constructed the data-structure, which is what our BOOK_SHELF is, we can now go on to the next stage, getting the power out of an array by searching for a value.


Date: 2016-03-03; view: 599


<== previous page | next page ==>
Subranges of enumerated types | Searching for a value
doclecture.net - lectures - 2014-2024 year. Copyright infringement or personal data (0.008 sec.)