SlideShare a Scribd company logo
CSWD1001
Programming Methods
Array
18/9/2018
CSWD1001 @ Kwan Lee First City Unversity Malaysia
(FCUC)
1
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
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
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
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
18/9/2018
CSWD1001 @ Kwan Lee First City Unversity Malaysia
(FCUC)
6
Declaration of Array
Constant integer size = n number
Declare integer units[size]
18/9/2018
CSWD1001 @ Kwan Lee First City Unversity Malaysia
(FCUC)
7
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
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
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
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
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
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
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
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
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
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
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
Watch Out for off-by-one Error
18/9/2018
CSWD1001 @ Kwan Lee First City Unversity Malaysia
(FCUC)
19
• 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
// 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
Partially Filled Arrays
18/9/2018
CSWD1001 @ Kwan Lee First City Unversity Malaysia
(FCUC)
22
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
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
Two-Dimensional (2D) Array
18/9/2018
CSWD1001 @ Kwan Lee First City Unversity Malaysia
(FCUC)
25
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
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
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
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
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
• 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
• 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
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
18/9/2018
CSWD1001 @ Kwan Lee First City Unversity Malaysia
(FCUC)
34
Arrays Of Three or More Dimensions
18/9/2018
CSWD1001 @ Kwan Lee First City Unversity Malaysia
(FCUC)
35
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
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
Concept Of A Three-dimensional Array As
“Pages” Of 2D Arrays.
18/9/2018
CSWD1001 @ Kwan Lee First City Unversity Malaysia
(FCUC)
38
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

More Related Content

What's hot

Recursion
RecursionRecursion
Recursion
Nalin Adhikari
 
Linked List
Linked ListLinked List
Linked List
Ashim Lamichhane
 
Algorithm and psuedocode
Algorithm and psuedocodeAlgorithm and psuedocode
Algorithm and psuedocode
Mustafa Qureshi
 
13. Pointer and 2D array
13. Pointer  and  2D array13. Pointer  and  2D array
13. Pointer and 2D array
Gem WeBlog
 
Error detection recovery
Error detection recoveryError detection recovery
Error detection recoveryTech_MX
 
Algorithms
AlgorithmsAlgorithms
Algorithms
Liam Dunphy
 
What is c
What is cWhat is c
What is c
Nitesh Saitwal
 
Services provided by os
Services provided by osServices provided by os
Services provided by osSumant Diwakar
 
Python tuple
Python   tuplePython   tuple
Python tuple
Mohammed Sikander
 
Linked List, Types of Linked LIst, Various Operations, Applications of Linked...
Linked List, Types of Linked LIst, Various Operations, Applications of Linked...Linked List, Types of Linked LIst, Various Operations, Applications of Linked...
Linked List, Types of Linked LIst, Various Operations, Applications of Linked...
Balwant Gorad
 
Error detection and correction
Error detection and correctionError detection and correction
Error detection and correction
Abdul Razaq
 
Loops in c language
Loops in c languageLoops in c language
Loops in c language
Tanmay Modi
 
Programming : QBASIC
Programming : QBASICProgramming : QBASIC
Programming : QBASIC
vnuvalcrepollo
 
Unit 1-problem solving with algorithm
Unit 1-problem solving with algorithmUnit 1-problem solving with algorithm
Unit 1-problem solving with algorithm
rajkumar1631010038
 
Python Exception Handling
Python Exception HandlingPython Exception Handling
Python Exception Handling
Megha V
 
Pseudo code of stack Queue and Array
Pseudo code of stack Queue and ArrayPseudo code of stack Queue and Array
Pseudo code of stack Queue and Array
rdp rehmatullah
 
Difference between stack and queue
Difference between stack and queueDifference between stack and queue
Difference between stack and queue
Pulkitmodi1998
 
Error Detection And Correction
Error Detection And CorrectionError Detection And Correction
Error Detection And Correction
Renu Kewalramani
 
ER to Relational Mapping
ER to Relational MappingER to Relational Mapping

What's hot (20)

Recursion
RecursionRecursion
Recursion
 
