+ All Categories
Home > Documents > Arrays - California State University,...

Arrays - California State University,...

Date post: 27-Mar-2020
Category:
Upload: others
View: 0 times
Download: 0 times
Share this document with a friend
41
1 Arrays Chapter 8 Spring 2017, CSUS Array Basics Chapter 8.1
Transcript
Page 1: Arrays - California State University, Sacramentoathena.ecs.csus.edu/~jacksocj/handouts/CSC10A_Slides_Chapter8_Spring... · 2 Normally, variables only have one piece of data associated

1

Arrays

Chapter 8

Spring 2017, CSUS

Array Basics

Chapter 8.1

Page 2: Arrays - California State University, Sacramentoathena.ecs.csus.edu/~jacksocj/handouts/CSC10A_Slides_Chapter8_Spring... · 2 Normally, variables only have one piece of data associated

2

Normally, variables only have

one piece of data associated

with them

An array allows you to store a

group of items of the same

data type together in memory

1/21/2017 Sacramento State - CSc 10A 3

Array Basics

Why? Instead of creating multiple similar

variables such as employee1, employee2,

employee3 and so on…

It’s more efficient to create just one variable

– with a shared, but multiple values

1/21/2017 Sacramento State - CSc 10A 4

Array Basics

Page 3: Arrays - California State University, Sacramentoathena.ecs.csus.edu/~jacksocj/handouts/CSC10A_Slides_Chapter8_Spring... · 2 Normally, variables only have one piece of data associated

3

Think of an array as a set of

mailboxes

Each mailbox belongs to the

same variable

Each mailbox has a unique

number

1/21/2017 Sacramento State - CSc 10A 5

Metaphor for Arrays

... or think of arrays as a

group of boxes

Each box belongs to the

same variable

Each box has a unique

number

1/21/2017 Sacramento State - CSc 10A 6

Metaphor for Arrays

Page 4: Arrays - California State University, Sacramentoathena.ecs.csus.edu/~jacksocj/handouts/CSC10A_Slides_Chapter8_Spring... · 2 Normally, variables only have one piece of data associated

4

Each value located in an

array is called an element

Each can be accessed using

an unique number called an

index (also called a subscript)

1/21/2017 Sacramento State - CSc 10A 7

Array Terminology

So, what are the valid values

for the index?

Most languages use 0-indexing

• other languages use 1-indexing

• the success of C set the standard

• nowadays, most major languages

use 0-indexing (e.g. Visual Basic)

1/21/2017 Sacramento State - CSc 10A 8

What Value Do We Start With?

Page 5: Arrays - California State University, Sacramentoathena.ecs.csus.edu/~jacksocj/handouts/CSC10A_Slides_Chapter8_Spring... · 2 Normally, variables only have one piece of data associated

5

This means the first element

in any array has the index 0

So, even though this will be

odd and strange, it is

something you must learn to

live with

1/21/2017 Sacramento State - CSc 10A 9

Zero Indexing

Creating

Arrays

Chapter 8.1

Page 6: Arrays - California State University, Sacramentoathena.ecs.csus.edu/~jacksocj/handouts/CSC10A_Slides_Chapter8_Spring... · 2 Normally, variables only have one piece of data associated

6

Arrays are created pretty

much the same as any other

variable

However, since the array can

contain multiple values, you

must specify its size

1/21/2017 Sacramento State - CSc 10A 11

Creating Arrays

Declare type name [ size ]

1/21/2017 Sacramento State - CSc 10A 12

Book Pseudocode: Array Declare

Real, Integer, or String

total number of elements

Page 7: Arrays - California State University, Sacramentoathena.ecs.csus.edu/~jacksocj/handouts/CSC10A_Slides_Chapter8_Spring... · 2 Normally, variables only have one piece of data associated

7

Declare String employees[50]

Declare Real salesAmounts[7]

1/21/2017 Sacramento State - CSc 10A 13

Examples

1/21/2017 Sacramento State - CSc 10A 14

