SlideShare a Scribd company logo
1 of 20
ARRAYS
INTRODUCTION
• Array a collection of a fixed number of
components wherein all of the components
have the same data type
• In a one-dimensional array, the
components are arranged in a list form
• Syntax for declaring a one-dimensional
array:
Example
• int num[5];
Defining Arrays
• When defining arrays, specify
–Name
–Type of array
–Number of elements
• arrayType arrayName[ numberOfElements ];
–Examples:
• int c[ 10 ];
• float myArray[ 3284 ];
Accessing Array Components
• General syntax:
where indexExp, called an index, is any expression whose value
is a nonnegative integer
• Index value specifies the position of the component
in the array
• [ ] is the array subscripting operator
• The array index always starts at 0
Accessing Array Components
One-Dimensional Arrays
• Some basic operations performed on a one-
dimensional array are:
– Initializing
– Inputting data
– Outputting data stored in an array
– Finding the largest and/or smallest element
• Each operation requires ability to step through
the elements of the array
• Easily accomplished by a loop
Processing One-Dimensional Arrays
• Consider the declaration
int list[100]; //array of size 100
int i;
• Using forloops to access array elements:
for (i = 0; i < 100; i++) //Line 1
//process list[i] //Line 2
• Example:
for (i = 0; i < 100; i++) //Line 1
cin >> list[i]; //Line 2
Array Initialization During
Declaration
• Arrays can be initialized during
declaration.
 In this case, it is not necessary to specify the size of
the array
 Size determined by the number of initial values in
