SlideShare a Scribd company logo
An Introduction to Programming with C++
Eighth Edition
Lesson 11:
One – Dimensional Array
Chapter Objectives
© 2016 Cengage Learning®. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in
whole or in part.
An Introduction to Programming with C++, Eighth Edition 2
• Declare and initialize a one-dimensional array
• Enter data into a one-dimensional array
• Display the contents of a one-dimensional array
• Pass a one-dimensional array to a function
• Calculate the total and average of the values in a one-
dimensional array
• Search a one-dimensional array
• Access an individual element in a one-dimensional array
• Find the highest value in a one-dimensional array
• Use parallel one-dimensional arrays
• Explain the bubble sort algorithm
The Concept of Array
You already are familiar with the concept of grouping.
The clothes in your closet are probably separated into
groups, such as coats, sweaters, shirts, and so on.
Grouping your clothes in this manner allows you to easily
locate your favorite sweater because you only need to
look through the sweater group rather than through the
entire closet. You may also have the songs on your MP3
player grouped by either music type or artist. If the songs
are grouped by artist, it will take only a few seconds to
find all of your Katy Perry songs and, depending on the
number of Katy Perry songs you own, only a short time
after that to locate a particular song.
What is an Array?
• When you group together related variables that
have the same data type, the group is referred to
as an array of variables or, more simply, an
ARRAY
• An array is a collection of one or more values of the
same type. Each value is called an element of the
array. The elements of the array share the same
variable name but each element has its own unique
index number (also known as a subscript). An array
can be of any type, For example: int, float, char etc. If
an array is of type int then it's elements must be of
type int only.
One – Dimensional Array
Conceptually you can think of a one-dimensional
array as a row, where elements are stored one after
another.
Syntax: datatype array_name[size];
datatype: It denotes the type of the
elements in the array.
array_name: Name of the array. It must be
a valid identifier.
size: Number of elements an array can
hold. here are some example of array
declarations:
Declaring and Initializing a one
dimensional array.
You can initialize the array elements at the same time
you declare the array. You do this by entering one or
more values, separated by commas, in the
initialValues section of the syntax. You enclose the
initialValues section in braces ({}),like
datatype array_name[size] = {1,2,3}
Populating the array- is the assigning initial values to
an array.
Declaring and Initializing a one
dimensional array.
Declaring and Initializing a one
dimensional array.
Declaring and Initializing a one
dimensional array.
Entering Data into a One-
Dimensional Array
Note:
Subscript also
called as index or
number of
element
Entering Data into a One-
Dimensional Array
Note:
Subscript also called as index or
number of element
Entering Data into a One-
Dimensional Array
• You can also use the extraction operator to store data in an array
element, as shown in the syntax and examples in Figure 11-4. (The
arrays in Figure 11-4 were declared earlier in Figure 11-2.)
• The cin statement in Example 1 stores the user’s entry in the first
element in the letters array, replacing the element’s existing data.
• Example 2 contains a for loop that repeats its instructions four times:
once for each element in the sales array.
• The loop instructions prompt the user to enter a sales amount and
then store the user’s response in the current element.
• Example 3 contains a while loop that repeats its instructions for each
of the six elements in the numbers array.
• The loop instructions prompt the user to enter an integer and then
store the user’s response in the current element.
Use the extraction operator to store
data in an array element
Use the extraction operator to store
data in an array element
Displaying the Contents of
a One-Dimensional Array
To display the contents of an array, you need to access each of its
elements. You do this using a loop along with a counter variable that
keeps track of each subscript in the array.
Displaying the Contents of
a One-Dimensional Array
Example Program(Calories
Program)
Note:
Try to copy the
code and try to
debug
Example Program(Calories
Program)
Possible Output:
Passing a One-Dimensional Array
to a Function
Passing a One-Dimensional Array
to a Function
Continue:
Passing a One-Dimensional Array
to a Function (Calculating a Total and an Average)
OUTPUT:
Passing a One-Dimensional Array
to a Function (Finding the highest value)
Displays the annual membership
fee
Key Terms:
• Array—a group of related variables that have the
same name and data type
• Bubble sort—one of many sorting algorithms
used to sort small arrays; works by comparing
adjacent array elements and swapping the ones
that are out of order
• Elements—the variables in an array One-
dimensional array—an array whose elements are
identified by a unique subscript
• Parallel arrays—two or more arrays whose
elements are related by their corresponding
position (subscript) in the arrays
Key Terms:
• Populating the array—the process of initializing
the elements in an array
• Scalar variable—another term for a simple
variable
• Simple variable—a variable that is unrelated to
any other variable in the computer’s internal
memory; also called a scalar variable
• Sorting—the process of arranging data in a
specific order
• Subscript—a unique number that identifies the
position of an element in an array

