2. What we’ll learn
• Array basics
• Processing the contents of an array
• Two-dimensional arrays
• Arrays of three or more dimensions
18/9/2018
CSWD1001 @ Kwan Lee First City Unversity Malaysia
(FCUC)
2
3. A single variable only able to hold one value.
Imagine you have a group of items
How you hold the data ??
18/9/2018
CSWD1001 @ Kwan Lee First City Unversity Malaysia
(FCUC)
3
4. Use an Array
• An array allows you to store a group of items of the same
data type together in memory
18/9/2018
CSWD1001 @ Kwan Lee First City Unversity Malaysia
(FCUC)
4
5. Characteristics of an Array
• The storage locations in an array are known as elements
• In memory, an array’s elements are usually located in consecutive
memory locations
• Each element in an array is assigned a unique number known as a
subscript/indexes
• Subscript are used to identify elements in an array
• The first element is assigned the subscript 0, the second element is
assigned the subscript 1
18/9/2018
CSWD1001 @ Kwan Lee First City Unversity Malaysia
(FCUC)
5
7. Declaration of Array
Constant integer size = n number
Declare integer units[size]
18/9/2018
CSWD1001 @ Kwan Lee First City Unversity Malaysia
(FCUC)
7
8. Example of Declaration of an Array
Constant integer size = 5
Declare integer units[size]
18/9/2018
CSWD1001 @ Kwan Lee First City Unversity Malaysia
(FCUC)
8
9. Assigning Values To Array Elements
• You access the individual elements in an array by using their
subscripts.
• Example, below pseudocode assigns the value 20 to element
0, the value 30 to element 1, and so forth.
18/9/2018
CSWD1001 @ Kwan Lee First City Unversity Malaysia
(FCUC)
9
10. Inputting And Outputting Array Contents
• You can read values from the keyboard and store them in an
array element just as you can a regular variable
• You can also output the contents of an array element
18/9/2018
CSWD1001 @ Kwan Lee First City Unversity Malaysia
(FCUC)
10
11. Example Inputting and Outputting Array
Contents
// Create a constant for the number of employees.
Constant Integer SIZE = 3
// Declare an array to hold the number of hours
worked by each employee.
Declare Integer hours[SIZE]
// Get the hours worked by employee 1.
Display "Enter the hours worked by employee 1."
Input hours[0]
// Get the hours worked by employee 2.
Display "Enter the hours worked by employee 2."
Input hours[1]
// Get the hours worked by employee 3.
Display "Enter the hours worked by employee 3."
Input hours[2]
// Display the values entered.
Display "The hours you entered are:"
Display hours[0]
Display hours[1]
Display hours[2]
18/9/2018
CSWD1001 @ Kwan Lee First City Unversity Malaysia
(FCUC)
11
12. Using A Loop To Step Through An Array
• Most programming languages allow you to store a number in
a variable and then use that variable as a subscript.
• This makes it possible to use a loop to step through an entire
array, performing the same operation on each element
18/9/2018
CSWD1001 @ Kwan Lee First City Unversity Malaysia
(FCUC)
12
13. Example Using A Loop To Step Through An
Array
// Create a constant for the size of the array.
Constant Integer SIZE = 3
// Declare an array to hold the number of
hours
// worked by each employee.
Declare Integer hours[SIZE]
// Declare a variable to use in the loops.
Declare Integer index
// Get the hours for each employee.
For index = 0 To SIZE - 1
Display "Enter the hours worked by"
Display "employee number ", index + 1
Input hours[index]
End For
// Display the values entered.
Display "The hours you entered are:"
For index = 0 To SIZE - 1
Display hours[index]
End For
18/9/2018
CSWD1001 @ Kwan Lee First City Unversity Malaysia
(FCUC)
13
14. Processing The Elements Of An Array
• Processing array elements is no different than processing
other variables.
18/9/2018
CSWD1001 @ Kwan Lee First City Unversity Malaysia
(FCUC)
14
15. Example Use Case Processing The Elements
Of An Array
• Megan owns a small neighborhood coffee shop, and she has six
employees who work as baristas (coffee bartenders). All of the
employees have the same hourly pay rate.
• Megan has asked you to design a program that will allow her to enter
the number of hours worked by each employee and then display the
amounts of all the employees’ gross pay. You determine that the
program should perform the following steps:
1. For each employee: get the number of hours worked and store it in an array
element.
2. For each array element: use the value stored in the element to calculate an
employee’s gross pay. Display the amount of the gross pay.
18/9/2018
CSWD1001 @ Kwan Lee First City Unversity Malaysia
(FCUC)
15
16. Pseudocode
// Constant for the size of the array.
Constant Integer SIZE = 6
// Array to hold each employee's hours.
Declare Real hours[SIZE]
// Variable to hold the hourly pay rate.
Declare Real payRate
// Variable to hold a gross pay amount.
Declare Real grossPay
// Variable to use as a loop counter.
Declare Integer index
// Get each employee's hours worked.
For index = 0 To SIZE - 1
Display "Enter the hours worked by
employee ", index + 1
Input hours[index]
End For
// Get the hourly pay rate.
Display "Enter the hourly pay rate."
Input payRate
// Display each employee's gross pay.
Display "Here is each employee's gross
pay.“
For index = 0 To SIZE - 1
Set grossPay = hours[index] *
payRate
Display "Employee ", index + 1, ": $",
currencyFormat(grossPay)
End For
18/9/2018
CSWD1001 @ Kwan Lee First City Unversity Malaysia
(FCUC)
16
17. Flowchart
Start
Constant Integer SIZE = 6
Declare
Real hours[SIZE],
payRate, grossPa,
Integer index
Set index = 0
Index <=
size - 1
Display “Enter the
hours worked by
employee”, index + 1
Display “Enter the
hourly pay rate”
Input payRate
A
Input hours[index]
Set index = index + 1
18/9/2018
CSWD1001 @ Kwan Lee First City Unversity Malaysia
(FCUC)
17
18. A
Set index = 0
Index <=
size - 1
End
Set grossPay = hours[index] *
payRate
Display “Employee”m index
+ 1, “:”,
currencyFormat(grossPay)
Set index = index + 1
18/9/2018
CSWD1001 @ Kwan Lee First City Unversity Malaysia
(FCUC)
18
19. Watch Out for off-by-one Error
18/9/2018
CSWD1001 @ Kwan Lee First City Unversity Malaysia
(FCUC)
19
20. • Because array subscripts start at 0 rather than 1, have to be
careful not to perform an off-by-one error
• An off-by-error occurs when a loop iterates one time too
many or one time too few
18/9/2018
CSWD1001 @ Kwan Lee First City Unversity Malaysia
(FCUC)
20
21. // This code has an off-by-one error.
Constant Integer SIZE = 100;
Declare Integer numbers[SIZE]
Declare Integer index
For index = 1 To SIZE - 1
Set numbers[index] = 0
End For
18/9/2018
CSWD1001 @ Kwan Lee First City Unversity Malaysia
(FCUC)
21
23. Concept
• Sometimes we do not know the number of items in the
serious of items in an array
• As a result, we do not know the exact number of elements
needed for the array
• One solution is to make the array large enough to hold the
largest possible number of items.
18/9/2018
CSWD1001 @ Kwan Lee First City Unversity Malaysia
(FCUC)
23
24. Example Partially Filled Arrays
// Declare a constant for the array size.
Constant Integer SIZE = 100
// Declare an array to hold integer values.
Declare Integer values[SIZE]
// Declare an Integer variable to hold the number
of items
// that are actually stored in the array.
Declare Integer count = 0
// Declare an Integer variable to hold the user's
input.
Declare Integer number
// Declare a variable to step through the array.
Declare Integer index
// Prompt the user to enter a number. If the user
enters the sentinel value -1 we will stop accepting
input.
Display "Enter a number or -1 to quit."
Input number
// If the input is not -1 and the array is not
// full, process the input.
While (number != -1 AND count < SIZE)
// Store the input in the array.
Set values[count] = number
// Increment count.
count = count + 1
// Prompt the user for the next number.
Display "Enter a number or -1 to quit."
Input number
End While
// Display the values stored in the array.
Display "Here are the numbers you entered:"
For index = 0 To count - 1
Display values[index]
End For
18/9/2018
CSWD1001 @ Kwan Lee First City Unversity Malaysia
(FCUC)
24
26. Characteristics of 2D Array
• A two-dimensional array is like several identical arrays put
together.
• Useful for storing multiple sets of data
18/9/2018
CSWD1001 @ Kwan Lee First City Unversity Malaysia
(FCUC)
26
27. Example 2D Array Concept
• Suppose you are designing a grade-averaging program for a teacher
• The teacher has six students, and each student takes give exams during
the semester
18/9/2018
CSWD1001 @ Kwan Lee First City Unversity Malaysia
(FCUC)
27
28. Declaration of 2D Array
Constant Integer ROWS = n number
Constant Integer COLS = n number
Declare Integer values[ROWS][COLS]
18/9/2018
CSWD1001 @ Kwan Lee First City Unversity Malaysia
(FCUC)
28
29. Accessing The Elements In A 2D Array
• To access one of the elements in a two-dimensional array, you
must use both subscript
• Programs that process two-dimensional arrays commonly do
so with nested loops
18/9/2018
CSWD1001 @ Kwan Lee First City Unversity Malaysia
(FCUC)
29
30. Example Use Case 2D Array
• Unique Candy Inc. has three divisions: division 1 (East Coast), division 2 (Midwest), and division
3 (West Coast). The sales manager has asked you to design a program that will read as input each
division’s sales for each quarter of the year, and then display the total sales for all divisions
• This program requires you to process three sets of data:
• The sales amounts for division 1
• The sales amounts for division 2
• The sales amounts for division 3
• Each of these sets of data contains four items:
• The sales for quarter 1
• The sales for quarter 2
• The sales for quarter 3
• The sales for quarter 4
18/9/2018
CSWD1001 @ Kwan Lee First City Unversity Malaysia
(FCUC)
30
31. • You decide to store the sales amounts in a two-dimensional array. The
array will have three rows (one for each division) and four columns
(one for each quarter).
• Figure below shows how the sales data will be organized in the array.
18/9/2018
CSWD1001 @ Kwan Lee First City Unversity Malaysia
(FCUC)
31
32. • The program will use a pair of nested loops to read the sales amounts.
It will then use a pair of nested loops to add all of the array elements to
an accumulator variable. Here is an overview of the algorithm:
1. For each division:
For each quarter:
Read the amount of sales for the quarter and store it in the array.
2. For each row in the array:
For each column in the array:
Add the amount in the column to an accumulator.
3. Display the amount in the accumulator.
18/9/2018
CSWD1001 @ Kwan Lee First City Unversity Malaysia
(FCUC)
32
33. Pseudocode
// Constants for the array sizes.
Constant Integer ROWS = 3
Constant Integer COLS = 4
// An array to hold company sales.
Declare Real sales[ROWS][COLS]
// Counter variables
Declare Integer row, col
// Accumulator
Declare Real total = 0
// Display instructions.
Display "This program calculates the company's"
Display "total sales. Enter the quarterly sales"
Display "amounts for each division when prompted."
// Nested loops to fill the array with quarterly
// sales amounts for each division.
For row = 0 To ROWS - 1
For col = 0 To COLS - 1
Display "Division ", row + 1, " quarter
", col + 1
Input sales[row][col]
End For
// Display a blank line.
Display
End For
// Nested loops to add all of the array elements.
For row = 0 To ROWS - 1
For col = 0 To COLS - 1
Set total = total + sales[row][col]
End For
End For
// Display the total sales.
Display "The total company sales are: $",
currencyFormat(total)
18/9/2018
CSWD1001 @ Kwan Lee First City Unversity Malaysia
(FCUC)
33
35. Arrays Of Three or More Dimensions
18/9/2018
CSWD1001 @ Kwan Lee First City Unversity Malaysia
(FCUC)
35
36. Concepts
• To model data that occurs in multiple sets, most languages
allow you to create arrays with multiple dimensions
18/9/2018
CSWD1001 @ Kwan Lee First City Unversity Malaysia
(FCUC)
36
37. Declaration of 3D Array
Constant Integer ROWS = n number
Constant Integer COLS = n number
Constant Integer PAGE= n number
Declare Integer values[ROWS][COLS][PAGE]
18/9/2018
CSWD1001 @ Kwan Lee First City Unversity Malaysia
(FCUC)
37
38. Concept Of A Three-dimensional Array As
“Pages” Of 2D Arrays.
18/9/2018
CSWD1001 @ Kwan Lee First City Unversity Malaysia
(FCUC)
38
39. Summary
• An array allows you to store a group of items of the same data type
together in memory
• You access the individual elements in an array by using their subscripts.
• You can read values from the keyboard and store them in an array element
just as you can a regular variable
• You can also output the contents of an array element
• Processing array elements is no different than processing other variables.
• An off-by-error occurs when a loop iterates one time too many or one time
too few
• A two-dimensional array is like several identical arrays put together.
• To model data that occurs in multiple sets, most languages allow you to
create arrays with multiple dimensions
18/9/2018
CSWD1001 @ Kwan Lee First City Unversity Malaysia
(FCUC)
39
Notes de l'éditeur
To make array sizes easier to maintain, many programmers prefer to use named constants as array size declarator
The intent of this pseudocode is to create an array of integers with 100 elements, and
store the value 0 in each element. However, this code has an off-by-one error. The
loop uses its counter variable, index, as a subscript with the numbers array. During
the loop’s execution, the index variable takes on the values 1 through 99, when it
should take on the values 0 through 99. As a result, the first element, which is at subscript
0, is skipped.