Accessing Elements

Page 8: Arrays - California State University, Sacramentoathena.ecs.csus.edu/~jacksocj/handouts/CSC10A_Slides_Chapter8_Spring... · 2 Normally, variables only have one piece of data associated

8

Declare Real age[3]

1/21/2017 Sacramento State - CSc 10A 15

Array Declarations

0

age

1

2

After an array is created you

can read/write any element

You can also access the

entire array using the variable

name

1/21/2017 Sacramento State - CSc 10A 16

Accessing Each Cell

Page 9: Arrays - California State University, Sacramentoathena.ecs.csus.edu/~jacksocj/handouts/CSC10A_Slides_Chapter8_Spring... · 2 Normally, variables only have one piece of data associated

9

The notation is incredibly

simple

Simply follow the array name

by square brackets and the

index of the element you

want

1/21/2017 Sacramento State - CSc 10A 17

Accessing Each Cell

name [index]

1/21/2017 Sacramento State - CSc 10A 18

How You Access an Element

Variable Name

0, 1, 2, …

Page 10: Arrays - California State University, Sacramentoathena.ecs.csus.edu/~jacksocj/handouts/CSC10A_Slides_Chapter8_Spring... · 2 Normally, variables only have one piece of data associated

10

sacState

totalCost

test[4]

name[2]

1/21/2017 Sacramento State - CSc 10A 19

Example Variables

Normal

variables

Array

elements

8-20

Array Elements

1/21/2017 Sacramento State - CSc 10A

Page 11: Arrays - California State University, Sacramentoathena.ecs.csus.edu/~jacksocj/handouts/CSC10A_Slides_Chapter8_Spring... · 2 Normally, variables only have one piece of data associated

11

Declare Integer test[2]

Set test[0] = 1947

Set test[1] = 42

Display test[0]

Display test[1]

1/21/2017 Sacramento State - CSc 10A 21

Array Example – What Happens?

Declare Integer test[2]

Set test[0] = 1947

Set test[1] = 42

1/21/2017 Sacramento State - CSc 10A 22

Array Example – What Happens?

0

test

1

Page 12: Arrays - California State University, Sacramentoathena.ecs.csus.edu/~jacksocj/handouts/CSC10A_Slides_Chapter8_Spring... · 2 Normally, variables only have one piece of data associated

12

0

test

1

Declare Integer test[2]

Set test[0] = 1947

Set test[1] = 42

1/21/2017 Sacramento State - CSc 10A 23

Array Example – What Happens?

1947

19470

test

1

Declare Integer test[2]

Set test[0] = 1947

Set test[1] = 42

1/21/2017 Sacramento State - CSc 10A 24

Array Example – What Happens?

42

Page 13: Arrays - California State University, Sacramentoathena.ecs.csus.edu/~jacksocj/handouts/CSC10A_Slides_Chapter8_Spring... · 2 Normally, variables only have one piece of data associated

13

Declare Integer test[2]

Set test[0] = 1947

Set test[1] = 42

Display test[0]

Display test[1]

1/21/2017 Sacramento State - CSc 10A 25

Array Example

1947

42

1/21/2017 Sacramento State - CSc 10A 26

Array Example Output

Page 14: Arrays - California State University, Sacramentoathena.ecs.csus.edu/~jacksocj/handouts/CSC10A_Slides_Chapter8_Spring... · 2 Normally, variables only have one piece of data associated

14

Just like regular variables, arrays can be

initialized to 0 or specific values

Not all languages support this…

• however, the big ones such as C#, Java, and

Visual Basic do

• even though the notation varies a bit

8-27

Array Initialization

1/21/2017 Sacramento State - CSc 10A

Declare String days[7] = "Sunday",

"Monday", "Tuesday", "Wednesday",

"Thursday", "Friday", "Saturday"

1/21/2017 Sacramento State - CSc 10A 28

Example

Page 15: Arrays - California State University, Sacramentoathena.ecs.csus.edu/~jacksocj/handouts/CSC10A_Slides_Chapter8_Spring... · 2 Normally, variables only have one piece of data associated

