17
Processing Arrays *Property of STI Page 1 of 17 Data Structures and Algorithms Processing Arrays Arrays @ Often advantageous for a user to store several values for the same variable in the internal memory of the computer because it decreases processing time. @ This multiple storage means there has to be more than one memory location in the computer for each variable name. @ When more than one memory location is designated for a single variable, it is called an array. Static Arrays @ This means that once the computer is told how many locations to save, that number cannot be changed unless the instruction is changed.

9 processing arrays

Embed Size (px)

DESCRIPTION

 

Citation preview

Page 1: 9 processing arrays

Processing Arrays *Property of STIPage 1 of 17

Data Structures and Algorithms

ProcessingArrays

Arrays@ Often advantageous for a user to store several

values for the same variable in the internalmemory of the computer because it decreasesprocessing time.

@ This multiple storage means there has to bemore than one memory location in thecomputer for each variable name.

@ When more than one memory location isdesignated for a single variable, it is calledan array.

Static Arrays@ This means that once the computer is told

how many locations to save, that numbercannot be changed unless the instruction ischanged.

Page 2: 9 processing arrays

Processing Arrays *Property of STIPage 2 of 17

Data Structures and Algorithms

ProcessingArrays

Dynamic Arrays@ When using dynamic arrays, the programmer

designates the number of array locations as avariable, which can be expanded or contractedduring the execution of the solution.

Base-Zero System@ Because computers are zero-based, for

counting purposes, many programminglanguages are also zero-based.

@ This means that the first array element isnumbered zero and not one.

Base-One System@ Base one is easier for the programmer to

understand since the first element will havean index of 1.

Page 3: 9 processing arrays

Processing Arrays *Property of STIPage 3 of 17

Data Structures and Algorithms

ProcessingArrays

Base-Zero Versus Base-One Arrays

Page 4: 9 processing arrays

Processing Arrays *Property of STIPage 4 of 17

Data Structures and Algorithms

ProcessingArrays

One-Dimensional Arrays

Page 5: 9 processing arrays

Processing Arrays *Property of STIPage 5 of 17

Data Structures and Algorithms

Parallel Arrays

ProcessingArrays

Page 6: 9 processing arrays

Processing Arrays *Property of STIPage 6 of 17

Data Structures and Algorithms

ProcessingArrays

Entering Data into an Array

A

ENTERA(R)

R

B

R1

1N

FlowchartAlgorithm

LOOP:R = 1 TO N STEP 1

ENTER A(R)

LOOP-END:R

R = Counter

N = Number of elements in the array

A(R) = Element Rin the A array

Page 7: 9 processing arrays

Processing Arrays *Property of STIPage 7 of 17

Data Structures and Algorithms

ProcessingArrays

A

R = 0

R = R + 1

ENTERA(R)

UNTILA(R) = -1

N = R - 1

B

REPEAT

T

*

F

1. R = 02. REPEAT

R = R+1ENTER A(R)

UNTIL A(R) = -1

*3. N = R-1

Algorithm Flowchart

Page 8: 9 processing arrays

Processing Arrays *Property of STIPage 8 of 17

Data Structures and Algorithms

A

R = 1

ENTERA(R)

WHILEA(R) <> -1

R = R + 1

B

*

F

ENTERA(R)

N = R + 1

ProcessingArrays

1. R = 12. ENTER A(R)3. WHILE A(R) <> -1

R = R+1ENTER A(R)

WHILE - END

*4. N = R-1

Algorithm Flowchart

Page 9: 9 processing arrays

Processing Arrays *Property of STIPage 9 of 17

Data Structures and Algorithms

ProcessingArrays

Printing an Array

LOOP: R=1 TO N STEP 1

PRINT A(R)

LOOP-END: R

R = Element number

N = Total number of elements

A(R) = Rth element of the A array

Algorithm Flowchart

A

PRINTA(R)

R

B

RN

11

Page 10: 9 processing arrays

Processing Arrays *Property of STIPage 10 of 17

Data Structures and Algorithms

ProcessingArrays

Accumulating the Elements of an Array

Algorithm Flowchart

A

RN

11

SUM = SUM+ A(R)

R

B

LOOP:R = 1 TO N STEP 1

