SlideShare a Scribd company logo
Arrays
C++ LECTURE 04
What is an array?
•Arrays are a series of elements (variables) of the same type placed consecutively
in memory that can be individually referenced by adding an index to a unique
name.
• A typical declaration for an array in C++ is:
◦ type name [elements];
◦ where type is a valid object type (int, float...), name is a valid variable identifier and
the elements field, that is enclosed within brackets [], specifies how many of these
elements the array contains.
◦ Eg ; int marks[5];
Initialize an Array
int marks [5] = { 16, 20, 77, 40, 71 };
Int marks[ ] = {16,20,77,40,71}
Access to the values of an Array
In any point of the program in which the array is visible we can access
individually anyone of its values for reading or modifying as if it was a normal
variable.
The format is the following:
◦ name[index]
Eg: marks[2] = 20;
◦ a = marks[2]; i.e a =20;
Example
// arrays example
#include <iostream>
Using namespace std;
int value [] = {16, 2, 77, 40, 12071};
int n, result=0;
int main () {
for ( n=0 ; n<5 ; n++ ) {
result += value[n]; }
cout << result;
Return 0;
}
C++ lecture 04
Example 2:
C++ Program to store 5 numbers entered by user in an array and display first and last number only.
Multidimensional Arrays
•Multidimensional arrays can be described as arrays of arrays.
•For example, a bi dimensional array can be imagined as a bi dimensional
table of a uniform concrete data type.
Arrays as parameters
// arrays as parameters
#include <iostream>
Using namespace std;
void printarray (int arg[], int length) {
for (int n=0; n<length; n++)
cout << arg[n] << " ";
cout << "n";
}
int main () {
int firstarray[] = {5, 10, 15};
int secondarray[] = {2, 4, 6, 8, 10};
printarray (firstarray,3);
printarray (secondarray,5);
return 0; }
Strings of Characters
•strings of characters, that allow us to represent successions of characters,
like words, sentences, names, texts
•For example, the following array (or string of characters):
•char jenny [20];
Initialization of strings
•char mystring[] = { 'H', 'e', 'l', 'l', 'o', '0' };
•In this case we would have declared a string of characters (array) of 6
elements of type char initialized with the characters that compose Hello plus
a null character '0'.
•char mystring [] = { 'H', 'e', 'l', 'l', 'o', '0' };
•char mystring [] = "Hello";

More Related Content

PPTX
C++ programming (Array)
PPTX
Array in c++
PPTX
One dimensional arrays
PPT
Array in c
PPTX
Array in C
PPSX
C Programming : Arrays
PPTX
Array Introduction One-dimensional array Multidimensional array
PPTX
Arrays in c
C++ programming (Array)
Array in c++
One dimensional arrays
Array in c
Array in C
C Programming : Arrays
Array Introduction One-dimensional array Multidimensional array
Arrays in c

What's hot (20)

PPTX
Introduction to Array ppt
PPTX
2- Dimensional Arrays
PPTX
Programming in c Arrays
PPTX
Array C programming
PPTX
Arrays in C language
PPT
C programming , array 2020
PPT
1 D Arrays in C++
PPTX
concept of Array, 1D & 2D array
PPTX
PPTX
Array in c language
PPT
One dimensional 2
PPT
Array
PPTX
Arrays in c language
PPTX
Array in c language
PPT
Java: Introduction to Arrays
PPT
C++ Arrays
PPTX
2D Array
PPT
Arrays in c
PDF
PPTX
Arrays In C++
Introduction to Array ppt
2- Dimensional Arrays
Programming in c Arrays
Array C programming
Arrays in C language
C programming , array 2020
1 D Arrays in C++
concept of Array, 1D & 2D array
Array in c language
One dimensional 2
Array
Arrays in c language
Array in c language
Java: Introduction to Arrays
C++ Arrays
2D Array
Arrays in c
Arrays In C++
Ad

Viewers also liked (20)

