arrays array basicsathena.ecs.csus.edu/~jacksocj/handouts/csc10_slides... · 2016. 4. 2. · arrays...

14
1 Arrays Chapter 8 Spring 2016, CSUS Array Basics Chapter 8.1 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 4/2/2016 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 4/2/2016 Sacramento State - CSc 10A 4 Array Basics Think of an array as a set of mailboxes Each mailbox belongs to the same variable Each mailbox has a unique number 4/2/2016 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 4/2/2016 Sacramento State - CSc 10A 6 Metaphor for Arrays

Upload: others

Post on 18-Aug-2021

2 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Arrays Array Basicsathena.ecs.csus.edu/~jacksocj/handouts/CSC10_Slides... · 2016. 4. 2. · Arrays Chapter 8 Spring 2016, CSUS Array Basics Chapter 8.1 ... It’s more efficient

1

Arrays

Chapter 8

Spring 2016, CSUS

Array Basics

Chapter 8.1

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

4/2/2016 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

4/2/2016 Sacramento State - CSc 10A 4

Array Basics

Think of an array as a set of

mailboxes

Each mailbox belongs to the

same variable

Each mailbox has a unique

number

4/2/2016 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

4/2/2016 Sacramento State - CSc 10A 6

Metaphor for Arrays

Page 2: Arrays Array Basicsathena.ecs.csus.edu/~jacksocj/handouts/CSC10_Slides... · 2016. 4. 2. · Arrays Chapter 8 Spring 2016, CSUS Array Basics Chapter 8.1 ... It’s more efficient

2

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)

4/2/2016 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)

4/2/2016 Sacramento State - CSc 10A 8

What Value Do We Start With?

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

4/2/2016 Sacramento State - CSc 10A 9

Zero Indexing

Creating

Arrays

Chapter 8.1

Arrays are created pretty

much the same as any other

variable

However, since the array can

contain multiple values, you

must specify its size

4/2/2016 Sacramento State - CSc 10A 11

Creating Arrays

Declare type name [ size ]

4/2/2016 Sacramento State - CSc 10A 12

Book Pseudocode: Array Declare

Real, Integer, or String

total number of elements

Page 3: Arrays Array Basicsathena.ecs.csus.edu/~jacksocj/handouts/CSC10_Slides... · 2016. 4. 2. · Arrays Chapter 8 Spring 2016, CSUS Array Basics Chapter 8.1 ... It’s more efficient

3

Declare String employees[50]

Declare Real salesAmounts[7]

4/2/2016 Sacramento State - CSc 10A 13

Examples

4/2/2016 Sacramento State - CSc 10A 14

Accessing Elements

Declare Real age[3]

4/2/2016 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

4/2/2016 Sacramento State - CSc 10A 16

Accessing Each Cell

The notation is incredibly

simple

Simply follow the array name

by square brackets and the

index of the element you

want

4/2/2016 Sacramento State - CSc 10A 17

Accessing Each Cell

name [index]

4/2/2016 Sacramento State - CSc 10A 18

How You Access an Element

Variable Name

0, 1, 2, …

Page 4: Arrays Array Basicsathena.ecs.csus.edu/~jacksocj/handouts/CSC10_Slides... · 2016. 4. 2. · Arrays Chapter 8 Spring 2016, CSUS Array Basics Chapter 8.1 ... It’s more efficient

4

sacState

totalCost

test[4]

name[2]

4/2/2016 Sacramento State - CSc 10A 19

Example Variables

Normal

variables

Array

elements

8-20

Array Elements

4/2/2016 Sacramento State - CSc 10A

Declare Integer test[2]

Set test[0] = 1947

Set test[1] = 42

Display test[0]

Display test[1]

4/2/2016 Sacramento State - CSc 10A 21

Array Example – What Happens?

Declare Integer test[2]

Set test[0] = 1947

Set test[1] = 42

4/2/2016 Sacramento State - CSc 10A 22

Array Example – What Happens?

0

test

1

0

test

1

Declare Integer test[2]

Set test[0] = 1947

Set test[1] = 42

4/2/2016 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

4/2/2016 Sacramento State - CSc 10A 24

