Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Introduction to Arrays. Objectives Distinguish between a simple variable and a subscripted variable. Input, output, and manipulate values stored in a."— Presentation transcript:

1 Introduction to Arrays

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

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

4 Introduction Up 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...

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

6 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!

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

8 Arrays Only 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.

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

10 List Examples INAREA(1)INAREA(2)INAREA(3)INAREA(5)INAREA(4) INAREA INAREA(10)

11 Initialize One-Dimensional Array Start SUB = 0 DOUNTIL SUB = 10 SUB = SUB + 1 LIST(SUB) = 0 ENDDO Stop

12 Input One-Dimensional Array Start SUB = 0 DOUNTIL SUB = 10 SUB = SUB + 1 READ LIST(SUB) ENDDO Stop

13 Output One-Dimensional Array Start SUB = 0 DOUNTIL SUB = 10 SUB = SUB + 1 WRITE LIST(SUB) ENDDO Stop

14 Sample Problem 9.1 Problem: Compute and output the smallest number in a ten-element array called LIST.

15 Sample Problem 9.1 (2) Structure Chart looks something like this: OVERALL CONTROL A000 COMPUTE AND OUTPUT SMALL B010 INPUT ARRAY B000

16 Sample Problem 9.1 (3) Overall Control module: A000 Start Input Array (B000) Compute and Output Small (B010) Stop

17 Sample Problem 9.1 (4) Input Array (we’ve seen this already): B000 Enter SUB = 0 DOUNTIL SUB = 10 SUB = SUB + 1 READ LIST(SUB) ENDDO Return

18 Sample Problem 9.1 (5) Compute and Output SMALL: B010 Enter SMALL = LIST(1) SUB = 1 DOUNTIL SUB = 10 SUB = SUB + 1 IF LIST(SUB) < SMALL THEN SMALL = LIST(SUB) (ELSE) ENDIF ENDDO WRITE SMALL Return

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

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

21 Sample Problem 9.2 (3) Pseudocode for Compute and Output Average: B020 Enter ACCUM = 0 SUB = 0 DOUNTIL SUB = 10 SUB = SUB + 1 ACCUM = ACCUM + LIST(SUB) ENDDO AVG = ACCUM / SUB WRITE ‘Average is’,AVG Return

22 Sample Problems 9.3 - 9.5 These 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...

23 Table Structures In 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.)

24 Table Structures Sometimes 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.

25 A Typical Table

26 Initialize Two-Dimensional Array Start ROW = 1 DOUNTIL ROW > 4 COL = 1 DOUNTIL COL > 5 GRID(ROW,COL) = 0 COL = COL + 1 ENDDO ROW = ROW + 1 ENDDO Stop

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

28 Sample Problem 9.7 This 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.

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

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

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

32 Solution Here is the overall control module - not too difficult: A000 Start Input two-dimensional array (B000) Input one-dimensional array (B010) Compute and output array (B020) Stop

33 Compute and Output Array B020 Enter ROW = 1 DOUNTIL 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

34 Compute and Output Array (Continued) Skip to new line ROW = ROW + 1 ENDDO Return

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

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

37 Enrichment Basic and Visual Basic Examples

38 Assignment 4 Chapter 9, page 226, Exercise 11 Due Tuesday


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

Similar presentations


Ads by Google