Introduction to Arrays. Objectives Distinguish between a simple variable and a subscripted variable....

Preview:

DESCRIPTION

Objectives (Continued) Distinguish between row-major ordering and column-major ordering. Explain how the ordering of variables in a table affects the efficiency of processing.

Citation preview

Introduction to Arrays

Objectives

• Distinguish between a simple variable and a subscripted variable.

• Input, output, and manipulate values stored in a list, or one-dimensional array.

• Input, output, and manipulate variables in a table, or two-dimensional array.

Objectives (Continued)

• Distinguish between row-major ordering and column-major ordering.

• Explain how the ordering of variables in a table affects the efficiency of processing.

IntroductionUp to this point we have focused on reading, processing, and writing of single values. Each value we have used has been stored in a single, unique location and referred to as a single (or simple) variable. Each variable had its own name, such as COUNT, N, INPUT, ACCUM, etc. We need not always do things this way ...

What If ...

... We want to input a list of ten items to ten consecutive storage locations. We could name them ITEM1, ITEM2, and so on, and use these names throughout the program.

So Far, So Good ...

... But what if we want to use 100 values? 1000 values? We could use the same approach but we learned a long time ago that the resulting program would be extremely large and cumbersome!

List Structures

Suppose that instead of treating our ten input items as ten similar but separate data items, we treat them as a group of data items. We will set aside a storage area large enough to hold all ten values, and assign a name to the storage area.

Data items stored and defined in this way are called lists, vectors, or arrays - depending on the programming language you are using.

ArraysOnly the group of items stored in an array (vector, list) is given a name. An individual item in the group is referred to by its relative position in the group (going left to right). This position is identified by a subscript in parentheses following the group name.

Arrays (Continued)For example, assume we have reserved a storage area called INAREA for the ten numbers we described earlier. When we use the unsubscripted name INAREA we are referring to the entire group. The individual members of the group are referred to by the subscripted name - for example, the first member of the group is INAREA(1).

List Examples

INAR

EA(1)IN

AREA(2)

INAR

EA(3)

INAR

EA(5)

INAR

EA(4)

INAREAIN

AREA(10)

Initialize One-Dimensional ArrayStartSUB = 0DOUNTIL SUB = 10 SUB = SUB + 1 LIST(SUB) = 0ENDDOStop

Input One-Dimensional ArrayStartSUB = 0DOUNTIL SUB = 10 SUB = SUB + 1 READ LIST(SUB)ENDDOStop

Output One-Dimensional ArrayStartSUB = 0DOUNTIL SUB = 10 SUB = SUB + 1 WRITE LIST(SUB)ENDDOStop

Sample Problem 9.1

Problem:

Compute and output the smallest number in a ten-element array called LIST.

Sample Problem 9.1 (2)

Structure Chart looks something like this:OVERALLCONTROL

A000

COMPUTE ANDOUTPUT SMALL

B010

INPUTARRAYB000

Sample Problem 9.1 (3)

Overall Control module:A000StartInput Array (B000)Compute and Output Small (B010)Stop

Sample Problem 9.1 (4)Input Array (we’ve seen this already):

B000EnterSUB = 0DOUNTIL SUB = 10 SUB = SUB + 1 READ LIST(SUB)ENDDOReturn

Sample Problem 9.1 (5)Compute and Output SMALL:

B010EnterSMALL = LIST(1)SUB = 1DOUNTIL SUB = 10 SUB = SUB + 1 IF LIST(SUB) < SMALL THEN SMALL = LIST(SUB)

(ELSE) ENDIFENDDOWRITE SMALLReturn

Sample Problem 9.2

This time we are to compute and print the average of the ten values in LIST, and print the entries in LIST as well.

Sample Problem 9.2 (2)About the only thing different here is the processing of the members of the array. We added a new module called Output Array (which we have already seen). Compute and Output Average is the name of the module which computes the average of the array elements.

Sample Problem 9.2 (3)Pseudocode for Compute and Output Average:B020EnterACCUM = 0SUB = 0DOUNTIL SUB = 10 SUB = SUB + 1 ACCUM = ACCUM + LIST(SUB)ENDDOAVG = ACCUM / SUBWRITE ‘Average is’,AVGReturn

Sample Problems 9.3 - 9.5These examples are pretty straightforward - all we’re doing is other manipulations of the array data. In examples 9.4 and 9.5 we create new arrays to contain the output of manipulations on values in the original array.

Let’s move on to tables ...

Table StructuresIn lists we used only a single subscript to define the position of a value in the list. This single subscript is also sometimes called a DIMENSION. (Personal note: the first programming language I learned was FORTRAN - in FORTRAN, arrays are defined with the word DIMENSION.)

Table StructuresSometimes it is desirable to treat data as having more than one dimension: for example, the air fare between two cities depends on where your trip starts and ends - two things are needed to decide the value.

A Typical Table

1 4 5 7 2

3 8 8 9 0

4 7 4 8 1

9 9 8 8 2

Initialize Two-Dimensional Array

StartROW = 1DOUNTIL ROW > 4 COL = 1 DOUNTIL COL > 5 GRID(ROW,COL) = 0 COL = COL + 1 ENDDO ROW = ROW + 1ENDDOStop

Sample Problem 9.6 (Seating Chart Problem)

We use the familiar Input Array module to read in the names and fill the chart, then an Output Array module to print it. To find a student’s seat, the name is input and the array is searched for an entry with that name. (See page 209.)

Sample Problem 9.7This problem uses a two-dimensional array: each row contains a student name and the student’s average. The program is to find the highest average in the class. The array has 25 rows.

Sample Problem 9.7 (2)Solution begins on page 210 (structure chart). Flowchart and pseudocode for the “Compute and Output Highest Average” module are on page 213.

Sample Problem 9.8

Create a two-dimensional array called A. (Dimensions of A come from a header record - M rows of N columns each.) We will also create a one-dimensional array V containing N values.

Problem 9.6 (Continued)

Each value in each row of A is multiplied by the value in the corresponding position in V: for example, A(M,1) is multiplied by V(1); A(M,2) is multiplied by V(2), etc., up to A(M,N) and V(N). Output is a two-dimensional array called T.

Solution

Here is the overall control module - not too difficult:

A000StartInput two-dimensional array (B000)Input one-dimensional array (B010)Compute and output array (B020)Stop

Compute and Output ArrayB020EnterROW = 1DOUNTIL ROW > M COL = 1 DOUNTIL COL > N T(ROW,COL) = A(ROW,COL) * V(COL) WRITE T(ROW,COL) on current line COL = COL + 1 ENDDO

Compute and Output Array (Continued)

Skip to new line

ROW = ROW + 1

ENDDO

Return

Row-Major and Column-Major Order

• Different programming languages store data in different ways. Our algorithm needs to be written to allow efficient processing of the array.

• Row-major order: first subscript varies least rapidly, last one most rapidly) - i.e., data is stored by rows.

• Column-major order: first subscript varies most rapidly, last one least rapidly).

• Supermarket example.

Ordering of Array Data

• Pascal, C, Basic and COBOL store and process data in row-major order.

• (Some implementations of Basic may vary.)

• FORTRAN uses column-major order.

Enrichment

Basic and Visual Basic Examples

Assignment 4

Chapter 9, page 226, Exercise 11

Due Tuesday

Recommended