Chapter 8 Assignment Page

One-Dimensional Array

Each of the problems that have been solved in this course to date use a single variable name to store data. For example, to store a person’s name, you can create a variable called NAME. This will create a storage location in memory to store a person’s name. However, when another name is read, the computer will place the new name on top of the previous name and previous name is deleted from memory.

Suppose your want to store 20 names in memory and you want access to all of the names. You can create twenty variables, one called NAME1 and one called NAME2, and so on, to store the names in memory. Creating many variables in memory will work, however it will make your program long and confusing. An alternative is to create a data structure to store all of the variable names and access the data using one variable name.
 
 

Definition of an Array

An ARRAY is a data structure that is designed to hold large amounts of data in an organized manner. An array is a data structure with columns and rows. Arrays permit us to set aside a group of memory locations that we can manipulate as a single entity or access a single component. This means I can access all of the data in the array or a single data item in the array.
 
 

Uses of an Array

Once the data is stored in the array, you can access any of the data items at any time during processing. Arrays are commonly used to sort data and print the data in a particular order (ascending or descending). Arrays are also used to store data in a table. Tables are commonly used in applications like calculating taxes. Using these tables, you locate your income level and find the tax amount in the table. Locating data that is stored in an array is called a search.
 
 

Array Dimensions

The arrays you will be working with are one-dimensional. A one-dimensional array is an array with one column and many rows. Each location in the array can store a single value. A one-dimensional array is also called a vector. A two dimensional array is an array with many columns and rows. A two-dimensional array is called a table. If you have worked with spreadsheets before, it is simply a use of a two-dimensional array. Some spreadsheet programs like MS Excel are three-dimensional arrays. They have rows and columns, like a two-dimensional array, but there is an array stored behind each array creating three dimensions. The first value read and stored in an array is the first array element. Subsequent values are placed in the array in the order in which they are read. The entire array values are accessed through a single array name.
 
 

(This is a one-dimensional array called Name. There is one column and Name 5 rows)

Name
 
1. Butler
2. Davis
3. Anderson
4. Smith
5. Boulton
(This is a two-dimensional array called Numbers. There are 4 columns and 6 rows.)
Numbers
 
1

              90

2

67

3

56

4

34

2

              89

78 678 654
3

             765

56 342 433
4

              567

58 124 98
5

              123

09 798 23
6

              543

43 432 75

 

Accessing data from an array

Before you can access data from an array, you need to tell the computer system which column and which row you want to access the data from. An index or subscript is used to indicate the column number and row number. A subscript is a number that points to an array location. The number of dimensions will indicate the number of subscripts that are needed to access an array location. If you are dealing with a one-dimensional array, then one subscript is needed; to specify the row. If you are dealing with a two-dimensional array, then two subscripts are needed; one for the row and one for the column. For example: to print data from the third location of the array Name, you need to use the statement WRITE NAME(3). This is pronounced Name sub 3. This will output the name Anderson. Since there is only one column, you only need to specify the row number. Some programming languages begin numbering the rows at 0 and some languages begin numbering the rows at 1, but the concepts are the same. If the first row is numbered 0, then to print the data from the third location, you need to use the statement WRITE Name(2), since you begin counting at 0, the third location is actually location 2. This will output the name Anderson.

To output data from the array Number, since there is more than one column and more than one row you need to specify the column and row number. To output the data in column 2, row 5, you need to use the statement WRITE Number(2,5). This would output the number 09.

Input data into the array

Entering data into an array is called loading an array. Data is input into an array using different methods depending upon whether the data is stored in an input file or not. Data that is stored in an input file is typed into the computer and stored on a secondary storage device. If you are creating a small array or if the data stored in each element of the array is the same or has a pattern, the data is typically not stored in a file.

Store Data in an Array the data is not stored in a file:

These are the steps you need to follow if you want to input data in an array and the data you want to place in each location is the same. For example, you want to create an array of accumulators or counters. You first need to set each location in the array to zero. In this example, you will create an array called COUNT and set each location to zero.

Count
 
1.

                0

2. 

                0

3. 

                0

4. 

                0

5. 

                0

In pseudocode: This sets a variable called POS to 1 to point to the first location in the array. As long as POS is less than or equal to 5, the loop is performed. A zero is placed in the array location. The POS value is incremented by 1 to point to the next location in the array.

(With a DOWHILE loop)

wpe2E.gif (1367 bytes)
 
 

Store Data in an Array (the data is not stored in a file):

These are the steps you need to follow if you want to input data in an array and the data you want to input is not the same. For example, you want to create an array to store a few company names. In this example, you will create an array called COMPANY and set each location to a company name.

COMPANY
 
1.

             IBM

2. 

             Microsoft

3. 

             Oracle

4. 

              Lotus

In pseudocode:

wpe2F.gif (1367 bytes)

Store Data in an Array the data is stored in a file:

If the data is stored in a file, you need to determine which method to use to determine how end of file is reached. Remember, there are four methods available (Counter Controlled, Header Record Logic, Trailer Record Logic and Automatic End of File). We will cover one example using each type of loop.
 
 

Counter Controlled loop – This example will input 4 names into an array called Name.

Input File

wpe30.gif (1367 bytes)
 
 

Result:

Name
 
1.

          Baker

2. 

          Butler

3. 

          Davis

4. 

         Harris

Header Record loop – This example will input 4 names into an array called Name.

Input File

wpe31.gif (1367 bytes)

Result:

Name
 
1.

          Baker

2. 

          Butler

3. 

          Davis

4. 

         Harris

Trailer RecordThis example will input 4 names into an array called Name. The variable Arraysize will contain the total number of data values stored in the array. The variable Arraysize will be used to process and output data from the array.

Input File
wpe32.gif (1367 bytes)

Result:

Name
 
1.

          Baker

2. 

          Butler

3. 

          Davis

4. 

         Harris

5.

         Dummy

Automatic End of FileThis example will input 4 names into an array called Name. The variable Arraysize will contain the total number of data values stored in the array. The variable Arraysize will be used to process and output data from the array.

Input File

Name
 
1.

          Baker

2. 

          Butler

3. 

          Davis

4. 

         Harris

5.

        End of File Marker

Output data from an array – a counter controlled loop is used if the data input is not in an input file or a counter controlled loop or header record loop is used to input the data into the array.
 
1.

          Baker

2. 

          Butler

3. 

          Davis

4. 

         Harris

DOWHILE loop

wpe36.gif (1367 bytes)

DOUNTIL loop

wpe38.gif (1367 bytes)

If you use a header record loop on input, substitute 4 with Count, which stores the total number of input records input.

DOWHILE loop

wpe39.gif (1367 bytes)

DOUNTIL loop

wpe3A.gif (1367 bytes)

Output data from an array this type of loop is used when a trailer record or automatic end of file loop is used on input. Since these loops input an undetermined number of input records, the variable Arraysize is created after the input loop to store the total number of records input.

Name
 
1.

          Baker

2. 

          Butler

3. 

          Davis

4. 

         Harris

5.  End of File Marker or trailer record
DOWHILE loop

wpe3C.gif (1367 bytes)
 
 

DOUNTIL loop

wpe3D.gif (1367 bytes)