Linked List
Linked ListLinked List
Linked List
 
Algorithm and psuedocode
Algorithm and psuedocodeAlgorithm and psuedocode
Algorithm and psuedocode
 
13. Pointer and 2D array
13. Pointer  and  2D array13. Pointer  and  2D array
13. Pointer and 2D array
 
Error detection recovery
Error detection recoveryError detection recovery
Error detection recovery
 
Algorithms
AlgorithmsAlgorithms
Algorithms
 
binary tree
binary treebinary tree
binary tree
 
What is c
What is cWhat is c
What is c
 
Services provided by os
Services provided by osServices provided by os
Services provided by os
 
Python tuple
Python   tuplePython   tuple
Python tuple
 
Linked List, Types of Linked LIst, Various Operations, Applications of Linked...
Linked List, Types of Linked LIst, Various Operations, Applications of Linked...Linked List, Types of Linked LIst, Various Operations, Applications of Linked...
Linked List, Types of Linked LIst, Various Operations, Applications of Linked...
 
Error detection and correction
Error detection and correctionError detection and correction
Error detection and correction
 
Loops in c language
Loops in c languageLoops in c language
Loops in c language
 
Programming : QBASIC
Programming : QBASICProgramming : QBASIC
Programming : QBASIC
 
Unit 1-problem solving with algorithm
Unit 1-problem solving with algorithmUnit 1-problem solving with algorithm
Unit 1-problem solving with algorithm
 
Python Exception Handling
Python Exception HandlingPython Exception Handling
Python Exception Handling
 
Pseudo code of stack Queue and Array
Pseudo code of stack Queue and ArrayPseudo code of stack Queue and Array
Pseudo code of stack Queue and Array
 
Difference between stack and queue
Difference between stack and queueDifference between stack and queue
Difference between stack and queue
 
Error Detection And Correction
Error Detection And CorrectionError Detection And Correction
Error Detection And Correction
 
ER to Relational Mapping
ER to Relational MappingER to Relational Mapping
ER to Relational Mapping
 

Similar to 8 Array

2 Program Design Methodology
2 Program Design Methodology2 Program Design Methodology
2 Program Design Methodology
Kwan Lee
 
4 Decision Structures and Boolean Logic
4 Decision Structures and Boolean Logic4 Decision Structures and Boolean Logic
4 Decision Structures and Boolean Logic
Kwan Lee
 
7 Input Validation
7 Input Validation7 Input Validation
7 Input Validation
Kwan Lee
 
6 Function
6 Function6 Function
6 Function
Kwan Lee
 
5 Repetition Structures
5 Repetition Structures5 Repetition Structures
5 Repetition Structures
Kwan Lee
 
Monte Carlo Simulation for project estimates v1.0
Monte Carlo Simulation for project estimates v1.0Monte Carlo Simulation for project estimates v1.0
Monte Carlo Simulation for project estimates v1.0
PMILebanonChapter
 
algo 1.ppt
algo 1.pptalgo 1.ppt
algo 1.ppt
example43
 
UOPOPS571 Lessons in Excellence--uopops571.com
UOPOPS571 Lessons in Excellence--uopops571.comUOPOPS571 Lessons in Excellence--uopops571.com
UOPOPS571 Lessons in Excellence--uopops571.com
thomashard90
 
Chapter06.ppt/Management Accounting-Activity Based Accounting
Chapter06.ppt/Management Accounting-Activity Based AccountingChapter06.ppt/Management Accounting-Activity Based Accounting
Chapter06.ppt/Management Accounting-Activity Based Accounting
SrikantKapoor1
 
OPS 571 HELP Education for Service--ops571help.com
 OPS 571 HELP Education for Service--ops571help.com OPS 571 HELP Education for Service--ops571help.com
OPS 571 HELP Education for Service--ops571help.com
mamata44
 
3. Capacity 22 HR.ppt
3. Capacity 22 HR.ppt3. Capacity 22 HR.ppt
3. Capacity 22 HR.ppt
MehulDatta1
 