More Related Content

What's hot

Methods in C#
Methods in C#Methods in C#
Methods in C#
Prasanna Kumar SM
 
Bootstrap
BootstrapBootstrap
Bootstrap
Jadson Santos
 
Content provider in_android
Content provider in_androidContent provider in_android
Content provider in_android
PRITI TELMORE
 
Bridge Design Pattern
Bridge Design PatternBridge Design Pattern
Bridge Design Pattern
Shahriar Hyder
 
Getting Started with C++
Getting Started with C++Getting Started with C++
Getting Started with C++
Praveen M Jigajinni
 
Web services
Web servicesWeb services
Web services
Akshay Ballarpure
 
Unit 4 Foc
Unit 4 FocUnit 4 Foc
Unit 4 Foc
JAYA
 
Design Patterns (Examples in .NET)
Design Patterns (Examples in .NET)Design Patterns (Examples in .NET)
Design Patterns (Examples in .NET)
Aniruddha Chakrabarti
 
Javascript variables and datatypes
Javascript variables and datatypesJavascript variables and datatypes
Javascript variables and datatypesVarun C M
 
Introduction to numpy Session 1
Introduction to numpy Session 1Introduction to numpy Session 1
Introduction to numpy Session 1
Jatin Miglani
 
Method overloading
Method overloadingMethod overloading
Method overloading
Lovely Professional University
 
State chart diagram
State chart diagramState chart diagram
State chart diagram
Preeti Mishra
 
Arrays & Strings
Arrays & StringsArrays & Strings
Arrays & Strings
Munazza-Mah-Jabeen
 
CSS notes
CSS notesCSS notes
CSS notes
Rajendra Prasad
 
C++ programming (Array)
C++ programming (Array)C++ programming (Array)
C++ programming (Array)
طارق بالحارث
 
Function in C
Function in CFunction in C
Function in C
Dr. Abhineet Anand
 
Web Technology End semester Examination Questions
Web Technology End semester Examination QuestionsWeb Technology End semester Examination Questions
Web Technology End semester Examination Questions
Ansuman Mahapatra
 
Dynamic memory allocation
Dynamic memory allocationDynamic memory allocation
Dynamic memory allocation
Viji B
 
Advanced Software Engineering.ppt
Advanced Software Engineering.pptAdvanced Software Engineering.ppt
Advanced Software Engineering.ppt
Rvishnupriya2
 

What's hot (20)

Methods in C#
Methods in C#Methods in C#
Methods in C#
 
Bootstrap
BootstrapBootstrap
Bootstrap
 
Content provider in_android
Content provider in_androidContent provider in_android
Content provider in_android
 
Bridge Design Pattern
Bridge Design PatternBridge Design Pattern
Bridge Design Pattern
 
Getting Started with C++
Getting Started with C++Getting Started with C++
Getting Started with C++
 
Web services
Web servicesWeb services
Web services
 
Unit 4 Foc
Unit 4 FocUnit 4 Foc
Unit 4 Foc
 
Design Patterns (Examples in .NET)
Design Patterns (Examples in .NET)Design Patterns (Examples in .NET)
Design Patterns (Examples in .NET)
 
Javascript variables and datatypes
Javascript variables and datatypesJavascript variables and datatypes
Javascript variables and datatypes
 
Introduction to numpy Session 1
Introduction to numpy Session 1Introduction to numpy Session 1
Introduction to numpy Session 1
 
Method overloading
Method overloadingMethod overloading
Method overloading
 
State chart diagram
State chart diagramState chart diagram
State chart diagram
 
Arrays & Strings
Arrays & StringsArrays & Strings
Arrays & Strings
 
CSS notes
CSS notesCSS notes
CSS notes
 
C++ programming (Array)
C++ programming (Array)C++ programming (Array)
C++ programming (Array)
 
Function in C
Function in CFunction in C
Function in C
 
Web Technology End semester Examination Questions
Web Technology End semester Examination QuestionsWeb Technology End semester Examination Questions
Web Technology End semester Examination Questions
 
