SlideShare a Scribd company logo
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
Ad

More Related Content

What's hot (20)

Data Preprocessing
Data PreprocessingData Preprocessing
Data Preprocessing
Kamal Acharya
 
Feistel cipher
Feistel cipherFeistel cipher
Feistel cipher
MDKAWSARAHMEDSAGAR
 
Binary Heap Tree, Data Structure
Binary Heap Tree, Data Structure Binary Heap Tree, Data Structure
Binary Heap Tree, Data Structure
Anand Ingle
 
Database management system basic, database, database management, learn databa...
Database management system basic, database, database management, learn databa...Database management system basic, database, database management, learn databa...
Database management system basic, database, database management, learn databa...
University of Science and Technology Chitttagong
 
Python introduction
Python introductionPython introduction
Python introduction
Jignesh Kariya
 
Symmetric Encryption Techniques
Symmetric Encryption Techniques Symmetric Encryption Techniques
Symmetric Encryption Techniques
Dr. Kapil Gupta
 
Chapter Two(1)
Chapter Two(1)Chapter Two(1)
Chapter Two(1)
bolovv
 
Python Programming Tutorial | Edureka
Python Programming Tutorial | EdurekaPython Programming Tutorial | Edureka
Python Programming Tutorial | Edureka
Edureka!
 
Regular expressions and php
Regular expressions and phpRegular expressions and php
Regular expressions and php
David Stockton
 
Different types of Symmetric key Cryptography
Different types of Symmetric key CryptographyDifferent types of Symmetric key Cryptography
Different types of Symmetric key Cryptography
subhradeep mitra
 
Heaps
HeapsHeaps
Heaps
Hafiz Atif Amin
 
UNIT III NON LINEAR DATA STRUCTURES – TREES
UNIT III 	NON LINEAR DATA STRUCTURES – TREESUNIT III 	NON LINEAR DATA STRUCTURES – TREES
UNIT III NON LINEAR DATA STRUCTURES – TREES
Kathirvel Ayyaswamy
 
Ch 7 data binding
Ch 7 data bindingCh 7 data binding
Ch 7 data binding
Madhuri Kavade
 
Data Structures and Algorithm - Module 1.pptx
Data Structures and Algorithm - Module 1.pptxData Structures and Algorithm - Module 1.pptx
Data Structures and Algorithm - Module 1.pptx
EllenGrace9
 
Cypher technique
Cypher techniqueCypher technique
Cypher technique
Zubair CH
 
Data Science With Python | Python For Data Science | Python Data Science Cour...
Data Science With Python | Python For Data Science | Python Data Science Cour...Data Science With Python | Python For Data Science | Python Data Science Cour...
Data Science With Python | Python For Data Science | Python Data Science Cour...
Simplilearn
 
Xml presentation
Xml presentationXml presentation
Xml presentation
Miguel Angel Teheran Garcia
 
Introduction to JavaScript
Introduction to JavaScriptIntroduction to JavaScript
Introduction to JavaScript
Andres Baravalle
 
Css position property
Css position propertyCss position property
Css position property
AnujRana43
 
OSI Layer Security
OSI Layer SecurityOSI Layer Security
OSI Layer Security
Nurkholish Halim
 
Data Preprocessing
Data PreprocessingData Preprocessing
Data Preprocessing
Kamal Acharya
 
Binary Heap Tree, Data Structure
Binary Heap Tree, Data Structure Binary Heap Tree, Data Structure
Binary Heap Tree, Data Structure
Anand Ingle
 
Python introduction
Python introductionPython introduction
Python introduction
Jignesh Kariya
 
Symmetric Encryption Techniques
Symmetric Encryption Techniques Symmetric Encryption Techniques
Symmetric Encryption Techniques
Dr. Kapil Gupta
 
Chapter Two(1)
Chapter Two(1)Chapter Two(1)
Chapter Two(1)
bolovv
 
Python Programming Tutorial | Edureka
Python Programming Tutorial | EdurekaPython Programming Tutorial | Edureka
Python Programming Tutorial | Edureka
Edureka!
 
Regular expressions and php
Regular expressions and phpRegular expressions and php
Regular expressions and php
David Stockton
 
