Chapter 5:
Arrays: Lists and Tables
Extended Prelude to Programming
Concepts & Design, 3/e
by
Stewart Venit and Elizabeth Drake
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
5.1 One-Dimensional Arrays
• A one-dimensional array is a list of related data of
the same data type, referenced by the same name.
• An array is composed of elements, each of which is
referenced by the array name and the element’s
position in the array.
• Arrays are stored in consecutive memory locations.
• The position of an element in an array is called a
subscript or an index.
– In the text example, Scores[3], Scores is the array
name and 3 is the subscript.
5-2
Declaring Arrays
• When an array is declared, the computer sets aside memory to store the
values of the elements of the array.
• Declaring an array means telling the computer what is the data type of
the array and how many elements will be stored in it.
• Pseudocode to declare arrays:
Declare Height[5] Of Integers
declares an array with 5 elements, all of which are integers
• Note: the first element of an array has subscipt 0
Address:
Height[0]
Height[1]
Height[2]
Height[3]
Content:
5
8
13
22
Height[4]
39
5-3
Filling (loading) an Array
Here is how to load an array with 52 values. The values represent a
salesperson’s sales for each week of the year.
Declare WeeklySales[52] Of Reals
Declare K As Integer
For K = 0 Step 1 to 51
Write “Enter sales for week #”, K + 1
Input WeeklySales[K]
End For
Note that the sales for Week 1 correspond to the element
WeeklySales[0] and the sales for Week 52 correspond to
the element WeeklySales[51].
5-4
Parallel Arrays
• Parallel arrays can be used when array variables are related to
each other by position, or subscript.
• Tip: It is necessary that the data in each array be loaded in the
correct order.
• If we have arrays NumberGrade and LetterGrade (as
below), each element of NumberGrade is related to an
element in LetterGrade by the position in the array.
NumberGrade:
LetterGrade:
90
“A”
80
”B”
70
”C”
60
”D”
50
”F”
5-5
Why Use Arrays?
• Arrays reduce the number of variable names in the program.
Sales[K] versus Sales1, Sales2, SalesN, ….
• Arrays improve efficiency by allowing data to be read into the
program once but processed as many times as necessary.
• Arrays increase the flexibility of the program.
• Arrays reduce the number of If statements needed in selection
processing.
If
If
If
If
Sales[K] Then rather than
Sales1 Then …
Sales2 Then …
Sales3 Then …
5-6
5.2 Searching and Sorting Arrays
• If we have multiple, related arrays, with the elements
ordered correctly, we can find values in one array
based on a search key value in another array.
• A search key is a known value for one of the arrays.
In the text example, a specific value of ‘Flight’ can be
used to find the Origin, Time, and Gate of that flight. It
can be used as a search key.
• This is called a table lookup or serial search.
5-7
The Serial Search
• 3 steps:
– Load, or populate, the arrays.
– Search the array that contains the key to find a
match.
– Display the key and corresponding values in the
non-key arrays with the same subscript as the one
with the key value, if a match is found.
– Display an ‘unsuccessful search’ message if no
matches are found.
5-8
The Serial Search
(continued)
• Use a flag, or switch, to decide which message
to print.
• A flag is a variable that is set to zero or one,
depending on the results of an operation.
• The value of the flag can be checked later in the
program with an If statement to determine
which action to take.
5-9
The Serial Search
(continued)
Pseudocode for the serial search of an array named KeyData containing N
elements:
Set the subscript (here named Index) of the current
table key equal to 0
Set a flag (here named Found) equal to 0
While (Found = 0) AND (Index < N)
If KeyData[Index] = Key Then
Set Found = 1
End If
Set Index = Index + 1
End While
If Found = 1 Then
Display array element with subscript Index – 1
Else
Display an “unsuccessful search” message
End If
5-10
The Bubble Sort Technique
• To sort data means to arrange it in a particular
numerical or alphabetical order, ascending or
descending.
• To sort small amounts of data, use the bubble
sort technique.
–
–
–
–
Make several passes through the data.
Compare adjacent pairs of data.
If the pairs are not in order, swap them.
Continue until all data is processed.
5-11
Swapping Values
To swap two values, you must store one of them in a temporary
storage location while you make the swap.
To interchange array elements Array[K] and Array[K+1], the
pseudocode for the swap looks like this:
Set Temp = Array[K]
Set Array[K] = Array[K+1]
Set Array[K+1] = Temp
Walk through the swap routine with a few values to see how it
works. For example, if Array[K] = 6 and Array[K +1] =
18 at first, after the swap, Array[K] will contain the value 18
and Array[K+1] will contain the value 6. What will be the
value in Temp?
5-12
5.3 Other Uses of Arrays
• String Processing:
– Some languages do not contain string data types, but
implement strings as an array of character data type.
– Declare the data type of an array in your program:
• Declare Profit[50] Of Integers
• Declare Month[10] Of Characters
– Array size is the number of elements in an array.
– String length is the number of characters of a particular
element of a character array.
5-13
String Length and the Length Function
• The length of a string is the number of characters it contains.
• The Length function returns the number of characters in a
given string.
• For example:
Declare Str[10] Of Characters
Set Str = “Hello!”
Write Length(Str)
• The output will be 6 because the string “Hello!” is made up of 6
characters.
5-14
5.4 Two-Dimensional Arrays
• In a two-dimensional array, the value of an element depends on
two factors instead of just one.
• Here is how to declare a two-dimensional array:
Declare Profit[12, 24] Of Integers
• A two-dimensional array is like a matrix or an electronic
spreadsheet.
– Two factors can be, for example, Row and Column or Test Number and
Student.
– In the example in the text, Boynton’s (Student #2) scores can be
referenced as: Score[1,0],Score[1,1], Score[1,2],
Score[1,3], Score[1,4].
– Notice the ‘1’ subscript for Boynton remains constant, while the
subscript for Test changes.
5-15
Using Two-Dimensional Arrays
• Use with nested loops to read in multiple values, multiple times.
• In the text example, for each student input, we load 5 tests
scores.
• This allows us to process multiple tests for multiple students.
• Can use in parallel array processing
• Some languages allow for processing arrays of more than two
dimensions. These are called higher-dimensional arrays.
5-16
Nested Loops Help Load Arrays
To input data into a two-dimensional array, use a nested
loop.
A two-dimensional array named Working contains the
names of 20 work-study students in one department
and the hours worked in each of the 15 weeks of the
semester. The array is declared as follows (below). It
is assumed that the students are identified by number
(1 through 20). The pseudocode to load this array is
shown on the next slide.
Declare Working[20, 15]
5-17
Nested Loops Help Load Arrays
(continued)
Declare Working[20, 15]
For Student = 0 Step 1 to 19
Write “Enter hours worked each week”
Write “for student # “ , Student + 1
For Week = 0 Step 1 to 14
Input Working[Student, Week]
End For(Week)
End For(Student)
5-18
Pseudocode Language (Ch 5)
Declare a one-dimensional array:
Declare ThisArray[N] Of Integers
Declare ThatArray[N] Of Reals
Declare StringArray[N] Of Characters
Declare a two-dimensional array:
Declare ThisArray[X, Y] declares an array with X rows and Y
columns
5-19
Descargar

Chapter 5