Lesson 1 php syntax
Lesson 1   php syntaxLesson 1   php syntax
Lesson 1 php syntax
 
Dynamic memory allocation
Dynamic memory allocationDynamic memory allocation
Dynamic memory allocation
 
Advanced Software Engineering.ppt
Advanced Software Engineering.pptAdvanced Software Engineering.ppt
Advanced Software Engineering.ppt
 

Similar to Lesson 11 one dimensional array

Ppt chapter09
Ppt chapter09Ppt chapter09
Ppt chapter09
Richard Styner
 
ARRAYS.pptx
ARRAYS.pptxARRAYS.pptx
Arrays.pptx
 Arrays.pptx Arrays.pptx
Arrays.pptx
PankajKumar497975
 
Arrays in programming
Arrays in programmingArrays in programming
Arrays in programming
TaseerRao
 
0-Slot18-19-20-ContiguousStorage.pdf
0-Slot18-19-20-ContiguousStorage.pdf0-Slot18-19-20-ContiguousStorage.pdf
0-Slot18-19-20-ContiguousStorage.pdf
ssusere19c741
 
Array andfunction
Array andfunctionArray andfunction
Array andfunction
Girmachew Tilahun
 
Arrays Basics
Arrays BasicsArrays Basics
Arrays Basics
Nikhil Pandit
 
ppt on arrays in c programming language.pptx
ppt on arrays in c programming language.pptxppt on arrays in c programming language.pptx
ppt on arrays in c programming language.pptx
AmanRai352102
 
Unit-4 (Scope Rules and Arrays).pptx for
Unit-4 (Scope Rules and Arrays).pptx forUnit-4 (Scope Rules and Arrays).pptx for
Unit-4 (Scope Rules and Arrays).pptx for
pattinsonhenry524
 
Acm aleppo cpc training seventh session
Acm aleppo cpc training seventh sessionAcm aleppo cpc training seventh session
Acm aleppo cpc training seventh session
Ahmad Bashar Eter
 
Mod 12
Mod 12Mod 12
Mod 12
obrienduke
 
Week 11.pptx
Week 11.pptxWeek 11.pptx
Week 11.pptx
CruiseCH
 
Array ppt
Array pptArray ppt
Array ppt
Kaushal Mehta
 
Array.pdf
Array.pdfArray.pdf
Array.pdf
DEEPAK948083
 
Data structures and algorithms arrays
Data structures and algorithms   arraysData structures and algorithms   arrays
Data structures and algorithms arrays
chauhankapil
 
Arrays In C++
Arrays In C++Arrays In C++
Arrays In C++
Awais Alam
 
Ap Power Point Chpt6
Ap Power Point Chpt6Ap Power Point Chpt6
Ap Power Point Chpt6dplunkett
 
Arrays
ArraysArrays
Arrays
ViniVini48
 

Similar to Lesson 11 one dimensional array (20)

Ppt lesson 12
Ppt lesson 12Ppt lesson 12
Ppt lesson 12
 
Ppt chapter09
Ppt chapter09Ppt chapter09
Ppt chapter09
 
ARRAYS.pptx
ARRAYS.pptxARRAYS.pptx
ARRAYS.pptx
 
Arrays.pptx
 Arrays.pptx Arrays.pptx
Arrays.pptx
 
Arrays in programming
Arrays in programmingArrays in programming
Arrays in programming
 
0-Slot18-19-20-ContiguousStorage.pdf
0-Slot18-19-20-ContiguousStorage.pdf0-Slot18-19-20-ContiguousStorage.pdf
0-Slot18-19-20-ContiguousStorage.pdf
 
Array andfunction
Array andfunctionArray andfunction
Array andfunction
 
Arrays Basics
Arrays BasicsArrays Basics
Arrays Basics
 
ppt on arrays in c programming language.pptx
ppt on arrays in c programming language.pptxppt on arrays in c programming language.pptx
ppt on arrays in c programming language.pptx
 
Unit-4 (Scope Rules and Arrays).pptx for
Unit-4 (Scope Rules and Arrays).pptx forUnit-4 (Scope Rules and Arrays).pptx for
Unit-4 (Scope Rules and Arrays).pptx for
 
Acm aleppo cpc training seventh session
Acm aleppo cpc training seventh sessionAcm aleppo cpc training seventh session
Acm aleppo cpc training seventh session
 