PPTX
C++ lecture 03
PDF
C++ ARRAY WITH EXAMPLES
PPTX
Array in C
PPT
PPTX
C++ lecture 01
PPTX
C++ lecture 02
PPT
Chapter 13 - Inheritance and Polymorphism
PDF
CP Handout#2
PPT
8.3 program structure (1 hour)
PPTX
Constructs (Programming Methodology)
PDF
CP Handout#5
DOC
Java programming lab assignments
PPTX
Pf cs102 programming-9 [pointers]
PPT
PPT
Apclass (2)
PDF
Chapter 2 - Structure of C++ Program
PPT
Chapter 12 - File Input and Output
PDF
Array in Java
PPTX
Loop c++
PPTX
Lecture 02 networking
C++ lecture 03
C++ ARRAY WITH EXAMPLES
Array in C
C++ lecture 01
C++ lecture 02
Chapter 13 - Inheritance and Polymorphism
CP Handout#2
8.3 program structure (1 hour)
Constructs (Programming Methodology)
CP Handout#5
Java programming lab assignments
Pf cs102 programming-9 [pointers]
Apclass (2)
Chapter 2 - Structure of C++ Program
Chapter 12 - File Input and Output
Array in Java
Loop c++
Lecture 02 networking
Ad

Similar to C++ lecture 04 (20)

PPT
PDF
PPT
C++ Arrays
PDF
Arrays in C++
PPTX
Array,string structures. Best presentation pptx
PPTX
Chapter-Five.pptx
PPTX
ARRAYSCPP.pptx
PPTX
5 ARRAYS AND STRINGSjiuojhiooioiiouioi.pptx
PPTX
Arrays in C++ in Tamil - TNSCERT SYLLABUS PPT
PPT
CHAPTER-5.ppt
PDF
Array Data Structure for programing language
PPTX
C_Arrays(3)bzxhgvxgxg.xhjvxugvxuxuxuxvxugvx.pptx
PPTX
Array and string in C++_093547 analysis.pptx
PPTX
ARRAYS.pptx
PPTX
Arrays_in_c++.pptx
PPTX
Module1_arrays.pptx
PPTX
Arrays presentation by Mam Iqra Kanwal.pptx
PPTX
Array In C++ programming object oriented programming
PPTX
Data structure.pptx
PPTX
"Understanding Arrays in Data Structures: A Beginners Guide."
C++ Arrays
Arrays in C++
Array,string structures. Best presentation pptx
Chapter-Five.pptx
ARRAYSCPP.pptx
5 ARRAYS AND STRINGSjiuojhiooioiiouioi.pptx
Arrays in C++ in Tamil - TNSCERT SYLLABUS PPT
CHAPTER-5.ppt
Array Data Structure for programing language
C_Arrays(3)bzxhgvxgxg.xhjvxugvxuxuxuxvxugvx.pptx
Array and string in C++_093547 analysis.pptx
ARRAYS.pptx
Arrays_in_c++.pptx
Module1_arrays.pptx
Arrays presentation by Mam Iqra Kanwal.pptx
Array In C++ programming object oriented programming
Data structure.pptx
"Understanding Arrays in Data Structures: A Beginners Guide."

Recently uploaded (20)