the braces
• Example:
double sales[] = {12.25, 32.50, 16.90, 23,
45.68};
Array Index Out of Bounds
• If we have the statements:
double num[10];
int i;
• The component num[i] is valid if i = 0, 1, 2,
3, 4, 5, 6, 7, 8, or 9
• The index of an array is in bounds if the
index >=0 and the index <=ARRAY_SIZE-1
• Otherwise, we say the index is out of
bounds
• In C++, there is no guard against indices
that are out of bounds
Two- and Multidimensional
Arrays
• Two-dimensional array collection of a fixed
number of components (of the same type)
arranged in two dimensions
– Sometimes called matrices or tables
• Declaration syntax:
where intexp1 and intexp2 are expressions
yielding positive integer values, and specify the
number of rows and the number of columns,
respectively, in the array
Two- and Multidimensional Arrays
Accessing Array Components
• Syntax:
where indexexp1 and indexexp2 are
expressions yielding nonnegative integer
values, and specify the row and column
position
Accessing Array Components
Two-Dimensional Array Initialization
During Declaration
• Two-dimensional arrays can be initialized
when they are declared:
• Elements of each row are enclosed within
braces and separated by commas
• All rows are enclosed within braces
• For number arrays, if all components of a row
aren’t specified, unspecified ones are set to 0
Processing Two-Dimensional Arrays
• Ways to process a two-dimensional array:
– Process the entire array
– Process a particular row of the array, called
row processing
– Process a particular column of the array,
called column processing
• Each row and each column of a two-
dimensional array is a one-dimensional
array
– To process, use algorithms similar to
processing one-dimensional arrays
Processing Two-Dimensional Arrays
MultidimensionalArrays
• Multidimensional array collection of a fixed
number of elements (called components)
arranged in n dimensions (n >= 1)
• Also called an n-dimensionalarray
• Declaration syntax:
• Toaccess a component:
MultidimensionalArrays (cont'd.)
• When declaring a multidimensional array
as a formal parameter in a function
 Can omit size of first dimension but not other
dimensions
• As parameters, multidimensional arrays
are passed by reference only
• A function cannot return a value of the
type array
• There is no check if the array indices
are within bounds
THANKS

More Related Content

What's hot (20)

Arrays in C++ in Tamil - TNSCERT SYLLABUS PPT
Arrays in C++ in Tamil - TNSCERT SYLLABUS PPT Arrays in C++ in Tamil - TNSCERT SYLLABUS PPT
Arrays in C++ in Tamil - TNSCERT SYLLABUS PPT
 
Arrays in Java
Arrays in JavaArrays in Java
Arrays in Java
 
Array
ArrayArray
Array
 
Arrays
ArraysArrays
Arrays
 
Array in Java
Array in JavaArray in Java
Array in Java
 
Arrays in java
Arrays in javaArrays in java
Arrays in java
 
Array in Java
Array in JavaArray in Java
Array in Java
 
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
 
One Dimentional Array
One Dimentional ArrayOne Dimentional Array
One Dimentional Array
 
C++ programming (Array)
C++ programming (Array)C++ programming (Array)
C++ programming (Array)
 
One dimensional arrays
One dimensional arraysOne dimensional arrays
One dimensional arrays
 
Java Arrays
Java ArraysJava Arrays
Java Arrays
 
Sorting
SortingSorting
Sorting
 
Arrays in Java
Arrays in JavaArrays in Java
Arrays in Java
 
C# Lesson 3
C# Lesson 3C# Lesson 3
C# Lesson 3
 
Chap09
Chap09Chap09
Chap09
 
Array in Java
Array in JavaArray in Java
Array in Java
 
Array in c#
Array in c#Array in c#
Array in c#
 
Array 2 hina
Array 2 hina Array 2 hina
Array 2 hina
 
Array in c
Array in cArray in c
Array in c
 

Similar to Arrays

Similar to Arrays (20)

Arrays_and_Strings_in_C_Programming.pptx
Arrays_and_Strings_in_C_Programming.pptxArrays_and_Strings_in_C_Programming.pptx
Arrays_and_Strings_in_C_Programming.pptx
 
Arrays
ArraysArrays
Arrays
 
ARRAYS.pptx
ARRAYS.pptxARRAYS.pptx
ARRAYS.pptx
 
Arrays In C Language
Arrays In C LanguageArrays In C Language
Arrays In C Language
 
Array
ArrayArray
Array
 
C++ Arrays
C++ ArraysC++ Arrays
C++ Arrays
 
Programming fundamentals week 12.pptx
Programming fundamentals week 12.pptxProgramming fundamentals week 12.pptx
Programming fundamentals week 12.pptx
 
0-Slot18-19-20-ContiguousStorage.pdf
0-Slot18-19-20-ContiguousStorage.pdf0-Slot18-19-20-ContiguousStorage.pdf
0-Slot18-19-20-ContiguousStorage.pdf
 
arrays.pptx
arrays.pptxarrays.pptx
arrays.pptx
 
Arrays.pptx
Arrays.pptxArrays.pptx
Arrays.pptx
 
ARRAYS.pptx
ARRAYS.pptxARRAYS.pptx
ARRAYS.pptx
 
arrays.pptx
arrays.pptxarrays.pptx
arrays.pptx
 
2 Arrays & Strings.pptx
2 Arrays & Strings.pptx2 Arrays & Strings.pptx
2 Arrays & Strings.pptx
 
Chapter-Five.pptx
Chapter-Five.pptxChapter-Five.pptx
Chapter-Five.pptx
 
Arrays.pptx
 Arrays.pptx Arrays.pptx
Arrays.pptx
 
Unit 6. Arrays
Unit 6. ArraysUnit 6. Arrays
Unit 6. Arrays
 
Python array
Python arrayPython array
Python array
 
array-191103180006.pdf
array-191103180006.pdfarray-191103180006.pdf
array-191103180006.pdf
 
arrays.pptx
arrays.pptxarrays.pptx
arrays.pptx
 
Array Introduction One-dimensional array Multidimensional array
Array Introduction One-dimensional array Multidimensional arrayArray Introduction One-dimensional array Multidimensional array
Array Introduction One-dimensional array Multidimensional array
 

Recently uploaded

Roles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in PharmacovigilanceRoles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in PharmacovigilanceSamikshaHamane
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsanshu789521
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentInMediaRes1
 
Painted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of IndiaPainted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of IndiaVirag Sontakke
 
Full Stack Web Development Course for Beginners
Full Stack Web Development Course  for BeginnersFull Stack Web Development Course  for Beginners
Full Stack Web Development Course for BeginnersSabitha Banu
 
AmericanHighSchoolsprezentacijaoskolama.
AmericanHighSchoolsprezentacijaoskolama.AmericanHighSchoolsprezentacijaoskolama.
AmericanHighSchoolsprezentacijaoskolama.arsicmarija21
 
Capitol Tech U Doctoral Presentation - April 2024.pptx
Capitol Tech U Doctoral Presentation - April 2024.pptxCapitol Tech U Doctoral Presentation - April 2024.pptx
Capitol Tech U Doctoral Presentation - April 2024.pptxCapitolTechU
 
Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...Jisc
 
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdfFraming an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdfUjwalaBharambe
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdfssuser54595a
 
Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxthorishapillay1
 
DATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersDATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersSabitha Banu
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...Marc Dusseiller Dusjagr
 
Earth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatEarth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatYousafMalik24
 
Historical philosophical, theoretical, and legal foundations of special and i...
Historical philosophical, theoretical, and legal foundations of special and i...Historical philosophical, theoretical, and legal foundations of special and i...
Historical philosophical, theoretical, and legal foundations of special and i...jaredbarbolino94
 
Final demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptxFinal demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptxAvyJaneVismanos
 

Recently uploaded (20)

Roles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in PharmacovigilanceRoles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in Pharmacovigilance
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha elections
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media Component
 
Painted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of IndiaPainted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of India
 
Full Stack Web Development Course for Beginners
Full Stack Web Development Course  for BeginnersFull Stack Web Development Course  for Beginners
Full Stack Web Development Course for Beginners
 
AmericanHighSchoolsprezentacijaoskolama.
AmericanHighSchoolsprezentacijaoskolama.AmericanHighSchoolsprezentacijaoskolama.
AmericanHighSchoolsprezentacijaoskolama.
 
Capitol Tech U Doctoral Presentation - April 2024.pptx
Capitol Tech U Doctoral Presentation - April 2024.pptxCapitol Tech U Doctoral Presentation - April 2024.pptx
Capitol Tech U Doctoral Presentation - April 2024.pptx
 
Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...
 
OS-operating systems- ch04 (Threads) ...
OS-operating systems- ch04 (Threads) ...OS-operating systems- ch04 (Threads) ...
OS-operating systems- ch04 (Threads) ...
 
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdfFraming an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
 
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
 
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdfTataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
 
Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptx
 
DATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersDATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginners
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
 
Earth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatEarth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice great
 
Historical philosophical, theoretical, and legal foundations of special and i...
Historical philosophical, theoretical, and legal foundations of special and i...Historical philosophical, theoretical, and legal foundations of special and i...
Historical philosophical, theoretical, and legal foundations of special and i...
 
Final demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptxFinal demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptx
 
ESSENTIAL of (CS/IT/IS) class 06 (database)
ESSENTIAL of (CS/IT/IS) class 06 (database)ESSENTIAL of (CS/IT/IS) class 06 (database)
ESSENTIAL of (CS/IT/IS) class 06 (database)
 

Arrays

  • 2. INTRODUCTION • Array a collection of a fixed number of components wherein all of the components have the same data type • In a one-dimensional array, the components are arranged in a list form • Syntax for declaring a one-dimensional array:
  • 4. Defining Arrays • When defining arrays, specify –Name –Type of array –Number of elements • arrayType arrayName[ numberOfElements ]; –Examples: • int c[ 10 ]; • float myArray[ 3284 ];
  • 5. Accessing Array Components • General syntax: where indexExp, called an index, is any expression whose value is a nonnegative integer • Index value specifies the position of the component in the array • [ ] is the array subscripting operator • The array index always starts at 0
  • 7. One-Dimensional Arrays • Some basic operations performed on a one- dimensional array are: – Initializing – Inputting data – Outputting data stored in an array – Finding the largest and/or smallest element • Each operation requires ability to step through the elements of the array • Easily accomplished by a loop
  • 8. Processing One-Dimensional Arrays • Consider the declaration int list[100]; //array of size 100 int i; • Using forloops to access array elements: for (i = 0; i < 100; i++) //Line 1 //process list[i] //Line 2 • Example: for (i = 0; i < 100; i++) //Line 1 cin >> list[i]; //Line 2
  • 9. Array Initialization During Declaration • Arrays can be initialized during declaration.  In this case, it is not necessary to specify the size of the array  Size determined by the number of initial values in the braces • Example: double sales[] = {12.25, 32.50, 16.90, 23, 45.68};
  • 10. Array Index Out of Bounds • If we have the statements: double num[10]; int i; • The component num[i] is valid if i = 0, 1, 2, 3, 4, 5, 6, 7, 8, or 9 • The index of an array is in bounds if the index >=0 and the index <=ARRAY_SIZE-1 • Otherwise, we say the index is out of bounds • In C++, there is no guard against indices that are out of bounds
  • 11. Two- and Multidimensional Arrays • Two-dimensional array collection of a fixed number of components (of the same type) arranged in two dimensions – Sometimes called matrices or tables • Declaration syntax: where intexp1 and intexp2 are expressions yielding positive integer values, and specify the number of rows and the number of columns, respectively, in the array
  • 13. Accessing Array Components • Syntax: where indexexp1 and indexexp2 are expressions yielding nonnegative integer values, and specify the row and column position
  • 15. Two-Dimensional Array Initialization During Declaration • Two-dimensional arrays can be initialized when they are declared: • Elements of each row are enclosed within braces and separated by commas • All rows are enclosed within braces • For number arrays, if all components of a row aren’t specified, unspecified ones are set to 0
  • 16. Processing Two-Dimensional Arrays • Ways to process a two-dimensional array: – Process the entire array – Process a particular row of the array, called row processing – Process a particular column of the array, called column processing • Each row and each column of a two- dimensional array is a one-dimensional array – To process, use algorithms similar to processing one-dimensional arrays
  • 18. MultidimensionalArrays • Multidimensional array collection of a fixed number of elements (called components) arranged in n dimensions (n >= 1) • Also called an n-dimensionalarray • Declaration syntax: • Toaccess a component:
  • 19. MultidimensionalArrays (cont'd.) • When declaring a multidimensional array as a formal parameter in a function  Can omit size of first dimension but not other dimensions • As parameters, multidimensional arrays are passed by reference only • A function cannot return a value of the type array • There is no check if the array indices are within bounds