Mod 12
Mod 12Mod 12
Mod 12
 
Week 11.pptx
Week 11.pptxWeek 11.pptx
Week 11.pptx
 
Array ppt
Array pptArray ppt
Array ppt
 
Array.pdf
Array.pdfArray.pdf
Array.pdf
 
Data structures and algorithms arrays
Data structures and algorithms   arraysData structures and algorithms   arrays
Data structures and algorithms arrays
 
Java arrays (1)
Java arrays (1)Java arrays (1)
Java arrays (1)
 
Arrays In C++
Arrays In C++Arrays In C++
Arrays In C++
 
Ap Power Point Chpt6
Ap Power Point Chpt6Ap Power Point Chpt6
Ap Power Point Chpt6
 
Arrays
ArraysArrays
Arrays
 

More from MLG College of Learning, Inc (20)

PC111.Lesson2
PC111.Lesson2PC111.Lesson2
PC111.Lesson2
 
PC111.Lesson1
PC111.Lesson1PC111.Lesson1
PC111.Lesson1
 
PC111-lesson1.pptx
PC111-lesson1.pptxPC111-lesson1.pptx
PC111-lesson1.pptx
 
PC LEESOON 6.pptx
PC LEESOON 6.pptxPC LEESOON 6.pptx
PC LEESOON 6.pptx
 
PC 106 PPT-09.pptx
PC 106 PPT-09.pptxPC 106 PPT-09.pptx
PC 106 PPT-09.pptx
 
PC 106 PPT-07
PC 106 PPT-07PC 106 PPT-07
PC 106 PPT-07
 
PC 106 PPT-01
PC 106 PPT-01PC 106 PPT-01
PC 106 PPT-01
 
PC 106 PPT-06
PC 106 PPT-06PC 106 PPT-06
PC 106 PPT-06
 
PC 106 PPT-05
PC 106 PPT-05PC 106 PPT-05
PC 106 PPT-05
 
PC 106 Slide 04
PC 106 Slide 04PC 106 Slide 04
PC 106 Slide 04
 
PC 106 Slide no.02
PC 106 Slide no.02PC 106 Slide no.02
PC 106 Slide no.02
 
pc-106-slide-3
pc-106-slide-3pc-106-slide-3
pc-106-slide-3
 
PC 106 Slide 2
PC 106 Slide 2PC 106 Slide 2
PC 106 Slide 2
 
PC 106 Slide 1.pptx
PC 106 Slide 1.pptxPC 106 Slide 1.pptx
PC 106 Slide 1.pptx
 
Db2 characteristics of db ms
Db2 characteristics of db msDb2 characteristics of db ms
Db2 characteristics of db ms
 
Db1 introduction
Db1 introductionDb1 introduction
Db1 introduction
 
Lesson 3.2
Lesson 3.2Lesson 3.2
Lesson 3.2
 
Lesson 3.1
Lesson 3.1Lesson 3.1
Lesson 3.1
 
Lesson 1.6
Lesson 1.6Lesson 1.6
Lesson 1.6
 
Lesson 3.2
Lesson 3.2Lesson 3.2
Lesson 3.2
 

Recently uploaded

1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx
JosvitaDsouza2
 
Home assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdfHome assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdf
Tamralipta Mahavidyalaya
 
CACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdfCACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdf
camakaiclarkmusic
 
Model Attribute Check Company Auto Property
Model Attribute  Check Company Auto PropertyModel Attribute  Check Company Auto Property
Model Attribute Check Company Auto Property
Celine George
 
A Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in EducationA Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in Education
Peter Windle
 
Thesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.pptThesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.ppt
EverAndrsGuerraGuerr
 
678020731-Sumas-y-Restas-Para-Colorear.pdf
678020731-Sumas-y-Restas-Para-Colorear.pdf678020731-Sumas-y-Restas-Para-Colorear.pdf
678020731-Sumas-y-Restas-Para-Colorear.pdf
CarlosHernanMontoyab2
 
Unit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdfUnit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdf
Thiyagu K
 
2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...
Sandy Millin
 
Palestine last event orientationfvgnh .pptx
Palestine last event orientationfvgnh .pptxPalestine last event orientationfvgnh .pptx
Palestine last event orientationfvgnh .pptx
RaedMohamed3
 
