SlideShare une entreprise Scribd logo
1  sur  39
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

Contenu connexe

Tendances

C++ Pointers And References
C++ Pointers And ReferencesC++ Pointers And References
C++ Pointers And References
verisan
 

Tendances (20)

SQL Queries
SQL QueriesSQL Queries
SQL Queries
 
Sql commands
Sql commandsSql commands
Sql commands
 
Data structures and algorithms
Data structures and algorithmsData structures and algorithms
Data structures and algorithms
 
03. Operators Expressions and statements
03. Operators Expressions and statements03. Operators Expressions and statements
03. Operators Expressions and statements
 
trigger dbms
trigger dbmstrigger dbms
trigger dbms
 
Triggers in SQL | Edureka
Triggers in SQL | EdurekaTriggers in SQL | Edureka
Triggers in SQL | Edureka
 
My Sql
My Sql My Sql
My Sql
 
sql function(ppt)
sql function(ppt)sql function(ppt)
sql function(ppt)
 
Aggregate functions
Aggregate functionsAggregate functions
Aggregate functions
 
C++ Pointers And References
C++ Pointers And ReferencesC++ Pointers And References
C++ Pointers And References
 
DML, DDL, DCL ,DRL/DQL and TCL Statements in SQL with Examples
DML, DDL, DCL ,DRL/DQL and TCL Statements in SQL with ExamplesDML, DDL, DCL ,DRL/DQL and TCL Statements in SQL with Examples
DML, DDL, DCL ,DRL/DQL and TCL Statements in SQL with Examples
 
ClickHouse tips and tricks. Webinar slides. By Robert Hodges, Altinity CEO
ClickHouse tips and tricks. Webinar slides. By Robert Hodges, Altinity CEOClickHouse tips and tricks. Webinar slides. By Robert Hodges, Altinity CEO
ClickHouse tips and tricks. Webinar slides. By Robert Hodges, Altinity CEO
 
SQL window functions for MySQL
SQL window functions for MySQLSQL window functions for MySQL
SQL window functions for MySQL
 
plsql.ppt
plsql.pptplsql.ppt
plsql.ppt
 
MySQL Functions
MySQL FunctionsMySQL Functions
MySQL Functions
 
Building ClickHouse and Making Your First Contribution: A Tutorial_06.10.2021
Building ClickHouse and Making Your First Contribution: A Tutorial_06.10.2021Building ClickHouse and Making Your First Contribution: A Tutorial_06.10.2021
Building ClickHouse and Making Your First Contribution: A Tutorial_06.10.2021
 
Database Management - Lecture 2 - SQL select, insert, update and delete
Database Management - Lecture 2 - SQL select, insert, update and deleteDatabase Management - Lecture 2 - SQL select, insert, update and delete
Database Management - Lecture 2 - SQL select, insert, update and delete
 
COMMON ABEND CODES
COMMON ABEND CODESCOMMON ABEND CODES
COMMON ABEND CODES
 
Database Concept - Normalization (1NF, 2NF, 3NF)
Database Concept - Normalization (1NF, 2NF, 3NF)Database Concept - Normalization (1NF, 2NF, 3NF)
Database Concept - Normalization (1NF, 2NF, 3NF)
 
Digital electronics- BCD & Decoder
Digital electronics- BCD & DecoderDigital electronics- BCD & Decoder
Digital electronics- BCD & Decoder
 

Similaire à 8 Array

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
 

Similaire à 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
 

Dernier

1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
QucHHunhnh
 
Seal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxSeal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptx
negromaestrong
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
kauryashika82
 

Dernier (20)

Magic bus Group work1and 2 (Team 3).pptx
Magic bus Group work1and 2 (Team 3).pptxMagic bus Group work1and 2 (Team 3).pptx
Magic bus Group work1and 2 (Team 3).pptx
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
 
Seal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxSeal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptx
 
Spatium Project Simulation student brief
Spatium Project Simulation student briefSpatium Project Simulation student brief
Spatium Project Simulation student brief
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
 
Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptx
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptx
 
Dyslexia AI Workshop for Slideshare.pptx
Dyslexia AI Workshop for Slideshare.pptxDyslexia AI Workshop for Slideshare.pptx
Dyslexia AI Workshop for Slideshare.pptx
 
ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
 
Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024
 
Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdf
 
Asian American Pacific Islander Month DDSD 2024.pptx
Asian American Pacific Islander Month DDSD 2024.pptxAsian American Pacific Islander Month DDSD 2024.pptx
Asian American Pacific Islander Month DDSD 2024.pptx
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
 
Unit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxUnit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptx
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and Modifications
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docx
 
psychiatric nursing HISTORY COLLECTION .docx
psychiatric  nursing HISTORY  COLLECTION  .docxpsychiatric  nursing HISTORY  COLLECTION  .docx
psychiatric nursing HISTORY COLLECTION .docx
 

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

Notes de l'éditeur

  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.