Array Example – What Happens?

42

Page 5: Arrays Array Basicsathena.ecs.csus.edu/~jacksocj/handouts/CSC10_Slides... · 2016. 4. 2. · Arrays Chapter 8 Spring 2016, CSUS Array Basics Chapter 8.1 ... It’s more efficient

5

Declare Integer test[2]

Set test[0] = 1947

Set test[1] = 42

Display test[0]

Display test[1]

4/2/2016 Sacramento State - CSc 10A 25

Array Example

1947

42

4/2/2016 Sacramento State - CSc 10A 26

Array Example Output

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

4/2/2016 Sacramento State - CSc 10A

Declare String days[7] = "Sunday",

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

"Thursday", "Friday", "Saturday"

4/2/2016 Sacramento State - CSc 10A 28

Example

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

4/2/2016 Sacramento State - CSc 10A

Declare String days[7]

days[7] = "Saturday"

4/2/2016 Sacramento State - CSc 10A 30

Example

Invalid because there is no 7 index

Page 6: Arrays Array Basicsathena.ecs.csus.edu/~jacksocj/handouts/CSC10_Slides... · 2016. 4. 2. · Arrays Chapter 8 Spring 2016, CSUS Array Basics Chapter 8.1 ... It’s more efficient

6

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

4/2/2016 Sacramento State - CSc 10A

Loops and

Arrays

Chapter 8.1

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

4/2/2016 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

4/2/2016 Sacramento State - CSc 10A 34

Loops and Arrays

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

4/2/2016 Sacramento State - CSc 10A 35

Tappa Kegga Bru

Cuppa Kappa Chino

Hu Delta Phart

Eta Lotta Pi

4/2/2016 Sacramento State - CSc 10A 36

Greek Example Output

Page 7: Arrays Array Basicsathena.ecs.csus.edu/~jacksocj/handouts/CSC10_Slides... · 2016. 4. 2. · Arrays Chapter 8 Spring 2016, CSUS Array Basics Chapter 8.1 ... It’s more efficient

7

Declare Integer n

Declare String days[7] = "Sunday",

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

"Thursday", "Friday", "Saturday"

For n = 0 to 6

Display days[n]

End For

4/2/2016 Sacramento State - CSc 10A 37

Loop Example

Sunday

Monday

Tuesday

Wednesday

Thursday

Friday

Saturday

4/2/2016 Sacramento State - CSc 10A 38

Loop Example Output

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

4/2/2016 Sacramento State - CSc 10A 39

Loop Example 2

0 Sunday

1 Monday

2 Tuesday

3 Wednesday

4 Thursday

5 Friday

6 Saturday

4/2/2016 Sacramento State - CSc 10A 40

Loop Example 2 Output

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

4/2/2016 Sacramento State - CSc 10A 41

85 passes

98 passes

61 fails

4/2/2016 Sacramento State - CSc 10A 42

Array Example 2 Output

Page 8: Arrays Array Basicsathena.ecs.csus.edu/~jacksocj/handouts/CSC10_Slides... · 2016. 4. 2. · Arrays Chapter 8 Spring 2016, CSUS Array Basics Chapter 8.1 ... It’s more efficient

8

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

4/2/2016 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

4/2/2016 Sacramento State - CSc 10A 44

For Each Example

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

4/2/2016 Sacramento State - CSc 10A 46

Partially Filled Array

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

4/2/2016 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

4/2/2016 Sacramento State - CSc 10A 48

100 Capacity

Not using all of it

Page 9: Arrays Array Basicsathena.ecs.csus.edu/~jacksocj/handouts/CSC10_Slides... · 2016. 4. 2. · Arrays Chapter 8 Spring 2016, CSUS Array Basics Chapter 8.1 ... It’s more efficient

9

How many tests?

3

65

89

77

4/2/2016 Sacramento State - CSc 10A 49

Array Example 2 Output

Sequentially

Searching an

Array

Chapter 8.2

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

4/2/2016 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

4/2/2016 Sacramento State - CSc 10A 52

How it Works

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