15

Sometimes the program will use an invalid

index

Naturally, this is attempting to access data

that does not exist

Array bounds checking prevents the use of

an invalid subscript

8-29

Bounds Checking

1/21/2017 Sacramento State - CSc 10A

Declare String days[7]

days[7] = "Saturday"

1/21/2017 Sacramento State - CSc 10A 30

Example

Invalid because there is no 7 index

Page 16: Arrays - California State University, Sacramentoathena.ecs.csus.edu/~jacksocj/handouts/CSC10A_Slides_Chapter8_Spring... · 2 Normally, variables only have one piece of data associated

16

A common error is running a loop one time

more than is necessary, exceeding the

bound of the array

This is an off-by-one error and is happens

quite often (especially so because we use

0-indexing)

8-31

Bounds Checking

1/21/2017 Sacramento State - CSc 10A

Loops and

Arrays

Chapter 8.1

Page 17: Arrays - California State University, Sacramentoathena.ecs.csus.edu/~jacksocj/handouts/CSC10A_Slides_Chapter8_Spring... · 2 Normally, variables only have one piece of data associated

17

For Loops are extremely well

suited for iterating through all

the values of an array

In fact, one of the reasons

For Loops exists – is to

interact with arrays

1/21/2017 Sacramento State - CSc 10A 33

For Loops and Arrays

Using For Loops, it is easy to

access all the elements of an

array linearly

The loop variable is used as

the index in the array

1/21/2017 Sacramento State - CSc 10A 34

Loops and Arrays

Page 18: Arrays - California State University, Sacramentoathena.ecs.csus.edu/~jacksocj/handouts/CSC10A_Slides_Chapter8_Spring... · 2 Normally, variables only have one piece of data associated

18

Declare String name[4]

Declare Integer n

name[0] = "Tappa Kegga Bru"

name[1] = "Cuppa Kappa Chino"

name[2] = "Hu Delta Phart"

name[3] = "Eta Lotta Pi"

For n = 0 TO 3

Display name[n]

End For

1/21/2017 Sacramento State - CSc 10A 35

Tappa Kegga Bru

Cuppa Kappa Chino

Hu Delta Phart

Eta Lotta Pi

1/21/2017 Sacramento State - CSc 10A 36

Greek Example Output

Page 19: Arrays - California State University, Sacramentoathena.ecs.csus.edu/~jacksocj/handouts/CSC10A_Slides_Chapter8_Spring... · 2 Normally, variables only have one piece of data associated

19

Declare Integer n

Declare String days[7] = "Sunday",

"Monday", "Tuesday", "Wednesday",

"Thursday", "Friday", "Saturday"

For n = 0 to 6

Display days[n]

End For

1/21/2017 Sacramento State - CSc 10A 37

Loop Example

Sunday

Monday

Tuesday

Wednesday

Thursday

Friday

Saturday

1/21/2017 Sacramento State - CSc 10A 38

Loop Example Output

Page 20: Arrays - California State University, Sacramentoathena.ecs.csus.edu/~jacksocj/handouts/CSC10A_Slides_Chapter8_Spring... · 2 Normally, variables only have one piece of data associated

20

Declare Integer n

Declare String days[7] = "Sunday",

"Monday", "Tuesday", "Wednesday",

"Thursday", "Friday", "Saturday"

For n = 0 to 6

Display n, days[n]

End For

1/21/2017 Sacramento State - CSc 10A 39

Loop Example 2

0 Sunday

1 Monday

2 Tuesday

3 Wednesday

4 Thursday

5 Friday

6 Saturday

1/21/2017 Sacramento State - CSc 10A 40

Loop Example 2 Output

Page 21: Arrays - California State University, Sacramentoathena.ecs.csus.edu/~jacksocj/handouts/CSC10A_Slides_Chapter8_Spring... · 2 Normally, variables only have one piece of data associated

21

Declare Real score[3]