PDF
ETO & MEO Certificate of Competency Questions and Answers
DOCX
573137875-Attendance-Management-System-original
PPT
Project quality management in manufacturing
PPTX
ANIMAL INTERVENTION WARNING SYSTEM (4).pptx
PDF
Embodied AI: Ushering in the Next Era of Intelligent Systems
PPTX
Fluid Mechanics, Module 3: Basics of Fluid Mechanics
PPTX
Simulation of electric circuit laws using tinkercad.pptx
PDF
Geotechnical Engineering, Soil mechanics- Soil Testing.pdf
PDF
Arduino robotics embedded978-1-4302-3184-4.pdf
PPTX
MCN 401 KTU-2019-PPE KITS-MODULE 2.pptx
PPTX
web development for engineering and engineering
PPTX
Recipes for Real Time Voice AI WebRTC, SLMs and Open Source Software.pptx
PPTX
24AI201_AI_Unit_4 (1).pptx Artificial intelligence
PDF
Queuing formulas to evaluate throughputs and servers
PPTX
CH1 Production IntroductoryConcepts.pptx
PPTX
Internship_Presentation_Final engineering.pptx
PDF
composite construction of structures.pdf
PPTX
Lesson 3_Tessellation.pptx finite Mathematics
PPTX
Geodesy 1.pptx...............................................
ETO & MEO Certificate of Competency Questions and Answers
573137875-Attendance-Management-System-original
Project quality management in manufacturing
ANIMAL INTERVENTION WARNING SYSTEM (4).pptx
Embodied AI: Ushering in the Next Era of Intelligent Systems
Fluid Mechanics, Module 3: Basics of Fluid Mechanics
Simulation of electric circuit laws using tinkercad.pptx
Geotechnical Engineering, Soil mechanics- Soil Testing.pdf
Arduino robotics embedded978-1-4302-3184-4.pdf
MCN 401 KTU-2019-PPE KITS-MODULE 2.pptx
web development for engineering and engineering
Recipes for Real Time Voice AI WebRTC, SLMs and Open Source Software.pptx
24AI201_AI_Unit_4 (1).pptx Artificial intelligence
Queuing formulas to evaluate throughputs and servers
CH1 Production IntroductoryConcepts.pptx
Internship_Presentation_Final engineering.pptx
composite construction of structures.pdf
Lesson 3_Tessellation.pptx finite Mathematics
Geodesy 1.pptx...............................................

C++ lecture 04

  • 2. What is an array? •Arrays are a series of elements (variables) of the same type placed consecutively in memory that can be individually referenced by adding an index to a unique name. • A typical declaration for an array in C++ is: ◦ type name [elements]; ◦ where type is a valid object type (int, float...), name is a valid variable identifier and the elements field, that is enclosed within brackets [], specifies how many of these elements the array contains. ◦ Eg ; int marks[5];
  • 3. Initialize an Array int marks [5] = { 16, 20, 77, 40, 71 }; Int marks[ ] = {16,20,77,40,71} Access to the values of an Array In any point of the program in which the array is visible we can access individually anyone of its values for reading or modifying as if it was a normal variable. The format is the following: ◦ name[index] Eg: marks[2] = 20; ◦ a = marks[2]; i.e a =20;
  • 4. Example // arrays example #include <iostream> Using namespace std; int value [] = {16, 2, 77, 40, 12071}; int n, result=0; int main () { for ( n=0 ; n<5 ; n++ ) { result += value[n]; } cout << result; Return 0; }
  • 6. Example 2: C++ Program to store 5 numbers entered by user in an array and display first and last number only.
  • 7. Multidimensional Arrays •Multidimensional arrays can be described as arrays of arrays. •For example, a bi dimensional array can be imagined as a bi dimensional table of a uniform concrete data type.
  • 8. Arrays as parameters // arrays as parameters #include <iostream> Using namespace std; void printarray (int arg[], int length) { for (int n=0; n<length; n++) cout << arg[n] << " "; cout << "n"; } int main () { int firstarray[] = {5, 10, 15}; int secondarray[] = {2, 4, 6, 8, 10}; printarray (firstarray,3); printarray (secondarray,5); return 0; }
  • 9. Strings of Characters •strings of characters, that allow us to represent successions of characters, like words, sentences, names, texts •For example, the following array (or string of characters): •char jenny [20];
  • 10. Initialization of strings •char mystring[] = { 'H', 'e', 'l', 'l', 'o', '0' }; •In this case we would have declared a string of characters (array) of 6 elements of type char initialized with the characters that compose Hello plus a null character '0'. •char mystring [] = { 'H', 'e', 'l', 'l', 'o', '0' }; •char mystring [] = "Hello";