SlideShare a Scribd company logo
BY THEISA
ARRAY
C++ Arrays
Arrays are used to store multiple values in a single variable,
instead of declaring separate variables for each value.
 To declare an array, define the variable type, specify the
name of the array followed by square brackets and
specify the number of elements it should store:
Example:- string cars[4];
 We have now declared a variable that holds an array of four strings. To insert values to it, we can use an
array literal - place the values in a comma-separated list, inside curly braces:
Example:- string cars[4] = {"Volvo", "BMW", "Ford", "Mazda"};
 To create an array of three integers, you could write:
Example:- int myNum[3] = {10, 20, 30};
Access the Elements of an Array
 You access an array element by referring to the index number inside square brackets [].
This statement accesses the value of the first element in cars:
Example:- string cars[4] = {"Volvo", "BMW", "Ford", "Mazda"};
cout << cars[0];
// Outputs Volvo
Change an Array Element
 To change the value of a specific element, refer to the index number:
Example :- cars[0] = "Opel";
Example :- string cars[4] = {"Volvo", "BMW", "Ford", "Mazda"};
cars[0] = "Opel";
cout << cars[0];
// Now outputs Opel instead of Volvo
C++ Arrays and Loops
Loop Through an Array
You can loop through the array elements with the for loop.
The following example outputs all elements in the cars array:
Example:- string cars[5] =
{"Volvo", "BMW", "Ford", "Mazda", "Tesla"};
for (int i = 0; i < 5; i++) {
cout << cars[i] << "n";
}
The foreach Loop
 There is also a "for-each loop" (introduced in C++ version 11 (2011),
which is used exclusively to loop through elements in an array:
Syntax :- for (type variableName : arrayName) {
// code block to be executed
}
 The following example outputs all elements in an array, using a "for-
each loop":
Example : - int myNumbers[5] = {10, 20, 30, 40, 50};
for (int i : myNumbers) {
cout << i << "n";
}
Get the Size of an Array
 To get the size of an array, you can use the sizeof() operator:
Example :- int myNumbers[5] = {10, 20, 30, 40, 50};
cout << sizeof(myNumbers);
output:- 20
Why did the result show 20 instead of 5, when the array contains 5 elements?
It is because the sizeof() operator returns the size of a type in bytes. You
learned from the Data Types chapter that an int type is usually 4 bytes, so from
the example above, 4 x 5 (4 bytes x 5 elements) = 20 bytes.
 To find out how many elements an array has, you have to divide the size of
the array by the size of the data type it contains:
Example:- int myNumbers[5] = {10, 20, 30, 40, 50};
int getArrayLength = sizeof(myNumbers) / sizeof(int);
cout << getArrayLength;
output:- 5
CHAPTER 5 oop chapter 5 programming sem2

More Related Content

PPTX
Lecture 7
PPTX
Array In C++ programming object oriented programming
PPTX
Array,string structures. Best presentation pptx
PPTX
Arrays_in_c++.pptx
PPTX
Chapter-Five.pptx
PPT
02 c++ Array Pointer
PDF
1-Intoduction ------------- Array in C++
PDF
Lecture 7
Array In C++ programming object oriented programming
Array,string structures. Best presentation pptx
Arrays_in_c++.pptx
Chapter-Five.pptx
02 c++ Array Pointer
1-Intoduction ------------- Array in C++

Similar to CHAPTER 5 oop chapter 5 programming sem2 (20)

PPTX
Lecture 9
PPT
Array
PPTX
Chapter1.pptx
PPTX
Array and string in C++_093547 analysis.pptx
PDF
Arrays in C++
PPT
PPTX
C_Arrays(3)bzxhgvxgxg.xhjvxugvxuxuxuxvxugvx.pptx
PPTX
Arrays matrix 2020 ab
PPTX
arraytypes of array and pointer string in c++.pptx
PPTX
PPTX
PPTX
C++ lecture 04
PDF
array 2 (1)_merged.pdf
PDF
11. Programming(BS-phy6)-Lecture11+12 .pdf
PPT
C++ Arrays
PPT
C++ Arrays
PDF
c++ referesher 1.pdf
PPT
Pengaturcaraan asas
PDF
Loops and ArraysObjectivesArrays are a series of elements consi.pdf
PPTX
Arrays presentation by Mam Iqra Kanwal.pptx
Lecture 9
Array
Chapter1.pptx
Array and string in C++_093547 analysis.pptx
Arrays in C++
C_Arrays(3)bzxhgvxgxg.xhjvxugvxuxuxuxvxugvx.pptx
Arrays matrix 2020 ab
arraytypes of array and pointer string in c++.pptx
C++ lecture 04
array 2 (1)_merged.pdf
11. Programming(BS-phy6)-Lecture11+12 .pdf
C++ Arrays
C++ Arrays
c++ referesher 1.pdf
Pengaturcaraan asas
Loops and ArraysObjectivesArrays are a series of elements consi.pdf
Arrays presentation by Mam Iqra Kanwal.pptx
Ad

More from TSha7 (20)

PPTX
Computer_Graphics_Presentationa (1).pptx
PPTX
Guidelines and Examples.pptxGuidelines and Examples.pptx
PPTX
01-introduction-130924015121-phpapp02.pptx
PPTX
Core-Challenges-in-Supply-Chain-for-Minimising-Operational-Waste-in-the-Manuf...
PPTX
Computer-Graphics Fundamentals of Computer Graphics
PPTX
Fundamentalsss-of-Computer-Graphics.pptx
PPTX
electronicpaymentppt-140601022736-phpapp02.pptx
PPT
5-170401094214-1704010942145-170401094214.ppt
PDF
operating system in computer science .pdf
PDF
operating system in computer science ch05.pdf
PPTX
nursing researvh RESEARCH PROPOSAL SLIDE.pptx
PPTX
Streamlining Collaboration and Development - cloud computing .pptx
PPTX
CHAPTER 7 Legal Issues in E-Commerce: A Beginner's Guide
PPTX
CHAPTER 3 oop with programming java language
PPTX
CHAPTER 6 oop with c++ chapter programming
PDF
introduction to Python and Computer Programming.pdf
PDF
Lect 1b - Introduction to Pharmacy Informatics 1b.pdf
PDF
Lect 1a - Introduction to Pharmacy Informatics 1a.pdf
PPTX
APznzabZBYmM9xNVBHfOxhzhAYK6CVARVaOcFQRGQ_SDH3ZHiu4lPbmPUi4L1H3zAkRth3WEwZR2u...
PDF
bab 2 GEOGRAFI TINGKATAN TIGA MMALAYSIAA
Computer_Graphics_Presentationa (1).pptx
Guidelines and Examples.pptxGuidelines and Examples.pptx
01-introduction-130924015121-phpapp02.pptx
Core-Challenges-in-Supply-Chain-for-Minimising-Operational-Waste-in-the-Manuf...
Computer-Graphics Fundamentals of Computer Graphics
Fundamentalsss-of-Computer-Graphics.pptx
electronicpaymentppt-140601022736-phpapp02.pptx
5-170401094214-1704010942145-170401094214.ppt
operating system in computer science .pdf
operating system in computer science ch05.pdf
nursing researvh RESEARCH PROPOSAL SLIDE.pptx
Streamlining Collaboration and Development - cloud computing .pptx
CHAPTER 7 Legal Issues in E-Commerce: A Beginner's Guide
CHAPTER 3 oop with programming java language
CHAPTER 6 oop with c++ chapter programming
introduction to Python and Computer Programming.pdf
Lect 1b - Introduction to Pharmacy Informatics 1b.pdf
Lect 1a - Introduction to Pharmacy Informatics 1a.pdf
APznzabZBYmM9xNVBHfOxhzhAYK6CVARVaOcFQRGQ_SDH3ZHiu4lPbmPUi4L1H3zAkRth3WEwZR2u...
bab 2 GEOGRAFI TINGKATAN TIGA MMALAYSIAA
Ad

Recently uploaded (20)

PDF
Supply Chain Operations Speaking Notes -ICLT Program
PPTX
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
PDF
Mark Klimek Lecture Notes_240423 revision books _173037.pdf
PDF
Business Ethics Teaching Materials for college
PDF
Complications of Minimal Access Surgery at WLH
PPTX
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
PDF
FourierSeries-QuestionsWithAnswers(Part-A).pdf
PPTX
PPH.pptx obstetrics and gynecology in nursing
PPTX
Cell Structure & Organelles in detailed.
PDF
Pre independence Education in Inndia.pdf
PDF
Abdominal Access Techniques with Prof. Dr. R K Mishra
PPTX
master seminar digital applications in india
PDF
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
PPTX
Final Presentation General Medicine 03-08-2024.pptx
PDF
STATICS OF THE RIGID BODIES Hibbelers.pdf
PPTX
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
PPTX
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
PDF
O7-L3 Supply Chain Operations - ICLT Program
PDF
2.FourierTransform-ShortQuestionswithAnswers.pdf
PDF
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
Supply Chain Operations Speaking Notes -ICLT Program
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
Mark Klimek Lecture Notes_240423 revision books _173037.pdf
Business Ethics Teaching Materials for college
Complications of Minimal Access Surgery at WLH
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
FourierSeries-QuestionsWithAnswers(Part-A).pdf
PPH.pptx obstetrics and gynecology in nursing
Cell Structure & Organelles in detailed.
Pre independence Education in Inndia.pdf
Abdominal Access Techniques with Prof. Dr. R K Mishra
master seminar digital applications in india
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
Final Presentation General Medicine 03-08-2024.pptx
STATICS OF THE RIGID BODIES Hibbelers.pdf
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
O7-L3 Supply Chain Operations - ICLT Program
2.FourierTransform-ShortQuestionswithAnswers.pdf
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf

CHAPTER 5 oop chapter 5 programming sem2

  • 2. ARRAY C++ Arrays Arrays are used to store multiple values in a single variable, instead of declaring separate variables for each value.  To declare an array, define the variable type, specify the name of the array followed by square brackets and specify the number of elements it should store: Example:- string cars[4];
  • 3.  We have now declared a variable that holds an array of four strings. To insert values to it, we can use an array literal - place the values in a comma-separated list, inside curly braces: Example:- string cars[4] = {"Volvo", "BMW", "Ford", "Mazda"};  To create an array of three integers, you could write: Example:- int myNum[3] = {10, 20, 30}; Access the Elements of an Array  You access an array element by referring to the index number inside square brackets []. This statement accesses the value of the first element in cars: Example:- string cars[4] = {"Volvo", "BMW", "Ford", "Mazda"}; cout << cars[0]; // Outputs Volvo Change an Array Element  To change the value of a specific element, refer to the index number: Example :- cars[0] = "Opel"; Example :- string cars[4] = {"Volvo", "BMW", "Ford", "Mazda"}; cars[0] = "Opel"; cout << cars[0]; // Now outputs Opel instead of Volvo
  • 4. C++ Arrays and Loops Loop Through an Array You can loop through the array elements with the for loop. The following example outputs all elements in the cars array: Example:- string cars[5] = {"Volvo", "BMW", "Ford", "Mazda", "Tesla"}; for (int i = 0; i < 5; i++) { cout << cars[i] << "n"; }
  • 5. The foreach Loop  There is also a "for-each loop" (introduced in C++ version 11 (2011), which is used exclusively to loop through elements in an array: Syntax :- for (type variableName : arrayName) { // code block to be executed }  The following example outputs all elements in an array, using a "for- each loop": Example : - int myNumbers[5] = {10, 20, 30, 40, 50}; for (int i : myNumbers) { cout << i << "n"; }
  • 6. Get the Size of an Array  To get the size of an array, you can use the sizeof() operator: Example :- int myNumbers[5] = {10, 20, 30, 40, 50}; cout << sizeof(myNumbers); output:- 20 Why did the result show 20 instead of 5, when the array contains 5 elements? It is because the sizeof() operator returns the size of a type in bytes. You learned from the Data Types chapter that an int type is usually 4 bytes, so from the example above, 4 x 5 (4 bytes x 5 elements) = 20 bytes.  To find out how many elements an array has, you have to divide the size of the array by the size of the data type it contains: Example:- int myNumbers[5] = {10, 20, 30, 40, 50}; int getArrayLength = sizeof(myNumbers) / sizeof(int); cout << getArrayLength; output:- 5