4/2/2016 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 10: Arrays Array Basicsathena.ecs.csus.edu/~jacksocj/handouts/CSC10_Slides... · 2016. 4. 2. · Arrays Chapter 8 Spring 2016, CSUS Array Basics Chapter 8.1 ... It’s more efficient

10

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

4/2/2016 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…

4/2/2016 Sacramento State - CSc 10A

Set total = 0

For n = 0 to SIZE – 1

total = total + score[n]

End For

Set average = total / SIZE

4/2/2016 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

4/2/2016 Sacramento State - CSc 10A 8-58

Highest and Lowest Value

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

4/2/2016 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

4/2/2016 Sacramento State - CSc 10A 60

Highest Value

Page 11: Arrays Array Basicsathena.ecs.csus.edu/~jacksocj/handouts/CSC10_Slides... · 2016. 4. 2. · Arrays Chapter 8 Spring 2016, CSUS Array Basics Chapter 8.1 ... It’s more efficient

11

Set lowest = scores[0]

For n = 1 to count – 1

if score[n] < lowest

Set lowest = score[n]

end if

End For

4/2/2016 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

4/2/2016 Sacramento State - CSc 10A 62

Example 2

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-634/2/2016 Sacramento State - CSc 10A

Parallel

Arrays

Chapter 8.4

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

4/2/2016 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

4/2/2016 Sacramento State - CSc 10A 66

Parallel Arrays

Page 12: Arrays Array Basicsathena.ecs.csus.edu/~jacksocj/handouts/CSC10_Slides... · 2016. 4. 2. · Arrays Chapter 8 Spring 2016, CSUS Array Basics Chapter 8.1 ... It’s more efficient

12

8-67

Parallel Arrays

4/2/2016 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

4/2/2016 Sacramento State - CSc 10A 68

Example: Part 1 – Input

Set best = 0

For n = 1 to SIZE – 1

If cash[n] > cash[best]

Set best = n

End If

End For

4/2/2016 Sacramento State - CSc 10A 69

Example: Part 2 – Search

Display name[highest],

Display "got the most donations of $",

cash[best]

4/2/2016 Sacramento State - CSc 10A 70

Example: Part 3 – Results

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

4/2/2016 Sacramento State - CSc 10A 71

Example Output

Two

Dimension

Arrays

Chapter 8.5

Page 13: Arrays Array Basicsathena.ecs.csus.edu/~jacksocj/handouts/CSC10_Slides... · 2016. 4. 2. · Arrays Chapter 8 Spring 2016, CSUS Array Basics Chapter 8.1 ... It’s more efficient

13

A two-dimensional array is

like several identical arrays

put together

Suppose a teacher has six

students who take five tests

4/2/2016 Sacramento State - CSc 10A 73

Two Dimension Arrays

4/2/2016 Sacramento State - CSc 10A 8-74

Two-Dimensional Arrays

Two size variables are required when

declaring two dimensional arrays

Accessing is done with two loops, and both

indexes

4/2/2016 Sacramento State - CSc 10A 75

Multiple Sizes Needed

Constant Integer ROWS = 3

Constant Integer COLS = 4

Declare Integer values[ROWS][COLS]

4/2/2016 Sacramento State - CSc 10A 76

Declaring Arrays

For row = 0 To ROWS -1

For col = 0 To COLS – 1

Display "Enter a number."

Input values[row][col]

End For

End For

4/2/2016 Sacramento State - CSc 10A 77

Using Loops

Arrays of

Three or More

Dimensions

Chapter 8.6

Page 14: Arrays Array Basicsathena.ecs.csus.edu/~jacksocj/handouts/CSC10_Slides... · 2016. 4. 2. · Arrays Chapter 8 Spring 2016, CSUS Array Basics Chapter 8.1 ... It’s more efficient

14

Arrays can also be three or

more dimensions

In fact, there are no

limitations once the number

of dimensions

4/2/2016 Sacramento State - CSc 10A 79

Arrays of Three or More Dimensions

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

4/2/2016 Sacramento State - CSc 10A 8-80

Arrays of Three or More Dimensions

4/2/2016 Sacramento State - CSc 10A 81

Arrays of Three or More Dimensions