SlideShare a Scribd company logo
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

ODP
C++ arrays part1
PDF
Arrays in C++
PPT
Presentation of array
PPTX
PPT
C++ Arrays
PPT
Arrays in C++
PPTX
Java arrays
PPT
Lecture7
C++ arrays part1
Arrays in C++
Presentation of array
C++ Arrays
Arrays in C++
Java arrays
Lecture7

What's hot (20)

PPTX
Arrays in C++ in Tamil - TNSCERT SYLLABUS PPT
PDF
Arrays in Java
PPTX
Array
PPTX
Arrays
PDF
Array in Java
PPTX
Arrays in java
PPTX
Array in Java
PDF
An Introduction to Programming in Java: Arrays
PPTX
One Dimentional Array
PPTX
C++ programming (Array)
PPTX
One dimensional arrays
PDF
Java Arrays
PPTX
Sorting
PPTX
Arrays in Java
PPTX
C# Lesson 3
PPT
Chap09
PPT
Array in Java
PPTX
Array in c#
PPTX
Array 2 hina
PPT
Array in c
Arrays in C++ in Tamil - TNSCERT SYLLABUS PPT
Arrays in Java
Array
Arrays
Array in Java
Arrays in java
Array in Java
An Introduction to Programming in Java: Arrays
One Dimentional Array
C++ programming (Array)
One dimensional arrays
Java Arrays
Sorting
Arrays in Java
C# Lesson 3
Chap09
Array in Java
Array in c#
Array 2 hina
Array in c
Ad

Similar to Arrays (20)

PPTX
Arrays_and_Strings_in_C_Programming.pptx
PPT
PPTX
ARRAYS.pptx
PPT
PF Lecture 9.ppt hakakbshisokwb jaiksnsjomabhj
PPTX
Unit4pptx__2024_11_ 11_10_16_09.pptx
PPTX
Arrays In C Language
PPT
Array
PDF
Computer science abot c topics array 2d array
PPT
C++ Arrays
PPTX
Programming fundamentals week 12.pptx
PDF
0-Slot18-19-20-ContiguousStorage.pdf
PPTX
10 arrays and pointers.pptx for array and pointer
PPTX
arrays.pptx
PPTX
numpydocococ34554367827839271966666.pptx
PPTX
Arrays.pptx
PPTX
PPT
C array and Initialisation and declaration
PPT
show the arrays ppt for first year students
PPTX
c unit programming arrays in detail 3.pptx
PPTX
arrays.pptx
Arrays_and_Strings_in_C_Programming.pptx
ARRAYS.pptx
PF Lecture 9.ppt hakakbshisokwb jaiksnsjomabhj
Unit4pptx__2024_11_ 11_10_16_09.pptx
Arrays In C Language
Array
Computer science abot c topics array 2d array
C++ Arrays
Programming fundamentals week 12.pptx
0-Slot18-19-20-ContiguousStorage.pdf
10 arrays and pointers.pptx for array and pointer
arrays.pptx
numpydocococ34554367827839271966666.pptx
Arrays.pptx
C array and Initialisation and declaration
show the arrays ppt for first year students
c unit programming arrays in detail 3.pptx
arrays.pptx
Ad

Recently uploaded (20)

PDF
O7-L3 Supply Chain Operations - ICLT Program
PDF
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
PDF
Computing-Curriculum for Schools in Ghana
PDF
Classroom Observation Tools for Teachers
PDF
Sports Quiz easy sports quiz sports quiz
PPTX
master seminar digital applications in india
PDF
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
PDF
O5-L3 Freight Transport Ops (International) V1.pdf
PDF
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
PDF
VCE English Exam - Section C Student Revision Booklet
PDF
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
PDF
Abdominal Access Techniques with Prof. Dr. R K Mishra
PDF
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
PDF
Complications of Minimal Access Surgery at WLH
PPTX
Microbial diseases, their pathogenesis and prophylaxis
PPTX
Renaissance Architecture: A Journey from Faith to Humanism
PDF
TR - Agricultural Crops Production NC III.pdf
PPTX
Pharmacology of Heart Failure /Pharmacotherapy of CHF
PDF
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
PPTX
Institutional Correction lecture only . . .
O7-L3 Supply Chain Operations - ICLT Program
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
Computing-Curriculum for Schools in Ghana
Classroom Observation Tools for Teachers
Sports Quiz easy sports quiz sports quiz
master seminar digital applications in india
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
O5-L3 Freight Transport Ops (International) V1.pdf
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
VCE English Exam - Section C Student Revision Booklet
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
Abdominal Access Techniques with Prof. Dr. R K Mishra
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
Complications of Minimal Access Surgery at WLH
Microbial diseases, their pathogenesis and prophylaxis
Renaissance Architecture: A Journey from Faith to Humanism
TR - Agricultural Crops Production NC III.pdf
Pharmacology of Heart Failure /Pharmacotherapy of CHF
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
Institutional Correction lecture only . . .

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