38
Introduction to Arrays

Introduction to Arrays. Objectives Distinguish between a simple variable and a subscripted variable. Input, output, and manipulate values stored in a

Embed Size (px)

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

Page 1: Introduction to Arrays. Objectives Distinguish between a simple variable and a subscripted variable. Input, output, and manipulate values stored in a

Introduction to Arrays

Page 2: Introduction to Arrays. Objectives Distinguish between a simple variable and a subscripted variable. Input, output, and manipulate values stored in a

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.

Page 3: Introduction to Arrays. Objectives Distinguish between a simple variable and a subscripted variable. Input, output, and manipulate values stored in a

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.

Page 4: Introduction to Arrays. Objectives Distinguish between a simple variable and a subscripted variable. Input, output, and manipulate values stored in a

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

Page 5: Introduction to Arrays. Objectives Distinguish between a simple variable and a subscripted variable. Input, output, and manipulate values stored in a

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.

Page 6: Introduction to Arrays. Objectives Distinguish between a simple variable and a subscripted variable. Input, output, and manipulate values stored in a

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!

Page 7: Introduction to Arrays. Objectives Distinguish between a simple variable and a subscripted variable. Input, output, and manipulate values stored in a

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.

Page 8: Introduction to Arrays. Objectives Distinguish between a simple variable and a subscripted variable. Input, output, and manipulate values stored in a

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.

Page 9: Introduction to Arrays. Objectives Distinguish between a simple variable and a subscripted variable. Input, output, and manipulate values stored in a

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

Page 10: Introduction to Arrays. Objectives Distinguish between a simple variable and a subscripted variable. Input, output, and manipulate values stored in a

List Examples

INAR

EA(1)IN

AREA(2)

INAR

EA(3)

INAR

EA(5)

INAR

EA(4)

INAREAIN

AREA(10)

Page 11: Introduction to Arrays. Objectives Distinguish between a simple variable and a subscripted variable. Input, output, and manipulate values stored in a

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

Page 12: Introduction to Arrays. Objectives Distinguish between a simple variable and a subscripted variable. Input, output, and manipulate values stored in a

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

Page 13: Introduction to Arrays. Objectives Distinguish between a simple variable and a subscripted variable. Input, output, and manipulate values stored in a

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

Page 14: Introduction to Arrays. Objectives Distinguish between a simple variable and a subscripted variable. Input, output, and manipulate values stored in a

Sample Problem 9.1

Problem:

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

Page 15: Introduction to Arrays. Objectives Distinguish between a simple variable and a subscripted variable. Input, output, and manipulate values stored in a

Sample Problem 9.1 (2)

Structure Chart looks something like this:OVERALLCONTROL

A000

COMPUTE ANDOUTPUT SMALL

B010

INPUTARRAYB000

Page 16: Introduction to Arrays. Objectives Distinguish between a simple variable and a subscripted variable. Input, output, and manipulate values stored in a

Sample Problem 9.1 (3)

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

Page 17: Introduction to Arrays. Objectives Distinguish between a simple variable and a subscripted variable. Input, output, and manipulate values stored in a

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

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

Page 18: Introduction to Arrays. Objectives Distinguish between a simple variable and a subscripted variable. Input, output, and manipulate values stored in a

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

Page 19: Introduction to Arrays. Objectives Distinguish between a simple variable and a subscripted variable. Input, output, and manipulate values stored in a

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.

Page 20: Introduction to Arrays. Objectives Distinguish between a simple variable and a subscripted variable. Input, output, and manipulate values stored in a

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.

Page 21: Introduction to Arrays. Objectives Distinguish between a simple variable and a subscripted variable. Input, output, and manipulate values stored in a

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

Page 22: Introduction to Arrays. Objectives Distinguish between a simple variable and a subscripted variable. Input, output, and manipulate values stored in a

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

Page 23: Introduction to Arrays. Objectives Distinguish between a simple variable and a subscripted variable. Input, output, and manipulate values stored in a

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

Page 24: Introduction to Arrays. Objectives Distinguish between a simple variable and a subscripted variable. Input, output, and manipulate values stored in a

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.

Page 25: Introduction to Arrays. Objectives Distinguish between a simple variable and a subscripted variable. Input, output, and manipulate values stored in a

A Typical Table

1 4 5 7 2

3 8 8 9 0

4 7 4 8 1

9 9 8 8 2

Page 26: Introduction to Arrays. Objectives Distinguish between a simple variable and a subscripted variable. Input, output, and manipulate values stored in a

Initialize Two-Dimensional Array

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

Page 27: Introduction to Arrays. Objectives Distinguish between a simple variable and a subscripted variable. Input, output, and manipulate values stored in a

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

Page 28: Introduction to Arrays. Objectives Distinguish between a simple variable and a subscripted variable. Input, output, and manipulate values stored in a

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.

Page 29: Introduction to Arrays. Objectives Distinguish between a simple variable and a subscripted variable. Input, output, and manipulate values stored in a

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.

Page 30: Introduction to Arrays. Objectives Distinguish between a simple variable and a subscripted variable. Input, output, and manipulate values stored in a

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.

Page 31: Introduction to Arrays. Objectives Distinguish between a simple variable and a subscripted variable. Input, output, and manipulate values stored in a

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.

Page 32: Introduction to Arrays. Objectives Distinguish between a simple variable and a subscripted variable. Input, output, and manipulate values stored in a

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

Page 33: Introduction to Arrays. Objectives Distinguish between a simple variable and a subscripted variable. Input, output, and manipulate values stored in a

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

Page 34: Introduction to Arrays. Objectives Distinguish between a simple variable and a subscripted variable. Input, output, and manipulate values stored in a

Compute and Output Array (Continued)

Skip to new line

ROW = ROW + 1

ENDDO

Return

Page 35: Introduction to Arrays. Objectives Distinguish between a simple variable and a subscripted variable. Input, output, and manipulate values stored in a

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.

Page 36: Introduction to Arrays. Objectives Distinguish between a simple variable and a subscripted variable. Input, output, and manipulate values stored in a

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.

Page 37: Introduction to Arrays. Objectives Distinguish between a simple variable and a subscripted variable. Input, output, and manipulate values stored in a

Enrichment

Basic and Visual Basic Examples

Page 38: Introduction to Arrays. Objectives Distinguish between a simple variable and a subscripted variable. Input, output, and manipulate values stored in a

Assignment 4

Chapter 9, page 226, Exercise 11

Due Tuesday