SlideShare a Scribd company logo
1 of 20
Copyright © 2016 Pearson Education, Inc., Hoboken NJ
CHAPTER8
Arrays
Copyright © 2016 Pearson Education, Inc., Hoboken NJ
8-2
Chapter Topics
8.1 Array Basics
8.2 Sequentially Searching an Array
8.3 Processing the Contents of an Array
8.4 Parallel Arrays
8.5 Two-Dimensional Arrays
8.6 Arrays of Three or More Dimension
Copyright © 2016 Pearson Education, Inc., Hoboken NJ
8-3
8.1 Array Basics
An array allows you to store a group of items of
the same data type together in memory
– Why? Instead of creating multiple similar
variables such as employee1, employee2,
employee3 and so on…
– It’s more efficient to create just one variable
• Declare String employees[50]
• Declare Real salesAmounts[7]
– The number in the [ ] is the size of the array
Copyright © 2016 Pearson Education, Inc., Hoboken NJ
8-4
8.1 Array Basics
• The storage locations in an array are elements
• Each element of the array has a unique number
called a subscript that identifies it – the
subscript starts at 0 in most languages.
Figure 8-1 Array subscripts
Copyright © 2016 Pearson Education, Inc., Hoboken NJ
8-5
8.1 Array Basics
Assigning values can be done individually using
a subscript…
Set numbers[0] = 20
Set numbers[1] = 30
Set numbers[2] = 40
Set numbers[3] = 50
Set numbers[4[ = 60
But, it is much more efficient to use a Loop to
step through the array
Copyright © 2016 Pearson Education, Inc., Hoboken NJ
8-6
8.1 Array Basics
Figure 8-3 Contents of the hours array
Copyright © 2016 Pearson Education, Inc., Hoboken NJ
8-7
8.1 Array Basics
Arrays can be initialized to 0 or specific values
Declare String days[7] = “Sunday”, “Monday”, “Tuesday”,
Wednesday”, “Thursday”, “Friday”, “Saturday”
Array bounds checking should be performed to
avoid use of an invalid subscript
Days[7] = “Saturday” is invalid because there is no 7 index
– A common error is running a loop one time more than is
necessary, exceeding the bound of the array
– Off-by-one Error
Copyright © 2016 Pearson Education, Inc., Hoboken NJ
8.1 Array Basics
• Partially Filled Array
– Sometimes an array is only partially filled
– To avoid processing the unfilled elements, you must
have an accompanying integer variable that holds
the number of items stored in the array.
• When the array is empty, 0 is stored in this variable
• The variable is incremented each time an item is added to
the array
• The variable's value is used as the array's size when
stepping through the array.
8-8
Copyright © 2016 Pearson Education, Inc., Hoboken NJ
Constant Integer SIZE = 100
Declare Integer values[SIZE]
Declare Integer count = 0
Declare Integer number
Declare Integer Index
Display "Enter a number, or -1 to quit."
Input number
While (number != -1 AND count < SIZE)
Set values[count] = number
Set count = count + 1
Display "Enter a number, or -1 to quit."
Input number
End While
Display "Here are the values you entered:"
For index = 0 To count - 1
Display values[index]
End For
8.1 Array Basics
8-9
Partially Filled Array
Example
Thecountvariableholdsthenumberofitemsstoredinthearray.
Copyright © 2016 Pearson Education, Inc., Hoboken NJ
8.1 Array Basics
• Optional Topic: The For Each Loop
– Some languages provide a For Each loop
– It works with an array, iterating once for each array
element
– During each iteration, the loop copies an element's
value to a variable.
8-10
Copyright © 2016 Pearson Education, Inc., Hoboken NJ
8.1 Array Basics
8-11
Constant Integer SIZE = 5
Declare Integer numbers[SIZE] = 5, 10, 15, 20, 25
Declare Integer num
For Each num In numbers
Display num
End For
For Each Example
Copyright © 2016 Pearson Education, Inc., Hoboken NJ
8-12
8.2 Sequentially Searching an Array
A sequential search algorithm is a simple technique for finding an
item in a string or numeric array
– Uses a loop to sequentially step through an array
– Compares each element with the value being searched for
– Stops when the value is found or the end of the array is hit
Set found = False
Set index = 0
While found == False AND index <= SIZE -1
If (array[index] == searchValue Then
Set found = True
Else
Set index = index + 1
End If
End While
Copyright © 2016 Pearson Education, Inc., Hoboken NJ
8-13
8.3 Processing the Contents of an Array
Totaling the values in an array and calculating
average
– Loops are used to accumulate the values
– Then, the total is simply divided
by the size
Copyright © 2016 Pearson Education, Inc., Hoboken NJ
Example
8-14
Copyright © 2016 Pearson Education, Inc., Hoboken NJ
8-15
8.3 Processing the Contents of an Array
Finding the highest & lowest values in an array
• The highest
– Create a variable to hold the highest value
– Assign the value at element 0 to the highest
– Use a loop to step through the rest of the elements
– Each iteration, a comparison is made to the highest variable
– If the element is greater than the highest value, that value is
then the assigned to the highest variable
• The lowest
– Same process, but checks if the element is less than the
lowest value
Copyright © 2016 Pearson Education, Inc., Hoboken NJ
8-16
8.3 Processing the Contents of an Array
Copying an array can be done using loops
For index = 0 to SIZE – 1
Set secondArray[index] = firstArray[index]
End For
Passing an Array as an Argument
– Usually must pass the array and the size
The module call
getTotal(numbers, SIZE)
The module header
Function Integer getTotal (Integer array[], Integer arraySize)
Copyright © 2016 Pearson Education, Inc., Hoboken NJ
8-17
8.4 Parallel Arrays
By using the same subscript, you can establish a
relationship between data stored in two or
more arrays
Figure 8-14 The names
and addresses arrays
Copyright © 2016 Pearson Education, Inc., Hoboken NJ
8-18
8.5 Two-Dimensional Arrays
A two-dimensional array is like several identical
arrays put together
– Suppose a teacher has six students who take five
tests
Figure 8-17 Two-
dimensional array
with six rows and
five columns
Copyright © 2016 Pearson Education, Inc., Hoboken NJ
8-19
8.5 Two-Dimensional Arrays
Two size variables are required when declaring
Constant Integer ROWS = 3
Constant Integer COLS = 4
Declare Integer values[ROWS][COLS]
Accessing is done with two loops, and both subscripts
For row = 0 To ROWS -1
For col = 0 To COLS – 1
Display “Enter a number.”
Input values[row][col]
End For
End For
Copyright © 2016 Pearson Education, Inc., Hoboken NJ
8-20
8.6 Arrays of Three or More Dimensions
Arrays can also be three or more dimensions
Declare Real seats[3][5][8]
Figure 8-22 A three-
dimensional array

More Related Content

What's hot

Introduction to system programming
Introduction to system programmingIntroduction to system programming
Introduction to system programmingsonalikharade3
 
Lecture1 - Computer Architecture
Lecture1 - Computer ArchitectureLecture1 - Computer Architecture
Lecture1 - Computer ArchitectureVolodymyr Ushenko
 
Operating systems chapter 5 silberschatz
Operating systems chapter 5 silberschatzOperating systems chapter 5 silberschatz
Operating systems chapter 5 silberschatzGiulianoRanauro
 
Instruction Set Architecture (ISA)
Instruction Set Architecture (ISA)Instruction Set Architecture (ISA)
Instruction Set Architecture (ISA)Gaditek
 
Computer memory
Computer memoryComputer memory
Computer memoryJayapal Jp
 
Fundamentals of Programming Chapter 3
Fundamentals of Programming Chapter 3Fundamentals of Programming Chapter 3
Fundamentals of Programming Chapter 3Mohd Harris Ahmad Jaal
 
Introduction to multi core
Introduction to multi coreIntroduction to multi core
Introduction to multi coremukul bhardwaj
 
Memory (Computer Organization)
Memory (Computer Organization)Memory (Computer Organization)
Memory (Computer Organization)JyotiprakashMishra18
 
OS Components and Structure
OS Components and StructureOS Components and Structure
OS Components and Structuresathish sak
 
Chapter 5. computer system
Chapter 5. computer systemChapter 5. computer system
Chapter 5. computer systemAshish KC
 
Chapter 4 Microprocessor CPU
Chapter 4 Microprocessor CPUChapter 4 Microprocessor CPU
Chapter 4 Microprocessor CPUaskme
 
Basic hardware concept
Basic hardware concept Basic hardware concept
Basic hardware concept Danilo Anos
 
Parts of computer
Parts of computerParts of computer
Parts of computerMukul Kumar
 
1.Basic Structure of Computer System.ppt
1.Basic Structure of Computer System.ppt1.Basic Structure of Computer System.ppt
1.Basic Structure of Computer System.pptJEEVANANTHAMG6
 
System Programming- Unit I
System Programming- Unit ISystem Programming- Unit I
System Programming- Unit ISaranya1702
 
Units of storage in computer
Units of storage in computerUnits of storage in computer
Units of storage in computerakorede2000
 

What's hot (20)

Operating system notes pdf
Operating system notes pdfOperating system notes pdf
Operating system notes pdf
 
Introduction to system programming
Introduction to system programmingIntroduction to system programming
Introduction to system programming
 
Lecture1 - Computer Architecture
Lecture1 - Computer ArchitectureLecture1 - Computer Architecture
Lecture1 - Computer Architecture
 
Operating systems chapter 5 silberschatz
Operating systems chapter 5 silberschatzOperating systems chapter 5 silberschatz
Operating systems chapter 5 silberschatz
 
Instruction Set Architecture (ISA)
Instruction Set Architecture (ISA)Instruction Set Architecture (ISA)
Instruction Set Architecture (ISA)
 
Computer memory
Computer memoryComputer memory
Computer memory
 
Operating system ppt
Operating system pptOperating system ppt
Operating system ppt
 
Fundamentals of Programming Chapter 3
Fundamentals of Programming Chapter 3Fundamentals of Programming Chapter 3
Fundamentals of Programming Chapter 3
 
Introduction to multi core
Introduction to multi coreIntroduction to multi core
Introduction to multi core
 
8. data types
8. data types8. data types
8. data types
 
Types of system software
Types of system softwareTypes of system software
Types of system software
 
Memory (Computer Organization)
Memory (Computer Organization)Memory (Computer Organization)
Memory (Computer Organization)
 
OS Components and Structure
OS Components and StructureOS Components and Structure
OS Components and Structure
 
Chapter 5. computer system
Chapter 5. computer systemChapter 5. computer system
Chapter 5. computer system
 
Chapter 4 Microprocessor CPU
Chapter 4 Microprocessor CPUChapter 4 Microprocessor CPU
Chapter 4 Microprocessor CPU
 
Basic hardware concept
Basic hardware concept Basic hardware concept
Basic hardware concept
 
Parts of computer
Parts of computerParts of computer
Parts of computer
 
1.Basic Structure of Computer System.ppt
1.Basic Structure of Computer System.ppt1.Basic Structure of Computer System.ppt
1.Basic Structure of Computer System.ppt
 
System Programming- Unit I
System Programming- Unit ISystem Programming- Unit I
System Programming- Unit I
 
Units of storage in computer
Units of storage in computerUnits of storage in computer
Units of storage in computer
 

Similar to Programming Logic and Design: Arrays

Cso gaddis java_chapter8
Cso gaddis java_chapter8Cso gaddis java_chapter8
Cso gaddis java_chapter8RhettB
 
Week06
Week06Week06
Week06hccit
 
Eo gaddis java_chapter_07_5e
Eo gaddis java_chapter_07_5eEo gaddis java_chapter_07_5e
Eo gaddis java_chapter_07_5eGina Bullock
 
Array in php
Array in phpArray in php
Array in phpilakkiya
 
Basics of array.pptx
Basics of array.pptxBasics of array.pptx
Basics of array.pptxPRASENJITMORE2
 
CSCI 238 Chapter 08 Arrays Textbook Slides
CSCI 238 Chapter 08 Arrays Textbook SlidesCSCI 238 Chapter 08 Arrays Textbook Slides
CSCI 238 Chapter 08 Arrays Textbook SlidesDanWooster1
 
Cso gaddis java_chapter8
Cso gaddis java_chapter8Cso gaddis java_chapter8
Cso gaddis java_chapter8mlrbrown
 
Chapter 6 Absolute Java
Chapter 6 Absolute JavaChapter 6 Absolute Java
Chapter 6 Absolute JavaShariq Alee
 
Java arrays (1)
Java arrays (1)Java arrays (1)
Java arrays (1)Liza Abello
 
An Introduction to Programming in Java: Arrays
An Introduction to Programming in Java: ArraysAn Introduction to Programming in Java: Arrays
An Introduction to Programming in Java: ArraysMartin Chapman
 
Arrays.pptx
Arrays.pptxArrays.pptx
Arrays.pptxEpsiba1
 
CP Handout#7
CP Handout#7CP Handout#7
CP Handout#7trupti1976
 
Ppt lesson 12
Ppt lesson 12Ppt lesson 12
Ppt lesson 12Linda Bodrie
 
Arrays in programming
Arrays in programmingArrays in programming
Arrays in programmingTaseerRao
 

Similar to Programming Logic and Design: Arrays (20)

Cso gaddis java_chapter8
Cso gaddis java_chapter8Cso gaddis java_chapter8
Cso gaddis java_chapter8
 
Week06
Week06Week06
Week06
 
Lesson 11 one dimensional array
Lesson 11 one dimensional arrayLesson 11 one dimensional array
Lesson 11 one dimensional array
 
Eo gaddis java_chapter_07_5e
Eo gaddis java_chapter_07_5eEo gaddis java_chapter_07_5e
Eo gaddis java_chapter_07_5e
 
Array in php
Array in phpArray in php
Array in php
 
Basics of array.pptx
Basics of array.pptxBasics of array.pptx
Basics of array.pptx
 
CSCI 238 Chapter 08 Arrays Textbook Slides
CSCI 238 Chapter 08 Arrays Textbook SlidesCSCI 238 Chapter 08 Arrays Textbook Slides
CSCI 238 Chapter 08 Arrays Textbook Slides
 
Cso gaddis java_chapter8
Cso gaddis java_chapter8Cso gaddis java_chapter8
Cso gaddis java_chapter8
 
Mod 12
Mod 12Mod 12
Mod 12
 
Chapter 6 Absolute Java
Chapter 6 Absolute JavaChapter 6 Absolute Java
Chapter 6 Absolute Java
 
Chap6java5th
Chap6java5thChap6java5th
Chap6java5th
 
Arrays
ArraysArrays
Arrays
 
Data structures in c#
Data structures in c#Data structures in c#
Data structures in c#
 
Java arrays (1)
Java arrays (1)Java arrays (1)
Java arrays (1)
 
unit 2.pptx
unit 2.pptxunit 2.pptx
unit 2.pptx
 
An Introduction to Programming in Java: Arrays
An Introduction to Programming in Java: ArraysAn Introduction to Programming in Java: Arrays
An Introduction to Programming in Java: Arrays
 
Arrays.pptx
Arrays.pptxArrays.pptx
Arrays.pptx
 
CP Handout#7
CP Handout#7CP Handout#7
CP Handout#7
 
Ppt lesson 12
Ppt lesson 12Ppt lesson 12
Ppt lesson 12
 
Arrays in programming
Arrays in programmingArrays in programming
Arrays in programming
 

More from Nicole Ryan

Testing and Improving Performance
Testing and Improving PerformanceTesting and Improving Performance
Testing and Improving PerformanceNicole Ryan
 
Optimizing a website for search engines
Optimizing a website for search enginesOptimizing a website for search engines
Optimizing a website for search enginesNicole Ryan
 
Inheritance
InheritanceInheritance
InheritanceNicole Ryan
 
Javascript programming using the document object model
Javascript programming using the document object modelJavascript programming using the document object model
Javascript programming using the document object modelNicole Ryan
 
Working with Video and Audio
Working with Video and AudioWorking with Video and Audio
Working with Video and AudioNicole Ryan
 
Working with Images
Working with ImagesWorking with Images
Working with ImagesNicole Ryan
 
Python Dictionaries and Sets
Python Dictionaries and SetsPython Dictionaries and Sets
Python Dictionaries and SetsNicole Ryan
 
Creating Visual Effects and Animation
Creating Visual Effects and AnimationCreating Visual Effects and Animation
Creating Visual Effects and AnimationNicole Ryan
 
Creating and Processing Web Forms
Creating and Processing Web FormsCreating and Processing Web Forms
Creating and Processing Web FormsNicole Ryan
 
Organizing Content with Lists and Tables
Organizing Content with Lists and TablesOrganizing Content with Lists and Tables
Organizing Content with Lists and TablesNicole Ryan
 
Social media and your website
Social media and your websiteSocial media and your website
Social media and your websiteNicole Ryan
 
Working with Links
Working with LinksWorking with Links
Working with LinksNicole Ryan
 
Formatting text with CSS
Formatting text with CSSFormatting text with CSS
Formatting text with CSSNicole Ryan
 
Laying Out Elements with CSS
Laying Out Elements with CSSLaying Out Elements with CSS
Laying Out Elements with CSSNicole Ryan
 
Getting Started with CSS
Getting Started with CSSGetting Started with CSS
Getting Started with CSSNicole Ryan
 
Structure Web Content
Structure Web ContentStructure Web Content
Structure Web ContentNicole Ryan
 
Getting Started with your Website
Getting Started with your WebsiteGetting Started with your Website
Getting Started with your WebsiteNicole Ryan
 
Chapter 12 Lecture: GUI Programming, Multithreading, and Animation
Chapter 12 Lecture: GUI Programming, Multithreading, and AnimationChapter 12 Lecture: GUI Programming, Multithreading, and Animation
Chapter 12 Lecture: GUI Programming, Multithreading, and AnimationNicole Ryan
 
Chapter 11: Object Oriented Programming Part 2
Chapter 11: Object Oriented Programming Part 2Chapter 11: Object Oriented Programming Part 2
Chapter 11: Object Oriented Programming Part 2Nicole Ryan
 
Intro to Programming: Modularity
Intro to Programming: ModularityIntro to Programming: Modularity
Intro to Programming: ModularityNicole Ryan
 

More from Nicole Ryan (20)

Testing and Improving Performance
Testing and Improving PerformanceTesting and Improving Performance
Testing and Improving Performance
 
Optimizing a website for search engines
Optimizing a website for search enginesOptimizing a website for search engines
Optimizing a website for search engines
 
Inheritance
InheritanceInheritance
Inheritance
 
Javascript programming using the document object model
Javascript programming using the document object modelJavascript programming using the document object model
Javascript programming using the document object model
 
Working with Video and Audio
Working with Video and AudioWorking with Video and Audio
Working with Video and Audio
 
Working with Images
Working with ImagesWorking with Images
Working with Images
 
Python Dictionaries and Sets
Python Dictionaries and SetsPython Dictionaries and Sets
Python Dictionaries and Sets
 
Creating Visual Effects and Animation
Creating Visual Effects and AnimationCreating Visual Effects and Animation
Creating Visual Effects and Animation
 
Creating and Processing Web Forms
Creating and Processing Web FormsCreating and Processing Web Forms
Creating and Processing Web Forms
 
Organizing Content with Lists and Tables
Organizing Content with Lists and TablesOrganizing Content with Lists and Tables
Organizing Content with Lists and Tables
 
Social media and your website
Social media and your websiteSocial media and your website
Social media and your website
 
Working with Links
Working with LinksWorking with Links
Working with Links
 
Formatting text with CSS
Formatting text with CSSFormatting text with CSS
Formatting text with CSS
 
Laying Out Elements with CSS
Laying Out Elements with CSSLaying Out Elements with CSS
Laying Out Elements with CSS
 
Getting Started with CSS
Getting Started with CSSGetting Started with CSS
Getting Started with CSS
 
Structure Web Content
Structure Web ContentStructure Web Content
Structure Web Content
 
Getting Started with your Website
Getting Started with your WebsiteGetting Started with your Website
Getting Started with your Website
 
Chapter 12 Lecture: GUI Programming, Multithreading, and Animation
Chapter 12 Lecture: GUI Programming, Multithreading, and AnimationChapter 12 Lecture: GUI Programming, Multithreading, and Animation
Chapter 12 Lecture: GUI Programming, Multithreading, and Animation
 
Chapter 11: Object Oriented Programming Part 2
Chapter 11: Object Oriented Programming Part 2Chapter 11: Object Oriented Programming Part 2
Chapter 11: Object Oriented Programming Part 2
 
Intro to Programming: Modularity
Intro to Programming: ModularityIntro to Programming: Modularity
Intro to Programming: Modularity
 

Recently uploaded

Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Krashi Coaching
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAssociation for Project Management
 
Advance Mobile Application Development class 07
Advance Mobile Application Development class 07Advance Mobile Application Development class 07
Advance Mobile Application Development class 07Dr. Mazin Mohamed alkathiri
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingTechSoup
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptxVS Mahajan Coaching Centre
 
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...Sapna Thakur
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxiammrhaywood
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfsanyamsingh5019
 
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 . pdfQucHHunhnh
 
The byproduct of sericulture in different industries.pptx
The byproduct of sericulture in different industries.pptxThe byproduct of sericulture in different industries.pptx
The byproduct of sericulture in different industries.pptxShobhayan Kirtania
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionSafetyChain Software
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Sapana Sha
 
Russian Call Girls in Andheri Airport Mumbai WhatsApp 9167673311 đź’ž Full Nigh...
Russian Call Girls in Andheri Airport Mumbai WhatsApp  9167673311 đź’ž Full Nigh...Russian Call Girls in Andheri Airport Mumbai WhatsApp  9167673311 đź’ž Full Nigh...
Russian Call Girls in Andheri Airport Mumbai WhatsApp 9167673311 đź’ž Full Nigh...Pooja Nehwal
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxGaneshChakor2
 
Student login on Anyboli platform.helpin
Student login on Anyboli platform.helpinStudent login on Anyboli platform.helpin
Student login on Anyboli platform.helpinRaunakKeshri1
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxheathfieldcps1
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)eniolaolutunde
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13Steve Thomason
 

Recently uploaded (20)

Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across Sectors
 
Advance Mobile Application Development class 07
Advance Mobile Application Development class 07Advance Mobile Application Development class 07
Advance Mobile Application Development class 07
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy Consulting
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
 
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdf
 
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
 
The byproduct of sericulture in different industries.pptx
The byproduct of sericulture in different industries.pptxThe byproduct of sericulture in different industries.pptx
The byproduct of sericulture in different industries.pptx
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory Inspection
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
 
Russian Call Girls in Andheri Airport Mumbai WhatsApp 9167673311 đź’ž Full Nigh...
Russian Call Girls in Andheri Airport Mumbai WhatsApp  9167673311 đź’ž Full Nigh...Russian Call Girls in Andheri Airport Mumbai WhatsApp  9167673311 đź’ž Full Nigh...
Russian Call Girls in Andheri Airport Mumbai WhatsApp 9167673311 đź’ž Full Nigh...
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptx
 
Student login on Anyboli platform.helpin
Student login on Anyboli platform.helpinStudent login on Anyboli platform.helpin
Student login on Anyboli platform.helpin
 
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
 
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13
 

Programming Logic and Design: Arrays

  • 1. Copyright © 2016 Pearson Education, Inc., Hoboken NJ CHAPTER8 Arrays
  • 2. Copyright © 2016 Pearson Education, Inc., Hoboken NJ 8-2 Chapter Topics 8.1 Array Basics 8.2 Sequentially Searching an Array 8.3 Processing the Contents of an Array 8.4 Parallel Arrays 8.5 Two-Dimensional Arrays 8.6 Arrays of Three or More Dimension
  • 3. Copyright © 2016 Pearson Education, Inc., Hoboken NJ 8-3 8.1 Array Basics An array allows you to store a group of items of the same data type together in memory – Why? Instead of creating multiple similar variables such as employee1, employee2, employee3 and so on… – It’s more efficient to create just one variable • Declare String employees[50] • Declare Real salesAmounts[7] – The number in the [ ] is the size of the array
  • 4. Copyright © 2016 Pearson Education, Inc., Hoboken NJ 8-4 8.1 Array Basics • The storage locations in an array are elements • Each element of the array has a unique number called a subscript that identifies it – the subscript starts at 0 in most languages. Figure 8-1 Array subscripts
  • 5. Copyright © 2016 Pearson Education, Inc., Hoboken NJ 8-5 8.1 Array Basics Assigning values can be done individually using a subscript… Set numbers[0] = 20 Set numbers[1] = 30 Set numbers[2] = 40 Set numbers[3] = 50 Set numbers[4[ = 60 But, it is much more efficient to use a Loop to step through the array
  • 6. Copyright © 2016 Pearson Education, Inc., Hoboken NJ 8-6 8.1 Array Basics Figure 8-3 Contents of the hours array
  • 7. Copyright © 2016 Pearson Education, Inc., Hoboken NJ 8-7 8.1 Array Basics Arrays can be initialized to 0 or specific values Declare String days[7] = “Sunday”, “Monday”, “Tuesday”, Wednesday”, “Thursday”, “Friday”, “Saturday” Array bounds checking should be performed to avoid use of an invalid subscript Days[7] = “Saturday” is invalid because there is no 7 index – A common error is running a loop one time more than is necessary, exceeding the bound of the array – Off-by-one Error
  • 8. Copyright © 2016 Pearson Education, Inc., Hoboken NJ 8.1 Array Basics • Partially Filled Array – Sometimes an array is only partially filled – To avoid processing the unfilled elements, you must have an accompanying integer variable that holds the number of items stored in the array. • When the array is empty, 0 is stored in this variable • The variable is incremented each time an item is added to the array • The variable's value is used as the array's size when stepping through the array. 8-8
  • 9. Copyright © 2016 Pearson Education, Inc., Hoboken NJ Constant Integer SIZE = 100 Declare Integer values[SIZE] Declare Integer count = 0 Declare Integer number Declare Integer Index Display "Enter a number, or -1 to quit." Input number While (number != -1 AND count < SIZE) Set values[count] = number Set count = count + 1 Display "Enter a number, or -1 to quit." Input number End While Display "Here are the values you entered:" For index = 0 To count - 1 Display values[index] End For 8.1 Array Basics 8-9 Partially Filled Array Example Thecountvariableholdsthenumberofitemsstoredinthearray.
  • 10. Copyright © 2016 Pearson Education, Inc., Hoboken NJ 8.1 Array Basics • Optional Topic: The For Each Loop – Some languages provide a For Each loop – It works with an array, iterating once for each array element – During each iteration, the loop copies an element's value to a variable. 8-10
  • 11. Copyright © 2016 Pearson Education, Inc., Hoboken NJ 8.1 Array Basics 8-11 Constant Integer SIZE = 5 Declare Integer numbers[SIZE] = 5, 10, 15, 20, 25 Declare Integer num For Each num In numbers Display num End For For Each Example
  • 12. Copyright © 2016 Pearson Education, Inc., Hoboken NJ 8-12 8.2 Sequentially Searching an Array A sequential search algorithm is a simple technique for finding an item in a string or numeric array – Uses a loop to sequentially step through an array – Compares each element with the value being searched for – Stops when the value is found or the end of the array is hit Set found = False Set index = 0 While found == False AND index <= SIZE -1 If (array[index] == searchValue Then Set found = True Else Set index = index + 1 End If End While
  • 13. Copyright © 2016 Pearson Education, Inc., Hoboken NJ 8-13 8.3 Processing the Contents of an Array Totaling the values in an array and calculating average – Loops are used to accumulate the values – Then, the total is simply divided by the size
  • 14. Copyright © 2016 Pearson Education, Inc., Hoboken NJ Example 8-14
  • 15. Copyright © 2016 Pearson Education, Inc., Hoboken NJ 8-15 8.3 Processing the Contents of an Array Finding the highest & lowest values in an array • The highest – Create a variable to hold the highest value – Assign the value at element 0 to the highest – Use a loop to step through the rest of the elements – Each iteration, a comparison is made to the highest variable – If the element is greater than the highest value, that value is then the assigned to the highest variable • The lowest – Same process, but checks if the element is less than the lowest value
  • 16. Copyright © 2016 Pearson Education, Inc., Hoboken NJ 8-16 8.3 Processing the Contents of an Array Copying an array can be done using loops For index = 0 to SIZE – 1 Set secondArray[index] = firstArray[index] End For Passing an Array as an Argument – Usually must pass the array and the size The module call getTotal(numbers, SIZE) The module header Function Integer getTotal (Integer array[], Integer arraySize)
  • 17. Copyright © 2016 Pearson Education, Inc., Hoboken NJ 8-17 8.4 Parallel Arrays By using the same subscript, you can establish a relationship between data stored in two or more arrays Figure 8-14 The names and addresses arrays
  • 18. Copyright © 2016 Pearson Education, Inc., Hoboken NJ 8-18 8.5 Two-Dimensional Arrays A two-dimensional array is like several identical arrays put together – Suppose a teacher has six students who take five tests Figure 8-17 Two- dimensional array with six rows and five columns
  • 19. Copyright © 2016 Pearson Education, Inc., Hoboken NJ 8-19 8.5 Two-Dimensional Arrays Two size variables are required when declaring Constant Integer ROWS = 3 Constant Integer COLS = 4 Declare Integer values[ROWS][COLS] Accessing is done with two loops, and both subscripts For row = 0 To ROWS -1 For col = 0 To COLS – 1 Display “Enter a number.” Input values[row][col] End For End For
  • 20. Copyright © 2016 Pearson Education, Inc., Hoboken NJ 8-20 8.6 Arrays of Three or More Dimensions Arrays can also be three or more dimensions Declare Real seats[3][5][8] Figure 8-22 A three- dimensional array