Set score[0] = 85

Set score[1] = 98

Set score[2] = 61

For n = 0 to 2

If score[n] >= 70 Then

Display score[n], " passes"

Else

Display score[n], " fails"

End If

End For

1/21/2017 Sacramento State - CSc 10A 41

85 passes

98 passes

61 fails

1/21/2017 Sacramento State - CSc 10A 42

Array Example 2 Output

Page 22: Arrays - California State University, Sacramentoathena.ecs.csus.edu/~jacksocj/handouts/CSC10A_Slides_Chapter8_Spring... · 2 Normally, variables only have one piece of data associated

22

Some languages provide a For Each loop

It works with an array, iterating once for

each array element

During each iteration, the loop copies an

element's value to a variable.

8-43

The For Each Loop

1/21/2017 Sacramento State - CSc 10A

Constant Integer SIZE = 4

Declare Integer numbers[SIZE] = 5, 10, 15, 20

Declare Integer num

For Each num In numbers

Display num

End For

1/21/2017 Sacramento State - CSc 10A 44

For Each Example

Page 23: Arrays - California State University, Sacramentoathena.ecs.csus.edu/~jacksocj/handouts/CSC10A_Slides_Chapter8_Spring... · 2 Normally, variables only have one piece of data associated

23

Partially Filled

Array

Chapter 8.1

Sometimes an array is only

partially filled

To avoid processing the

unfilled elements, you must

use an integer variable that

holds the number of items

stored in the array

1/21/2017 Sacramento State - CSc 10A 46

Partially Filled Array

Page 24: Arrays - California State University, Sacramentoathena.ecs.csus.edu/~jacksocj/handouts/CSC10A_Slides_Chapter8_Spring... · 2 Normally, variables only have one piece of data associated

24

When the array is empty, 0 is stored in this

variable

The variable is incremented each time an

item is added to the array

The variable's value is used as the array's

size when stepping through the array.

8-47

Partially Filled Array

1/21/2017 Sacramento State - CSc 10A

Declare Real score[100]

Declare Integer Count

Display "How many tests?"

Input Count

For n = 0 to Count - 1

Input score[n]

End For

1/21/2017 Sacramento State - CSc 10A 48

100 Capacity

Not using all of it

Page 25: Arrays - California State University, Sacramentoathena.ecs.csus.edu/~jacksocj/handouts/CSC10A_Slides_Chapter8_Spring... · 2 Normally, variables only have one piece of data associated

25

How many tests?

3

65

89

77

1/21/2017 Sacramento State - CSc 10A 49

Array Example 2 Output

Sequentially

Searching an

Array

Chapter 8.2

Page 26: Arrays - California State University, Sacramentoathena.ecs.csus.edu/~jacksocj/handouts/CSC10A_Slides_Chapter8_Spring... · 2 Normally, variables only have one piece of data associated

26

A sequential search algorithm

is a simple technique for

finding an item in a string or

numeric array

One of the most common and

ways of locating data

8-51

Sequentially Searching an Array

1/21/2017 Sacramento State - CSc 10A

Uses a loop to sequentially

step through an array

Compares each element with

the value being searched for

Stops when the value is

found or the end of the array

is hit

1/21/2017 Sacramento State - CSc 10A 52

How it Works

Page 27: Arrays - California State University, Sacramentoathena.ecs.csus.edu/~jacksocj/handouts/CSC10A_Slides_Chapter8_Spring... · 2 Normally, variables only have one piece of data associated

27

Set found = False

Set index = 0

While not found AND index <= SIZE - 1

If array[index] == searchValue Then

Set found = True

Else

Set index = index + 1

End If

End While

1/21/2017 Sacramento State - CSc 10A 53

Example

End if found

If not, look at next item (next index)

Processing

the Contents

of an Array

Chapter 8.3

Page 28: Arrays - California State University, Sacramentoathena.ecs.csus.edu/~jacksocj/handouts/CSC10A_Slides_Chapter8_Spring... · 2 Normally, variables only have one piece of data associated