Different types of Symmetric key Cryptography
Different types of Symmetric key CryptographyDifferent types of Symmetric key Cryptography
Different types of Symmetric key Cryptography
subhradeep mitra
 
UNIT III NON LINEAR DATA STRUCTURES – TREES
UNIT III 	NON LINEAR DATA STRUCTURES – TREESUNIT III 	NON LINEAR DATA STRUCTURES – TREES
UNIT III NON LINEAR DATA STRUCTURES – TREES
Kathirvel Ayyaswamy
 
Ch 7 data binding
Ch 7 data bindingCh 7 data binding
Ch 7 data binding
Madhuri Kavade
 
Data Structures and Algorithm - Module 1.pptx
Data Structures and Algorithm - Module 1.pptxData Structures and Algorithm - Module 1.pptx
Data Structures and Algorithm - Module 1.pptx
EllenGrace9
 
Cypher technique
Cypher techniqueCypher technique
Cypher technique
Zubair CH
 
Data Science With Python | Python For Data Science | Python Data Science Cour...
Data Science With Python | Python For Data Science | Python Data Science Cour...Data Science With Python | Python For Data Science | Python Data Science Cour...
Data Science With Python | Python For Data Science | Python Data Science Cour...
Simplilearn
 
Introduction to JavaScript
Introduction to JavaScriptIntroduction to JavaScript
Introduction to JavaScript
Andres Baravalle
 
Css position property
Css position propertyCss position property
Css position property
AnujRana43
 

Similar to Programming Logic and Design: Arrays (20)

Cso gaddis java_chapter8
Cso gaddis java_chapter8Cso gaddis java_chapter8
Cso gaddis java_chapter8
RhettB
 
Week06
Week06Week06
Week06
hccit
 
Lesson 11 one dimensional array
Lesson 11 one dimensional arrayLesson 11 one dimensional array
Lesson 11 one dimensional array
MLG College of Learning, Inc
 
the array, which stores a fixed-size sequential collection of elements of the...
the array, which stores a fixed-size sequential collection of elements of the...the array, which stores a fixed-size sequential collection of elements of the...
the array, which stores a fixed-size sequential collection of elements of the...
Kavitha S
 
Eo gaddis java_chapter_07_5e
Eo gaddis java_chapter_07_5eEo gaddis java_chapter_07_5e
Eo gaddis java_chapter_07_5e
Gina Bullock
 
Array in php
Array in phpArray in php
Array in php
ilakkiya
 
Basics of array.pptx
Basics of array.pptxBasics of array.pptx
Basics of array.pptx
PRASENJITMORE2
 
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
DanWooster1
 
Cso gaddis java_chapter8
Cso gaddis java_chapter8Cso gaddis java_chapter8
Cso gaddis java_chapter8
mlrbrown
 
Mod 12
Mod 12Mod 12
Mod 12
obrienduke
 
Chap6java5th
Chap6java5thChap6java5th
Chap6java5th
Asfand Hassan
 
Chapter 6 Absolute Java
Chapter 6 Absolute JavaChapter 6 Absolute Java
Chapter 6 Absolute Java
Shariq Alee
 
Arrays
ArraysArrays
Arrays
Andy Juan Sarango Veliz
 
Data structures in c#
Data structures in c#Data structures in c#
Data structures in c#
SivaSankar Gorantla
 
Java arrays (1)
Java arrays (1)Java arrays (1)
Java arrays (1)
Liza Abello
 
unit 2.pptx
unit 2.pptxunit 2.pptx
unit 2.pptx
researchgrad82
 
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
Martin Chapman
 
Arrays.pptx
Arrays.pptxArrays.pptx
Arrays.pptx
Epsiba1
 
CP Handout#7
CP Handout#7CP Handout#7
CP Handout#7
trupti1976
 
Ppt lesson 12
Ppt lesson 12Ppt lesson 12
Ppt lesson 12
Linda Bodrie
 
Cso gaddis java_chapter8
Cso gaddis java_chapter8Cso gaddis java_chapter8
Cso gaddis java_chapter8
RhettB
 
Week06
Week06Week06
Week06
hccit
 