Which of the following is a focusing step
Which of the following is a focusing stepWhich of the following is a focusing step
Which of the following is a focusing step
johann11373
 
OPS 571 HELP Lessons in Excellence / ops571help.com
OPS 571 HELP Lessons in Excellence / ops571help.comOPS 571 HELP Lessons in Excellence / ops571help.com
OPS 571 HELP Lessons in Excellence / ops571help.com
kopiko46
 
OPS 571 HELP Education Counseling--ops571help.com
OPS 571 HELP Education Counseling--ops571help.comOPS 571 HELP Education Counseling--ops571help.com
OPS 571 HELP Education Counseling--ops571help.com
venkat60041
 
OPS 571 GENIUS Inspiring Innovation--ops571genius.com
OPS 571 GENIUS Inspiring Innovation--ops571genius.comOPS 571 GENIUS Inspiring Innovation--ops571genius.com
OPS 571 GENIUS Inspiring Innovation--ops571genius.com
kopiko111
 
OPS 571 GENIUS Education Counseling--ops571genius.com
OPS 571 GENIUS Education Counseling--ops571genius.comOPS 571 GENIUS Education Counseling--ops571genius.com
OPS 571 GENIUS Education Counseling--ops571genius.com
venkat60040
 
In hau lee's uncertainty framework to classify supply chains
In hau lee's uncertainty framework to classify supply chainsIn hau lee's uncertainty framework to classify supply chains
In hau lee's uncertainty framework to classify supply chains
johann11371
 
UOP OPS 571 Inspiring Innovation--uopops571.com
UOP OPS 571 Inspiring Innovation--uopops571.comUOP OPS 571 Inspiring Innovation--uopops571.com
UOP OPS 571 Inspiring Innovation--uopops571.com
kopiko118
 
3 Modules
3 Modules3 Modules
3 Modules
Kwan Lee
 
If the actual output of a piece of equipment
If the actual output of a piece of equipmentIf the actual output of a piece of equipment
If the actual output of a piece of equipment
johann11371
 

Similar to 8 Array (20)

2 Program Design Methodology
2 Program Design Methodology2 Program Design Methodology
2 Program Design Methodology
 
4 Decision Structures and Boolean Logic
4 Decision Structures and Boolean Logic4 Decision Structures and Boolean Logic
4 Decision Structures and Boolean Logic
 
7 Input Validation
7 Input Validation7 Input Validation
7 Input Validation
 
6 Function
6 Function6 Function
6 Function
 
5 Repetition Structures
5 Repetition Structures5 Repetition Structures
5 Repetition Structures
 
Monte Carlo Simulation for project estimates v1.0
Monte Carlo Simulation for project estimates v1.0Monte Carlo Simulation for project estimates v1.0
Monte Carlo Simulation for project estimates v1.0
 
algo 1.ppt
algo 1.pptalgo 1.ppt
algo 1.ppt
 
UOPOPS571 Lessons in Excellence--uopops571.com
UOPOPS571 Lessons in Excellence--uopops571.comUOPOPS571 Lessons in Excellence--uopops571.com
UOPOPS571 Lessons in Excellence--uopops571.com
 
Chapter06.ppt/Management Accounting-Activity Based Accounting
Chapter06.ppt/Management Accounting-Activity Based AccountingChapter06.ppt/Management Accounting-Activity Based Accounting
Chapter06.ppt/Management Accounting-Activity Based Accounting
 
OPS 571 HELP Education for Service--ops571help.com
 OPS 571 HELP Education for Service--ops571help.com OPS 571 HELP Education for Service--ops571help.com
OPS 571 HELP Education for Service--ops571help.com
 
3. Capacity 22 HR.ppt
3. Capacity 22 HR.ppt3. Capacity 22 HR.ppt
3. Capacity 22 HR.ppt
 
Which of the following is a focusing step
Which of the following is a focusing stepWhich of the following is a focusing step
Which of the following is a focusing step
 
OPS 571 HELP Lessons in Excellence / ops571help.com
OPS 571 HELP Lessons in Excellence / ops571help.comOPS 571 HELP Lessons in Excellence / ops571help.com
OPS 571 HELP Lessons in Excellence / ops571help.com
 