28

It is common to use an array

to store multiple values to be

analyzed

The information is first stored,

and then analyzed later by a

different loop

8-55

Processing the Contents of an Array

1/21/2017 Sacramento State - CSc 10A

Loops are used to

accumulate the values –

create a total

Then, the total is simply

divided

by the size

8-56

Calculating the Average…

1/21/2017 Sacramento State - CSc 10A

Page 29: Arrays - California State University, Sacramentoathena.ecs.csus.edu/~jacksocj/handouts/CSC10A_Slides_Chapter8_Spring... · 2 Normally, variables only have one piece of data associated

29

Set total = 0

For n = 0 to SIZE – 1

total = total + score[n]

End For

Set average = total / SIZE

1/21/2017 Sacramento State - CSc 10A 57

Calculate the Average

Finding the highest & lowest

values in an array is common

It basically works by scanning

the array and setting a high

(or low value) based on the

current value

1/21/2017 Sacramento State - CSc 10A 8-58

Highest and Lowest Value

Page 30: Arrays - California State University, Sacramentoathena.ecs.csus.edu/~jacksocj/handouts/CSC10A_Slides_Chapter8_Spring... · 2 Normally, variables only have one piece of data associated

30

Create a variable to hold the highest value

Assign the value at element 0 to the highest

Use a loop to step through the rest of the elements

Each iteration, a comparison is made to the highest variable

If the element is greater than the highest value, that value is then the assigned to the highest variable

1/21/2017 Sacramento State - CSc 10A 8-59

Steps Involved – Highest Value

Set highest = scores[0]

For n = 1 to count – 1

If score[n] > highest

Set highest = score[n]

End if

End For

1/21/2017 Sacramento State - CSc 10A 60

Highest Value

Page 31: Arrays - California State University, Sacramentoathena.ecs.csus.edu/~jacksocj/handouts/CSC10A_Slides_Chapter8_Spring... · 2 Normally, variables only have one piece of data associated

31

Set lowest = scores[0]

For n = 1 to count – 1

if score[n] < lowest

Set lowest = score[n]

end if

End For

1/21/2017 Sacramento State - CSc 10A 61

Lowest Value

The following is a full program that uses many of the techniques we covered

• arrays

• counter – for partially filled arrays

• sentinel

• loops

• …and more

So, if you understand this program, then you understand the material

1/21/2017 Sacramento State - CSc 10A 62

Example 2

Page 32: Arrays - California State University, Sacramentoathena.ecs.csus.edu/~jacksocj/handouts/CSC10A_Slides_Chapter8_Spring... · 2 Normally, variables only have one piece of data associated

32

Constant Integer SIZE = 100

Declare Integer values[SIZE]

Declare Integer count = 0

Declare Integer number

Declare Integer Index

Display "Enter a number, or -1 to quit."

Input number

While (number != -1 AND count < SIZE)

Set values[count] = number

Set count = count + 1

Display "Enter a number, or -1 to quit."

Input number

End While

Display "Here are the values you entered:"

For index = 0 To count - 1

Display values[index]

End For

8-631/21/2017 Sacramento State - CSc 10A

Parallel

Arrays

Chapter 8.4

Page 33: Arrays - California State University, Sacramentoathena.ecs.csus.edu/~jacksocj/handouts/CSC10A_Slides_Chapter8_Spring... · 2 Normally, variables only have one piece of data associated

33

Often more than one piece of

information needs to be

saved for the same "object"

One common approach is to

use multiple arrays – one for

each type of data

1/21/2017 Sacramento State - CSc 10A 65

Parallel Arrays

These are called parallel

arrays

They are separate arrays but

we used the same index for

each

By using the same index, we

can establish a relationship

between them

1/21/2017 Sacramento State - CSc 10A 66

Parallel Arrays

Page 34: Arrays - California State University, Sacramentoathena.ecs.csus.edu/~jacksocj/handouts/CSC10A_Slides_Chapter8_Spring... · 2 Normally, variables only have one piece of data associated