SUM = SUM + A(R)

LOOP-END: R

N = Number of elements

R = Element number

SUM = Sum of theelements of A

A(R) = Rth element of thearray

24681012

123456

ATEST:

1 2 34 5 6 7

R SUM

2 6 1220 30 42

6

N

Page 11: 9 processing arrays

Processing Arrays *Property of STIPage 11 of 17

Data Structures and Algorithms

Two-Dimensional Arrays

ProcessingArrays

@ A two-dimensional array is a block of memorylocations associated with a single memory variablename and designated by row and column numbers.

Page 12: 9 processing arrays

Processing Arrays *Property of STIPage 12 of 17

Data Structures and Algorithms

ProcessingArrays

Loading a Two-Dimensional Array

123456789101112

A

ENTERA(R, C)

C

R

B

R31

1

C4

11

C = Column

R = Row

The row remainsconstant as thecolumn varies.

Array

A

1 2 3 4

5 6 7 8

9 10 11 12

1 2 3 4

1

2

3

R

C

Data Block

Row by Row

@ You load a two-dimensional arraywith nested loops.The data arenormally loadedrow by row. Whenyou load the datarow by row, theouter looprepresents the row,and the inner looprepresents thecolumn.

Page 13: 9 processing arrays

Processing Arrays *Property of STIPage 13 of 17

Data Structures and Algorithms

Printing a Two-Dimensional Array

ProcessingArrays

A

PRINTCOLUMN

HEADINGS

PRINT ROWHEADING (R)

C

RETURNCURSOR

R

B

RNR

11

CNC

11

PRINT A(R,C)W/O CURSOR

RETURNR = Row

NR = Number of rows

C = Column

NC = Number of columns

Page 14: 9 processing arrays

Processing Arrays *Property of STIPage 14 of 17

Data Structures and Algorithms

ProcessingArrays

Accumulating the Rows and Columns of a Two-Dimensional Array

@ Column 5 holds the sum of each of the rows@ Row 4 holds the sum of each of the columns@ A (4,5) holds the grand total

Page 15: 9 processing arrays

Processing Arrays *Property of STIPage 15 of 17

Data Structures and Algorithms

ProcessingArrays

A

A(R, NC + 1) =A(R, NC + 1) +

A(R,C)

A(NR + 1,C)=A(NR + 1,C) +

A(R,C)

C

A(NR + 1,NC + 1)=A(NR + 1, NC + 1)

+A(R, NC + 1)

R

B

RNR

11

CNC

11

LOOP:R = 1 TO NR STEP 1

LOOP: C = 1 TO NC STEP 1

A(R,NC + 1) = A(R,NC + 1)+ A(R,C)

A(NR + 1,C) = A(NR + 1,C)+ A(R,C)

LOOP-END: C

A(NR + 1,NC + 1) =A(NR + 1, NC + 1)

+A(R, NC + 1)

LOOP-END: R

Algorithm Flowchart

Page 16: 9 processing arrays

Processing Arrays *Property of STIPage 16 of 17

Data Structures and Algorithms

Multidimensional Arrays

ProcessingArrays

In some cases there is a need for arrays with a third oreven a fourth dimension. These arrays are calledmultidimensional arrays.

Advantages :@ Facilitate an

understandingof the data

@ Improve thereadability ofalgorithms

@ F a c i l i t a t eprocessing

Page 17: 9 processing arrays

Processing Arrays *Property of STIPage 17 of 17

Data Structures and Algorithms

Table Look-up Technique

ProcessingArrays

@ A common application for arrays is using a value to look up anothervalue in a table. A one-dimensional array would be used if theelement number can be utilized as the given value. A two-dimensional array with two columns would be used if the elementnumber cannot be utilized.

31

28

31

30

31

30

31

31

30

31

30

31

1

2

3

4

5

6

7

8

9

10

11

12

element DAYS Algorithm

1. ENTER MONTH2. DAYS_OF_THE_MONTH =DAYS(MONTH)3. PRINT DAYS_OF_MONTH4. END

FLOWCHART:

START

ENTERMONTH

DAYS_OF_THE_MONTH =

DAYS(MONTH)

PRINTDAYS_OF_

MONTH

END