OPS 571 HELP Education Counseling--ops571help.com
OPS 571 HELP Education Counseling--ops571help.comOPS 571 HELP Education Counseling--ops571help.com
OPS 571 HELP Education Counseling--ops571help.com
 
OPS 571 GENIUS Inspiring Innovation--ops571genius.com
OPS 571 GENIUS Inspiring Innovation--ops571genius.comOPS 571 GENIUS Inspiring Innovation--ops571genius.com
OPS 571 GENIUS Inspiring Innovation--ops571genius.com
 
OPS 571 GENIUS Education Counseling--ops571genius.com
OPS 571 GENIUS Education Counseling--ops571genius.comOPS 571 GENIUS Education Counseling--ops571genius.com
OPS 571 GENIUS Education Counseling--ops571genius.com
 
In hau lee's uncertainty framework to classify supply chains
In hau lee's uncertainty framework to classify supply chainsIn hau lee's uncertainty framework to classify supply chains
In hau lee's uncertainty framework to classify supply chains
 
UOP OPS 571 Inspiring Innovation--uopops571.com
UOP OPS 571 Inspiring Innovation--uopops571.comUOP OPS 571 Inspiring Innovation--uopops571.com
UOP OPS 571 Inspiring Innovation--uopops571.com
 
3 Modules
3 Modules3 Modules
3 Modules
 
If the actual output of a piece of equipment
If the actual output of a piece of equipmentIf the actual output of a piece of equipment
If the actual output of a piece of equipment
 

Recently uploaded

Lapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdfLapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdf
Jean Carlos Nunes Paixão
 
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
siemaillard
 
A Survey of Techniques for Maximizing LLM Performance.pptx
A Survey of Techniques for Maximizing LLM Performance.pptxA Survey of Techniques for Maximizing LLM Performance.pptx
A Survey of Techniques for Maximizing LLM Performance.pptx
thanhdowork
 
CACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdfCACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdf
camakaiclarkmusic
 
Marketing internship report file for MBA
Marketing internship report file for MBAMarketing internship report file for MBA
Marketing internship report file for MBA
gb193092
 
The basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptxThe basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptx
heathfieldcps1
 
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
Nguyen Thanh Tu Collection
 
The Accursed House by Émile Gaboriau.pptx
The Accursed House by Émile Gaboriau.pptxThe Accursed House by Émile Gaboriau.pptx
The Accursed House by Émile Gaboriau.pptx
DhatriParmar
 
Chapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptxChapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptx
Mohd Adib Abd Muin, Senior Lecturer at Universiti Utara Malaysia
 
Multithreading_in_C++ - std::thread, race condition
Multithreading_in_C++ - std::thread, race conditionMultithreading_in_C++ - std::thread, race condition
Multithreading_in_C++ - std::thread, race condition
Mohammed Sikander
 
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
MysoreMuleSoftMeetup
 
STRAND 3 HYGIENIC PRACTICES.pptx GRADE 7 CBC
STRAND 3 HYGIENIC PRACTICES.pptx GRADE 7 CBCSTRAND 3 HYGIENIC PRACTICES.pptx GRADE 7 CBC
STRAND 3 HYGIENIC PRACTICES.pptx GRADE 7 CBC
kimdan468
 
Guidance_and_Counselling.pdf B.Ed. 4th Semester
Guidance_and_Counselling.pdf B.Ed. 4th SemesterGuidance_and_Counselling.pdf B.Ed. 4th Semester
Guidance_and_Counselling.pdf B.Ed. 4th Semester
Atul Kumar Singh
 
special B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdfspecial B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdf
Special education needs
 
Thesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.pptThesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.ppt
EverAndrsGuerraGuerr
 
Home assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdfHome assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdf
Tamralipta Mahavidyalaya
 
Honest Reviews of Tim Han LMA Course Program.pptx
Honest Reviews of Tim Han LMA Course Program.pptxHonest Reviews of Tim Han LMA Course Program.pptx
Honest Reviews of Tim Han LMA Course Program.pptx
timhan337
 