the array, which stores a fixed-size sequential collection of elements of the...
the array, which stores a fixed-size sequential collection of elements of the...the array, which stores a fixed-size sequential collection of elements of the...
the array, which stores a fixed-size sequential collection of elements of the...
Kavitha S
 
Eo gaddis java_chapter_07_5e
Eo gaddis java_chapter_07_5eEo gaddis java_chapter_07_5e
Eo gaddis java_chapter_07_5e
Gina Bullock
 
Array in php
Array in phpArray in php
Array in php
ilakkiya
 
Basics of array.pptx
Basics of array.pptxBasics of array.pptx
Basics of array.pptx
PRASENJITMORE2
 
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
DanWooster1
 
Cso gaddis java_chapter8
Cso gaddis java_chapter8Cso gaddis java_chapter8
Cso gaddis java_chapter8
mlrbrown
 
Chapter 6 Absolute Java
Chapter 6 Absolute JavaChapter 6 Absolute Java
Chapter 6 Absolute Java
Shariq 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: Arrays
Martin Chapman
 
Arrays.pptx
Arrays.pptxArrays.pptx
Arrays.pptx
Epsiba1
 
CP Handout#7
CP Handout#7CP Handout#7
CP Handout#7
trupti1976
 
Ppt lesson 12
Ppt lesson 12Ppt lesson 12
Ppt lesson 12
Linda Bodrie
 
Ad

More from Nicole Ryan (20)

Testing and Improving Performance
Testing and Improving PerformanceTesting and Improving Performance
Testing and Improving Performance
Nicole Ryan
 
Optimizing a website for search engines
Optimizing a website for search enginesOptimizing a website for search engines
Optimizing a website for search engines
Nicole Ryan
 
Inheritance
InheritanceInheritance
Inheritance
Nicole 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 model
Nicole Ryan
 
Working with Video and Audio
Working with Video and AudioWorking with Video and Audio
Working with Video and Audio
Nicole Ryan
 
Working with Images
Working with ImagesWorking with Images
Working with Images
Nicole Ryan
 
Python Dictionaries and Sets
Python Dictionaries and SetsPython Dictionaries and Sets
Python Dictionaries and Sets
Nicole Ryan
 
Creating Visual Effects and Animation
Creating Visual Effects and AnimationCreating Visual Effects and Animation
Creating Visual Effects and Animation
Nicole Ryan
 
Creating and Processing Web Forms
Creating and Processing Web FormsCreating and Processing Web Forms
Creating and Processing Web Forms
Nicole Ryan
 
Organizing Content with Lists and Tables
Organizing Content with Lists and TablesOrganizing Content with Lists and Tables
Organizing Content with Lists and Tables
Nicole Ryan
 
Social media and your website
Social media and your websiteSocial media and your website
Social media and your website
Nicole Ryan
 
Working with Links
Working with LinksWorking with Links
Working with Links
Nicole Ryan
 
Formatting text with CSS
Formatting text with CSSFormatting text with CSS
Formatting text with CSS
Nicole Ryan
 
Laying Out Elements with CSS
Laying Out Elements with CSSLaying Out Elements with CSS
Laying Out Elements with CSS
Nicole Ryan
 
Getting Started with CSS
Getting Started with CSSGetting Started with CSS
Getting Started with CSS
Nicole Ryan
 
Structure Web Content
Structure Web ContentStructure Web Content
Structure Web Content
Nicole Ryan
 
Getting Started with your Website
Getting Started with your WebsiteGetting Started with your Website
Getting Started with your Website
Nicole 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 Animation
Nicole 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 2
Nicole Ryan
 
Intro to Programming: Modularity
Intro to Programming: ModularityIntro to Programming: Modularity
Intro to Programming: Modularity
Nicole Ryan
 
Testing and Improving Performance
Testing and Improving PerformanceTesting and Improving Performance
Testing and Improving Performance
Nicole Ryan
 
Optimizing a website for search engines
Optimizing a website for search enginesOptimizing a website for search engines
Optimizing a website for search engines
Nicole Ryan
 
Inheritance
InheritanceInheritance
Inheritance
Nicole 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 model
Nicole Ryan
 
Working with Video and Audio
Working with Video and AudioWorking with Video and Audio
Working with Video and Audio
Nicole Ryan
 
