SlideShare ist ein Scribd-Unternehmen logo
1 von 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

Weitere ähnliche Inhalte

Was ist angesagt?

Was ist angesagt? (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
 

Ähnlich wie Arrays

Programming fundamentals week 12.pptx
Programming fundamentals week 12.pptxProgramming fundamentals week 12.pptx
Programming fundamentals week 12.pptx
dfsdg3
 
array-191103180006.pdf
array-191103180006.pdfarray-191103180006.pdf
array-191103180006.pdf
HEMAHEMS5
 

Ähnlich wie 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
 

Kürzlich hochgeladen

Kürzlich hochgeladen (20)

HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxHMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
 
Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)
 
Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibit
 
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdfUnit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdf
 
Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024
 
Single or Multiple melodic lines structure
Single or Multiple melodic lines structureSingle or Multiple melodic lines structure
Single or Multiple melodic lines structure
 
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfUGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdf
 
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptxOn_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
 
Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.ppt
 
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptxHMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
 
Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdf
 
Towards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxTowards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptx
 
FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024
 
Graduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - EnglishGraduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - English
 
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
 
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptxCOMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptx
 

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