Chapter8 - Chapter 8 ARRAYS So far in our programming, each...

Info iconThis preview shows pages 1–4. Sign up to view the full content.

View Full Document Right Arrow Icon
Chapter 8 ARRAYS So far in our programming, each memory location for data had its own special name; each variable had a unique identifier. In this chapter we will introduce the idea that groups of data will share a common name and be differentiated from each other by numbering each one uniquely. Suppose that we have a list of names of persons. We could give the list the name person and identify the first name as person(0) , the second as person(2) , and so on. We call the number that is enclosed in parentheses the index of the list or array . The reason why we should use this method of identifying variables is not clear. We will have to do an example so that you can see the power of the new method. As an example, suppose you wanted to read in a list of names of 50 persons and output them in reverse order, that is, last first. We would need to read in the entire list before we could begin the output. This means we must have a memory location for each name. We must be able to reserve this space by a declaration. " ' DECLARATION OF ARRAYS For the list of names of persons we would use a declaration character* 20 person(50) Each name is a character string variable whose length is 20. The fact that the variable identifier person is a list is shown by having something in parentheses after it. And what is shown is the fact that it is a list numbered, or indexed, from 1 to 50. What actually is shown in parentheses is the number of entries in the list. If we want the fifty entries to be indexed say from 51 to 100 we would write the declaration as character* 20 person(51:100) Most arrays have an index starting at 1.
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
2 | ARRAYS HANDLING LISTS We are now ready for the program that reverses the order of a list of names. Here the array index is an integer variable i . program redo ! Reverse the order of a list of names character* 20 person(50) integer i, j print* , "Enter 50 names in quotes, one to a line" ! Read list of names do j = 1, 50 read* , person(i) end do print* , "Here is reversed list" ! Output reversed list do i = 50, 1, -1 print* , person(j) end do stop end The data input is a list of 50 names, each in quotes, one to a line. Now, perhaps, you can see what a powerful programming tool the indexed variable can be. In the do loops the index i that is counting the loops can be used to refer to the different members of the list. In the first do loop the names are read; the first is stored in the variable person(1) , the second in person(2) , and so on. In contrast, we want the output loop to output person(50) , next person(49) , and so on. We use a negative step size. When an array such as person is being read into the computer we do not need to write out a do loop; we can use a shorter form in which a do loop is implied. Instead of writing do i = 1, 50 read*, person(i) end we can write the single statement read*, (person(i),i=1,50)
Background image of page 2
ARRAYS | 3 As it happens we can write a shorter form still when the complete array is being read in normal order. We could have
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 4
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 02/13/2012 for the course CSE 1540 taught by Professor Hofbauer during the Winter '12 term at York University.

Page1 / 18

Chapter8 - Chapter 8 ARRAYS So far in our programming, each...

This preview shows document pages 1 - 4. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online