Working with Images
Working with ImagesWorking with Images
Working with Images
Nicole Ryan
 
Python Dictionaries and Sets
Python Dictionaries and SetsPython Dictionaries and Sets
Python Dictionaries and Sets
Nicole Ryan
 
Creating Visual Effects and Animation
Creating Visual Effects and AnimationCreating Visual Effects and Animation
Creating Visual Effects and Animation
Nicole Ryan
 
Creating and Processing Web Forms
Creating and Processing Web FormsCreating and Processing Web Forms
Creating and Processing Web Forms
Nicole Ryan
 
Organizing Content with Lists and Tables
Organizing Content with Lists and TablesOrganizing Content with Lists and Tables
Organizing Content with Lists and Tables
Nicole Ryan
 
Social media and your website
Social media and your websiteSocial media and your website
Social media and your website
Nicole Ryan
 
Working with Links
Working with LinksWorking with Links
Working with Links
Nicole Ryan
 
Formatting text with CSS
Formatting text with CSSFormatting text with CSS
Formatting text with CSS
Nicole Ryan
 
Laying Out Elements with CSS
Laying Out Elements with CSSLaying Out Elements with CSS
Laying Out Elements with CSS
Nicole Ryan
 
Getting Started with CSS
Getting Started with CSSGetting Started with CSS
Getting Started with CSS
Nicole Ryan
 
Structure Web Content
Structure Web ContentStructure Web Content
Structure Web Content
Nicole Ryan
 
Getting Started with your Website
Getting Started with your WebsiteGetting Started with your Website
Getting Started with your Website
Nicole 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 Animation
Nicole 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 2
Nicole Ryan
 
Intro to Programming: Modularity
Intro to Programming: ModularityIntro to Programming: Modularity
Intro to Programming: Modularity
Nicole Ryan
 
Ad

Recently uploaded (20)