34

8-67

Parallel Arrays

1/21/2017 Sacramento State - CSc 10A

Constant SIZE = 10

Declare String name[SIZE]

Declare Real cash[SIZE]

For n = 0 to SIZE – 1

Input name[n]

Input cash[n]

End For

1/21/2017 Sacramento State - CSc 10A 68

Example: Part 1 – Input

Page 35: Arrays - California State University, Sacramentoathena.ecs.csus.edu/~jacksocj/handouts/CSC10A_Slides_Chapter8_Spring... · 2 Normally, variables only have one piece of data associated

35

Set best = 0

For n = 1 to SIZE – 1

If cash[n] > cash[best]

Set best = n

End If

End For

1/21/2017 Sacramento State - CSc 10A 69

Example: Part 2 – Search

Display name[highest],

Display "got the most donations of $",

cash[best]

1/21/2017 Sacramento State - CSc 10A 70

Example: Part 3 – Results

Page 36: Arrays - California State University, Sacramentoathena.ecs.csus.edu/~jacksocj/handouts/CSC10A_Slides_Chapter8_Spring... · 2 Normally, variables only have one piece of data associated

36

Tappa Kegga Bru

32.23

Lambda Lambda Lambda

432.11

Eta Lotta Pi

54.25

Lambda Lambda Lambda

got the most donations of $432.11

1/21/2017 Sacramento State - CSc 10A 71

Example Output

Two

Dimension

Arrays

Chapter 8.5

Page 37: Arrays - California State University, Sacramentoathena.ecs.csus.edu/~jacksocj/handouts/CSC10A_Slides_Chapter8_Spring... · 2 Normally, variables only have one piece of data associated

37

A two-dimensional array is

like several identical arrays

put together

Suppose a teacher has six

students who take five tests

1/21/2017 Sacramento State - CSc 10A 73

Two Dimension Arrays

1/21/2017 Sacramento State - CSc 10A 8-74

Two-Dimensional Arrays

Page 38: Arrays - California State University, Sacramentoathena.ecs.csus.edu/~jacksocj/handouts/CSC10A_Slides_Chapter8_Spring... · 2 Normally, variables only have one piece of data associated

38

Two size variables are required when

declaring two dimensional arrays

Accessing is done with two loops, and both

indexes

1/21/2017 Sacramento State - CSc 10A 75

Multiple Sizes Needed

Constant Integer ROWS = 3

Constant Integer COLS = 4

Declare Integer values[ROWS][COLS]

1/21/2017 Sacramento State - CSc 10A 76

Declaring Arrays

Page 39: Arrays - California State University, Sacramentoathena.ecs.csus.edu/~jacksocj/handouts/CSC10A_Slides_Chapter8_Spring... · 2 Normally, variables only have one piece of data associated

39

For row = 0 To ROWS -1

For col = 0 To COLS – 1

Display "Enter a number."

Input values[row][col]

End For

End For

1/21/2017 Sacramento State - CSc 10A 77

Using Loops

Arrays of

Three or More

Dimensions

Chapter 8.6

Page 40: Arrays - California State University, Sacramentoathena.ecs.csus.edu/~jacksocj/handouts/CSC10A_Slides_Chapter8_Spring... · 2 Normally, variables only have one piece of data associated

40

Arrays can also be three or

more dimensions

In fact, there are no

limitations once the number

of dimensions

1/21/2017 Sacramento State - CSc 10A 79

Arrays of Three or More Dimensions

Declare Real seats[3][5][8]

1/21/2017 Sacramento State - CSc 10A 8-80

Arrays of Three or More Dimensions

Page 41: Arrays - California State University, Sacramentoathena.ecs.csus.edu/~jacksocj/handouts/CSC10A_Slides_Chapter8_Spring... · 2 Normally, variables only have one piece of data associated

41

1/21/2017 Sacramento State - CSc 10A 81

Arrays of Three or More Dimensions


Recommended