1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx
JosvitaDsouza2
 
Model Attribute Check Company Auto Property
Model Attribute  Check Company Auto PropertyModel Attribute  Check Company Auto Property
Model Attribute Check Company Auto Property
Celine George
 
Chapter -12, Antibiotics (One Page Notes).pdf
Chapter -12, Antibiotics (One Page Notes).pdfChapter -12, Antibiotics (One Page Notes).pdf
Chapter -12, Antibiotics (One Page Notes).pdf
Kartik Tiwari
 

Recently uploaded (20)

Lapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdfLapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdf
 
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
 
A Survey of Techniques for Maximizing LLM Performance.pptx
A Survey of Techniques for Maximizing LLM Performance.pptxA Survey of Techniques for Maximizing LLM Performance.pptx
A Survey of Techniques for Maximizing LLM Performance.pptx
 
CACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdfCACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdf
 
Marketing internship report file for MBA
Marketing internship report file for MBAMarketing internship report file for MBA
Marketing internship report file for MBA
 
The basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptxThe basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptx
 
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
 
The Accursed House by Émile Gaboriau.pptx
The Accursed House by Émile Gaboriau.pptxThe Accursed House by Émile Gaboriau.pptx
The Accursed House by Émile Gaboriau.pptx
 
Chapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptxChapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptx
 
Multithreading_in_C++ - std::thread, race condition
Multithreading_in_C++ - std::thread, race conditionMultithreading_in_C++ - std::thread, race condition
Multithreading_in_C++ - std::thread, race condition
 
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
 
STRAND 3 HYGIENIC PRACTICES.pptx GRADE 7 CBC
STRAND 3 HYGIENIC PRACTICES.pptx GRADE 7 CBCSTRAND 3 HYGIENIC PRACTICES.pptx GRADE 7 CBC
STRAND 3 HYGIENIC PRACTICES.pptx GRADE 7 CBC
 
Guidance_and_Counselling.pdf B.Ed. 4th Semester
Guidance_and_Counselling.pdf B.Ed. 4th SemesterGuidance_and_Counselling.pdf B.Ed. 4th Semester
Guidance_and_Counselling.pdf B.Ed. 4th Semester
 
special B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdfspecial B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdf
 
Thesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.pptThesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.ppt
 
Home assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdfHome assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdf
 
Honest Reviews of Tim Han LMA Course Program.pptx
Honest Reviews of Tim Han LMA Course Program.pptxHonest Reviews of Tim Han LMA Course Program.pptx
Honest Reviews of Tim Han LMA Course Program.pptx
 
1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx
 
Model Attribute Check Company Auto Property
Model Attribute  Check Company Auto PropertyModel Attribute  Check Company Auto Property
Model Attribute Check Company Auto Property
 
Chapter -12, Antibiotics (One Page Notes).pdf
Chapter -12, Antibiotics (One Page Notes).pdfChapter -12, Antibiotics (One Page Notes).pdf
Chapter -12, Antibiotics (One Page Notes).pdf
 

8 Array

  • 1. CSWD1001 Programming Methods Array 18/9/2018 CSWD1001 @ Kwan Lee First City Unversity Malaysia (FCUC) 1
  • 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
  • 6. 18/9/2018 CSWD1001 @ Kwan Lee First City Unversity Malaysia (FCUC) 6
  • 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
  • 22. Partially Filled Arrays 18/9/2018 CSWD1001 @ Kwan Lee First City Unversity Malaysia (FCUC) 22
  • 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
  • 25. Two-Dimensional (2D) Array 18/9/2018 CSWD1001 @ Kwan Lee First City Unversity Malaysia (FCUC) 25
  • 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
  • 34. 18/9/2018 CSWD1001 @ Kwan Lee First City Unversity Malaysia (FCUC) 34
  • 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

Editor's Notes

  1. To make array sizes easier to maintain, many programmers prefer to use named constants as array size declarator
  2. 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.