World war-1(Causes & impacts at a glance) PPT by Simanchala Sarab(BABed,sem-4...
World war-1(Causes & impacts at a glance) PPT by Simanchala Sarab(BABed,sem-4...World war-1(Causes & impacts at a glance) PPT by Simanchala Sarab(BABed,sem-4...
World war-1(Causes & impacts at a glance) PPT by Simanchala Sarab(BABed,sem-4...
larencebapu132
 
How to manage Multiple Warehouses for multiple floors in odoo point of sale
How to manage Multiple Warehouses for multiple floors in odoo point of saleHow to manage Multiple Warehouses for multiple floors in odoo point of sale
How to manage Multiple Warehouses for multiple floors in odoo point of sale
Celine George
 
Understanding P–N Junction Semiconductors: A Beginner’s Guide
Understanding P–N Junction Semiconductors: A Beginner’s GuideUnderstanding P–N Junction Semiconductors: A Beginner’s Guide
Understanding P–N Junction Semiconductors: A Beginner’s Guide
GS Virdi
 
Real GitHub Copilot Exam Dumps for Success
Real GitHub Copilot Exam Dumps for SuccessReal GitHub Copilot Exam Dumps for Success
Real GitHub Copilot Exam Dumps for Success
Mark Soia
 
YSPH VMOC Special Report - Measles Outbreak Southwest US 4-30-2025.pptx
YSPH VMOC Special Report - Measles Outbreak  Southwest US 4-30-2025.pptxYSPH VMOC Special Report - Measles Outbreak  Southwest US 4-30-2025.pptx
YSPH VMOC Special Report - Measles Outbreak Southwest US 4-30-2025.pptx
Yale School of Public Health - The Virtual Medical Operations Center (VMOC)
 
SCI BIZ TECH QUIZ (OPEN) PRELIMS XTASY 2025.pptx
SCI BIZ TECH QUIZ (OPEN) PRELIMS XTASY 2025.pptxSCI BIZ TECH QUIZ (OPEN) PRELIMS XTASY 2025.pptx
SCI BIZ TECH QUIZ (OPEN) PRELIMS XTASY 2025.pptx
Ronisha Das
 
Sinhala_Male_Names.pdf Sinhala_Male_Name
Sinhala_Male_Names.pdf Sinhala_Male_NameSinhala_Male_Names.pdf Sinhala_Male_Name
Sinhala_Male_Names.pdf Sinhala_Male_Name
keshanf79
 
Exercise Physiology MCQS By DR. NASIR MUSTAFA
Exercise Physiology MCQS By DR. NASIR MUSTAFAExercise Physiology MCQS By DR. NASIR MUSTAFA
Exercise Physiology MCQS By DR. NASIR MUSTAFA
Dr. Nasir Mustafa
 
APM Midlands Region April 2025 Sacha Hind Circulated.pdf
APM Midlands Region April 2025 Sacha Hind Circulated.pdfAPM Midlands Region April 2025 Sacha Hind Circulated.pdf
APM Midlands Region April 2025 Sacha Hind Circulated.pdf
Association for Project Management
 
YSPH VMOC Special Report - Measles Outbreak Southwest US 5-3-2025.pptx
YSPH VMOC Special Report - Measles Outbreak  Southwest US 5-3-2025.pptxYSPH VMOC Special Report - Measles Outbreak  Southwest US 5-3-2025.pptx
YSPH VMOC Special Report - Measles Outbreak Southwest US 5-3-2025.pptx
Yale School of Public Health - The Virtual Medical Operations Center (VMOC)
 
Operations Management (Dr. Abdulfatah Salem).pdf
Operations Management (Dr. Abdulfatah Salem).pdfOperations Management (Dr. Abdulfatah Salem).pdf
Operations Management (Dr. Abdulfatah Salem).pdf
Arab Academy for Science, Technology and Maritime Transport
 
Presentation of the MIPLM subject matter expert Erdem Kaya
Presentation of the MIPLM subject matter expert Erdem KayaPresentation of the MIPLM subject matter expert Erdem Kaya
Presentation of the MIPLM subject matter expert Erdem Kaya
MIPLM
 
Contact Lens:::: An Overview.pptx.: Optometry
Contact Lens:::: An Overview.pptx.: OptometryContact Lens:::: An Overview.pptx.: Optometry
Contact Lens:::: An Overview.pptx.: Optometry
MushahidRaza8
 
How to Manage Purchase Alternatives in Odoo 18
How to Manage Purchase Alternatives in Odoo 18How to Manage Purchase Alternatives in Odoo 18
How to Manage Purchase Alternatives in Odoo 18
Celine George
 
Political History of Pala dynasty Pala Rulers NEP.pptx
Political History of Pala dynasty Pala Rulers NEP.pptxPolitical History of Pala dynasty Pala Rulers NEP.pptx
Political History of Pala dynasty Pala Rulers NEP.pptx
Arya Mahila P. G. College, Banaras Hindu University, Varanasi, India.
 
Kenan Fellows Participants, Projects 2025-26 Cohort
Kenan Fellows Participants, Projects 2025-26 CohortKenan Fellows Participants, Projects 2025-26 Cohort
Kenan Fellows Participants, Projects 2025-26 Cohort
EducationNC
 
03#UNTAGGED. Generosity in architecture.
03#UNTAGGED. Generosity in architecture.03#UNTAGGED. Generosity in architecture.
03#UNTAGGED. Generosity in architecture.
MCH
 
To study Digestive system of insect.pptx
To study Digestive system of insect.pptxTo study Digestive system of insect.pptx
To study Digestive system of insect.pptx
Arshad Shaikh
 
Engage Donors Through Powerful Storytelling.pdf
Engage Donors Through Powerful Storytelling.pdfEngage Donors Through Powerful Storytelling.pdf
Engage Donors Through Powerful Storytelling.pdf
TechSoup
 
K12 Tableau Tuesday - Algebra Equity and Access in Atlanta Public Schools
K12 Tableau Tuesday  - Algebra Equity and Access in Atlanta Public SchoolsK12 Tableau Tuesday  - Algebra Equity and Access in Atlanta Public Schools
K12 Tableau Tuesday - Algebra Equity and Access in Atlanta Public Schools
dogden2
 
World war-1(Causes & impacts at a glance) PPT by Simanchala Sarab(BABed,sem-4...
World war-1(Causes & impacts at a glance) PPT by Simanchala Sarab(BABed,sem-4...World war-1(Causes & impacts at a glance) PPT by Simanchala Sarab(BABed,sem-4...
World war-1(Causes & impacts at a glance) PPT by Simanchala Sarab(BABed,sem-4...
larencebapu132
 
How to manage Multiple Warehouses for multiple floors in odoo point of sale
How to manage Multiple Warehouses for multiple floors in odoo point of saleHow to manage Multiple Warehouses for multiple floors in odoo point of sale
How to manage Multiple Warehouses for multiple floors in odoo point of sale
Celine George
 
Understanding P–N Junction Semiconductors: A Beginner’s Guide
Understanding P–N Junction Semiconductors: A Beginner’s GuideUnderstanding P–N Junction Semiconductors: A Beginner’s Guide
Understanding P–N Junction Semiconductors: A Beginner’s Guide
GS Virdi
 
Real GitHub Copilot Exam Dumps for Success
Real GitHub Copilot Exam Dumps for SuccessReal GitHub Copilot Exam Dumps for Success
Real GitHub Copilot Exam Dumps for Success
Mark Soia
 
SCI BIZ TECH QUIZ (OPEN) PRELIMS XTASY 2025.pptx
SCI BIZ TECH QUIZ (OPEN) PRELIMS XTASY 2025.pptxSCI BIZ TECH QUIZ (OPEN) PRELIMS XTASY 2025.pptx
SCI BIZ TECH QUIZ (OPEN) PRELIMS XTASY 2025.pptx
Ronisha Das
 
Sinhala_Male_Names.pdf Sinhala_Male_Name
Sinhala_Male_Names.pdf Sinhala_Male_NameSinhala_Male_Names.pdf Sinhala_Male_Name
Sinhala_Male_Names.pdf Sinhala_Male_Name
keshanf79
 
Exercise Physiology MCQS By DR. NASIR MUSTAFA
Exercise Physiology MCQS By DR. NASIR MUSTAFAExercise Physiology MCQS By DR. NASIR MUSTAFA
Exercise Physiology MCQS By DR. NASIR MUSTAFA
Dr. Nasir Mustafa
 
Presentation of the MIPLM subject matter expert Erdem Kaya
Presentation of the MIPLM subject matter expert Erdem KayaPresentation of the MIPLM subject matter expert Erdem Kaya
Presentation of the MIPLM subject matter expert Erdem Kaya
MIPLM
 
Contact Lens:::: An Overview.pptx.: Optometry
Contact Lens:::: An Overview.pptx.: OptometryContact Lens:::: An Overview.pptx.: Optometry
Contact Lens:::: An Overview.pptx.: Optometry
MushahidRaza8
 
How to Manage Purchase Alternatives in Odoo 18
How to Manage Purchase Alternatives in Odoo 18How to Manage Purchase Alternatives in Odoo 18
How to Manage Purchase Alternatives in Odoo 18
Celine George
 
Kenan Fellows Participants, Projects 2025-26 Cohort
Kenan Fellows Participants, Projects 2025-26 CohortKenan Fellows Participants, Projects 2025-26 Cohort
Kenan Fellows Participants, Projects 2025-26 Cohort
EducationNC
 
03#UNTAGGED. Generosity in architecture.
03#UNTAGGED. Generosity in architecture.03#UNTAGGED. Generosity in architecture.
03#UNTAGGED. Generosity in architecture.
MCH
 
To study Digestive system of insect.pptx
To study Digestive system of insect.pptxTo study Digestive system of insect.pptx
To study Digestive system of insect.pptx
Arshad Shaikh
 
Engage Donors Through Powerful Storytelling.pdf
Engage Donors Through Powerful Storytelling.pdfEngage Donors Through Powerful Storytelling.pdf
Engage Donors Through Powerful Storytelling.pdf
TechSoup
 
K12 Tableau Tuesday - Algebra Equity and Access in Atlanta Public Schools
K12 Tableau Tuesday  - Algebra Equity and Access in Atlanta Public SchoolsK12 Tableau Tuesday  - Algebra Equity and Access in Atlanta Public Schools
K12 Tableau Tuesday - Algebra Equity and Access in Atlanta Public Schools
dogden2
 

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