Guidance_and_Counselling.pdf B.Ed. 4th Semester
Guidance_and_Counselling.pdf B.Ed. 4th SemesterGuidance_and_Counselling.pdf B.Ed. 4th Semester
Guidance_and_Counselling.pdf B.Ed. 4th Semester
Atul Kumar Singh
 
The approach at University of Liverpool.pptx
The approach at University of Liverpool.pptxThe approach at University of Liverpool.pptx
The approach at University of Liverpool.pptx
Jisc
 
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
Nguyen Thanh Tu Collection
 
The geography of Taylor Swift - some ideas
The geography of Taylor Swift - some ideasThe geography of Taylor Swift - some ideas
The geography of Taylor Swift - some ideas
GeoBlogs
 
Overview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with MechanismOverview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with Mechanism
DeeptiGupta154
 
The Roman Empire A Historical Colossus.pdf
The Roman Empire A Historical Colossus.pdfThe Roman Empire A Historical Colossus.pdf
The Roman Empire A Historical Colossus.pdf
kaushalkr1407
 
Chapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptxChapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptx
Mohd Adib Abd Muin, Senior Lecturer at Universiti Utara Malaysia
 
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCECLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
BhavyaRajput3
 
"Protectable subject matters, Protection in biotechnology, Protection of othe...
"Protectable subject matters, Protection in biotechnology, Protection of othe..."Protectable subject matters, Protection in biotechnology, Protection of othe...
"Protectable subject matters, Protection in biotechnology, Protection of othe...
SACHIN R KONDAGURI
 
Instructions for Submissions thorugh G- Classroom.pptx
Instructions for Submissions thorugh G- Classroom.pptxInstructions for Submissions thorugh G- Classroom.pptx
Instructions for Submissions thorugh G- Classroom.pptx
Jheel Barad
 

Recently uploaded (20)

1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx
 
Home assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdfHome assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdf
 
CACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdfCACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdf
 
Model Attribute Check Company Auto Property
Model Attribute  Check Company Auto PropertyModel Attribute  Check Company Auto Property
Model Attribute Check Company Auto Property
 
A Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in EducationA Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in Education
 
Thesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.pptThesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.ppt
 
678020731-Sumas-y-Restas-Para-Colorear.pdf
678020731-Sumas-y-Restas-Para-Colorear.pdf678020731-Sumas-y-Restas-Para-Colorear.pdf
678020731-Sumas-y-Restas-Para-Colorear.pdf
 
Unit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdfUnit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdf
 
2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...
 
Palestine last event orientationfvgnh .pptx
Palestine last event orientationfvgnh .pptxPalestine last event orientationfvgnh .pptx
Palestine last event orientationfvgnh .pptx
 
Guidance_and_Counselling.pdf B.Ed. 4th Semester
Guidance_and_Counselling.pdf B.Ed. 4th SemesterGuidance_and_Counselling.pdf B.Ed. 4th Semester
Guidance_and_Counselling.pdf B.Ed. 4th Semester
 
The approach at University of Liverpool.pptx
The approach at University of Liverpool.pptxThe approach at University of Liverpool.pptx
The approach at University of Liverpool.pptx
 
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
 
The geography of Taylor Swift - some ideas
The geography of Taylor Swift - some ideasThe geography of Taylor Swift - some ideas
The geography of Taylor Swift - some ideas
 
Overview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with MechanismOverview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with Mechanism
 
The Roman Empire A Historical Colossus.pdf
The Roman Empire A Historical Colossus.pdfThe Roman Empire A Historical Colossus.pdf
The Roman Empire A Historical Colossus.pdf
 
Chapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptxChapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptx
 
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCECLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
 
"Protectable subject matters, Protection in biotechnology, Protection of othe...
"Protectable subject matters, Protection in biotechnology, Protection of othe..."Protectable subject matters, Protection in biotechnology, Protection of othe...
"Protectable subject matters, Protection in biotechnology, Protection of othe...
 
Instructions for Submissions thorugh G- Classroom.pptx
Instructions for Submissions thorugh G- Classroom.pptxInstructions for Submissions thorugh G- Classroom.pptx
Instructions for Submissions thorugh G- Classroom.pptx
 

