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

PPTX
Data structure array
PPTX
C programing -Structure
PDF
Symbol table in compiler Design
PPTX
html-table
PDF
PPT_X_IT-402_Electronic Spreadsheet.pptx.pdf
PPT
pseudo code basics
PPTX
Recovery & Atom city & Log based Recovery
PPT
Code Optimization
Data structure array
C programing -Structure
Symbol table in compiler Design
html-table
PPT_X_IT-402_Electronic Spreadsheet.pptx.pdf
pseudo code basics
Recovery & Atom city & Log based Recovery
Code Optimization

What's hot (20)

PPT
358 33 powerpoint-slides_14-sorting_chapter-14
PDF
Syntax directed translation
PPTX
Page maker - working with text
PPT
constants, variables and datatypes in C
PPTX
Key and its different types
PDF
DAA Notes.pdf
PPTX
Symbol Table
PPTX
Python comments and variables.pptx
PPTX
Data Structure and Algorithms The Tower of Hanoi
PPTX
Object oriented database concepts
PPTX
Pseudocode
PPTX
STRUCTURE OF SQL QUERIES
PPT
MAIL MERGE.ppt
PPTX
Database Languages.pptx
PPT
Array in c
PPT
Data Structure and Algorithms Binary Tree
PPTX
ACID Property in DBMS
PPTX
Relational algebra ppt
PPTX
Java script errors &amp; exceptions handling
PPTX
Typedef
358 33 powerpoint-slides_14-sorting_chapter-14
Syntax directed translation
Page maker - working with text
constants, variables and datatypes in C
Key and its different types
DAA Notes.pdf
Symbol Table
Python comments and variables.pptx
Data Structure and Algorithms The Tower of Hanoi
Object oriented database concepts
Pseudocode
STRUCTURE OF SQL QUERIES
MAIL MERGE.ppt
Database Languages.pptx
Array in c
Data Structure and Algorithms Binary Tree
ACID Property in DBMS
Relational algebra ppt
Java script errors &amp; exceptions handling
Typedef
Ad

Similar to 8 Array (20)

PPTX
2 Program Design Methodology
PPTX
4 Decision Structures and Boolean Logic
PPTX
7 Input Validation
PPTX
6 Function
PPTX
5 Repetition Structures
PDF
Monte Carlo Simulation for project estimates v1.0
PPTX
inbound4289876492495941544.pptxbhhhhjjjj
PPT
algo 1.ppt
PDF
UOPOPS571 Lessons in Excellence--uopops571.com
PPT
Chapter06.ppt/Management Accounting-Activity Based Accounting
DOC
OPS 571 HELP Education for Service--ops571help.com
PPT
3. Capacity 22 HR.ppt
DOC
Which of the following is a focusing step
DOCX
OPS 571 HELP Lessons in Excellence / ops571help.com
PDF
OPS 571 HELP Education Counseling--ops571help.com
PDF
OPS 571 GENIUS Education Counseling--ops571genius.com
DOCX
OPS 571 GENIUS Inspiring Innovation--ops571genius.com
DOC
In hau lee's uncertainty framework to classify supply chains
DOCX
UOP OPS 571 Inspiring Innovation--uopops571.com
PPTX
3 Modules
2 Program Design Methodology
4 Decision Structures and Boolean Logic
7 Input Validation
6 Function
5 Repetition Structures
Monte Carlo Simulation for project estimates v1.0
inbound4289876492495941544.pptxbhhhhjjjj
algo 1.ppt
UOPOPS571 Lessons in Excellence--uopops571.com
Chapter06.ppt/Management Accounting-Activity Based Accounting
OPS 571 HELP Education for Service--ops571help.com
3. Capacity 22 HR.ppt
Which of the following is a focusing step
OPS 571 HELP Lessons in Excellence / ops571help.com
OPS 571 HELP Education Counseling--ops571help.com
OPS 571 GENIUS Education Counseling--ops571genius.com
OPS 571 GENIUS Inspiring Innovation--ops571genius.com
In hau lee's uncertainty framework to classify supply chains
UOP OPS 571 Inspiring Innovation--uopops571.com
3 Modules
Ad

Recently uploaded (20)

PPTX
Final Presentation General Medicine 03-08-2024.pptx
PPTX
master seminar digital applications in india
PDF
STATICS OF THE RIGID BODIES Hibbelers.pdf
PDF
Paper A Mock Exam 9_ Attempt review.pdf.
PPTX
Final Presentation General Medicine 03-08-2024.pptx
PDF
Yogi Goddess Pres Conference Studio Updates
PPTX
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
PDF
Anesthesia in Laparoscopic Surgery in India
PDF
Complications of Minimal Access Surgery at WLH
PDF
01-Introduction-to-Information-Management.pdf
PPTX
History, Philosophy and sociology of education (1).pptx
PDF
Updated Idioms and Phrasal Verbs in English subject
PDF
Classroom Observation Tools for Teachers
PDF
Microbial disease of the cardiovascular and lymphatic systems
PDF
LDMMIA Reiki Yoga Finals Review Spring Summer
PDF
Practical Manual AGRO-233 Principles and Practices of Natural Farming
PDF
LNK 2025 (2).pdf MWEHEHEHEHEHEHEHEHEHEHE
PDF
Weekly quiz Compilation Jan -July 25.pdf
PDF
A systematic review of self-coping strategies used by university students to ...
PDF
RTP_AR_KS1_Tutor's Guide_English [FOR REPRODUCTION].pdf
Final Presentation General Medicine 03-08-2024.pptx
master seminar digital applications in india
STATICS OF THE RIGID BODIES Hibbelers.pdf
Paper A Mock Exam 9_ Attempt review.pdf.
Final Presentation General Medicine 03-08-2024.pptx
Yogi Goddess Pres Conference Studio Updates
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
Anesthesia in Laparoscopic Surgery in India
Complications of Minimal Access Surgery at WLH
01-Introduction-to-Information-Management.pdf
History, Philosophy and sociology of education (1).pptx
Updated Idioms and Phrasal Verbs in English subject
Classroom Observation Tools for Teachers
Microbial disease of the cardiovascular and lymphatic systems
LDMMIA Reiki Yoga Finals Review Spring Summer
Practical Manual AGRO-233 Principles and Practices of Natural Farming
LNK 2025 (2).pdf MWEHEHEHEHEHEHEHEHEHEHE
Weekly quiz Compilation Jan -July 25.pdf
A systematic review of self-coping strategies used by university students to ...
RTP_AR_KS1_Tutor's Guide_English [FOR REPRODUCTION].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

  • #8: To make array sizes easier to maintain, many programmers prefer to use named constants as array size declarator
  • #22: 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.