Lesson 11 one dimensional array

  • 1. An Introduction to Programming with C++ Eighth Edition Lesson 11: One – Dimensional Array
  • 2. Chapter Objectives © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. An Introduction to Programming with C++, Eighth Edition 2 • Declare and initialize a one-dimensional array • Enter data into a one-dimensional array • Display the contents of a one-dimensional array • Pass a one-dimensional array to a function • Calculate the total and average of the values in a one- dimensional array • Search a one-dimensional array • Access an individual element in a one-dimensional array • Find the highest value in a one-dimensional array • Use parallel one-dimensional arrays • Explain the bubble sort algorithm
  • 3. The Concept of Array You already are familiar with the concept of grouping. The clothes in your closet are probably separated into groups, such as coats, sweaters, shirts, and so on. Grouping your clothes in this manner allows you to easily locate your favorite sweater because you only need to look through the sweater group rather than through the entire closet. You may also have the songs on your MP3 player grouped by either music type or artist. If the songs are grouped by artist, it will take only a few seconds to find all of your Katy Perry songs and, depending on the number of Katy Perry songs you own, only a short time after that to locate a particular song.
  • 4. What is an Array? • When you group together related variables that have the same data type, the group is referred to as an array of variables or, more simply, an ARRAY • An array is a collection of one or more values of the same type. Each value is called an element of the array. The elements of the array share the same variable name but each element has its own unique index number (also known as a subscript). An array can be of any type, For example: int, float, char etc. If an array is of type int then it's elements must be of type int only.
  • 5. One – Dimensional Array Conceptually you can think of a one-dimensional array as a row, where elements are stored one after another.
  • 6. Syntax: datatype array_name[size]; datatype: It denotes the type of the elements in the array. array_name: Name of the array. It must be a valid identifier. size: Number of elements an array can hold. here are some example of array declarations: Declaring and Initializing a one dimensional array.
  • 7. You can initialize the array elements at the same time you declare the array. You do this by entering one or more values, separated by commas, in the initialValues section of the syntax. You enclose the initialValues section in braces ({}),like datatype array_name[size] = {1,2,3} Populating the array- is the assigning initial values to an array. Declaring and Initializing a one dimensional array.
  • 8. Declaring and Initializing a one dimensional array.
  • 9. Declaring and Initializing a one dimensional array.
  • 10. Entering Data into a One- Dimensional Array Note: Subscript also called as index or number of element
  • 11. Entering Data into a One- Dimensional Array Note: Subscript also called as index or number of element
  • 12. Entering Data into a One- Dimensional Array • You can also use the extraction operator to store data in an array element, as shown in the syntax and examples in Figure 11-4. (The arrays in Figure 11-4 were declared earlier in Figure 11-2.) • The cin statement in Example 1 stores the user’s entry in the first element in the letters array, replacing the element’s existing data. • Example 2 contains a for loop that repeats its instructions four times: once for each element in the sales array. • The loop instructions prompt the user to enter a sales amount and then store the user’s response in the current element. • Example 3 contains a while loop that repeats its instructions for each of the six elements in the numbers array. • The loop instructions prompt the user to enter an integer and then store the user’s response in the current element.
  • 13. Use the extraction operator to store data in an array element
  • 14. Use the extraction operator to store data in an array element
  • 15. Displaying the Contents of a One-Dimensional Array To display the contents of an array, you need to access each of its elements. You do this using a loop along with a counter variable that keeps track of each subscript in the array.
  • 16. Displaying the Contents of a One-Dimensional Array
  • 17. Example Program(Calories Program) Note: Try to copy the code and try to debug
  • 19. Passing a One-Dimensional Array to a Function
  • 20. Passing a One-Dimensional Array to a Function Continue:
  • 21. Passing a One-Dimensional Array to a Function (Calculating a Total and an Average) OUTPUT:
  • 22. Passing a One-Dimensional Array to a Function (Finding the highest value)
  • 23. Displays the annual membership fee
  • 24. Key Terms: • Array—a group of related variables that have the same name and data type • Bubble sort—one of many sorting algorithms used to sort small arrays; works by comparing adjacent array elements and swapping the ones that are out of order • Elements—the variables in an array One- dimensional array—an array whose elements are identified by a unique subscript • Parallel arrays—two or more arrays whose elements are related by their corresponding position (subscript) in the arrays
  • 25. Key Terms: • Populating the array—the process of initializing the elements in an array • Scalar variable—another term for a simple variable • Simple variable—a variable that is unrelated to any other variable in the computer’s internal memory; also called a scalar variable • Sorting—the process of arranging data in a specific order • Subscript—a unique number that identifies the position of an element in an array