SlideShare ist ein Scribd-Unternehmen logo
1 von 20
Arrays
AhmadMohammedSAlSubhi
EngIbraheemAlEdane
What Is An Array?
• An array is a series of elements of the same type placed in
contiguous memory locations that can be individually
referenced by adding an index to a unique identifier.
• Array Used to store a collection of elements (variables).
• That means that, for example, five values of type int can be
declared as an array without having to declare 5 different
variables (each with its own identifier). Instead, using an
array, the five int values are stored in contiguous memory
locations, and all five can be accessed using the same
identifier, with the proper index.
o int array [5];
8-2
Array int 0 1 2 3 4
Basic Array Definition
Type of
values in
list
BaseType Id [ SizeExp ] ;
Name
of list
Bracketed constant
expression
indicating number
of elements in list
double X [ 100 ] ;
// Subscripts are 0 through 99
Declaring an array
• The statement
int num[5];
declares an array num of 5 components of
the type int
• The components are num[0], num[1],
num[2], num[3], and num[4]
Array Storage in Memory
The definition
int tests[ISIZE]; // ISIZE is 5
allocates the following memory
8-5
Element 0 Element 1 Element 2 Element 3 Element 4
Processing One-Dimensional Arrays
• Some basic operations performed on a
one-dimensional array are:
– Initialize
– Input data
– Output data stored in an array
– Find the largest and/or smallest element
• Each operation requires ability to step
through the elements of the array
• Easily accomplished by a loop
Accessing Array Components (continued)
• Consider the declaration
int list[100]; //list is an array
//of the size 100
int i;
• This for loop steps through each element
of the array list starting at the first element
for (i = 0; i < 100; i++) //Line 1
//process list[i] //Line 2
Accessing Array Components (continued)
• If processing list requires inputting data
into list
– The statement in Line 2 takes the from of
an input statement, such as the cin
statement
for (i = 0; i < 100; i++) //Line 1
cin >> list[i];
Array Initialization
• As with simple variables
– Arrays can be initialized while they are being declared
• When initializing arrays while declaring them
– Not necessary to specify the size of the array
• Size of array is determined by the number of initial values
in the braces
• For example:
double sales[] = {12.25, 32.50, 16.90, 23,
45.68};
Partial Initialization (continued)
• The statement
int list[] = {5, 6, 3};
declares list to be an array of 3 components and
initializes list[0] to 5, list[1] to 6, and list[2] to 3
• The statement
int list[25]= {4, 7};
declares list to be an array of 25 components
– The first two components are initialized to 4 and 7
respectively
– All other components are initialized to 0
How To Declare Arrays
• To declare an array in C++, you should specify the
following things
– The data type of the values which will be stored in the array
– The name of the array (i.e. a C++ identifier that will be used to
access and update the array values)
– The dimensionality of the array:
• One dimensional (i.e. list of values ),
• Two-dimension array (a matrix or a table), etc.
– The size of each dimension
• Examples
– int x[10];int x[10]; // An integer array named x with size 10
– float GPA[30];float GPA[30]; // An array to store the GPA for 30 students
– int studentScores[30][5];int studentScores[30][5]; // A two-dimensional array to store the
scores of 5 exams for 30 students
One-dimensional Arrays
• We use one-dimensional (ID) arrays to store and
access list of data values in an easy way by
giving these values a common name, e.g.
int x[4];int x[4]; // all values are named x
x[0] = 10;x[0] = 10; // the 1st
value is 10
x[1] = 5;x[1] = 5; // the 2nd
value is 5
x[2] = 20;x[2] = 20; // the 3rd
value is 20
x[3] = 30;x[3] = 30; // the 4th
value is 30
10
5
20
30
x[0]
x[1]
x[2]
x[3]
Example: Read Values and Print
them in Reverse Order
#include <iomanip.h>
void main()
{
int x[5], index;
cout << “Please enter five integer values: “;
for( index=0; index<5; index++) cin >> x[index];
cout << “The values in reverse order are: “;
for (index=4; index>=0; index--)
cout << setw(3) << x[index];
cout << endl;
}
Two-Dimensional Arrays
• Two-dimensional arrays are stored in
row order
– The first row is stored first, followed by the
second row, followed by the third row and
so on
• When declaring a two-dimensional array
as a formal parameter
– can omit size of first dimension, but not the
second
• Number of columns must be specified
Example
• #include <iostream>
• using namespace std;
• int main()
• {
• int test[3][2] =
• {
• {2, -5},
• {4, 0},
• {9, 1}
• };
• // Accessing two dimensional array using
• // nested for loops
• for(int i = 0; i < 3; ++i)
• {
• for(int j = 0; j < 2; ++j)
• {
• cou t<< "test[" << i << "][" << j << "] = " << test[i][j] << endl;
• }
• }
• return 0;
• }
8-15
Multidimensional Arrays
• Multidimensional Array: collection of a fixed
number of elements (called components)
arranged in n dimensions (n >= 1)
• Also called an n-dimensional array
• General syntax of declaring an n-
dimensional array is:
dataType arrayName[intExp1][intExp2]...[intExpn];
where intExp1, intExp2, … are constant
expressions yielding positive integer values
Multidimensional Arrays (continued)
• The syntax for accessing a component of
an n-dimensional array is:
arrayName[indexExp1][indexExp2]...[indexExpn]
where indexExp1,indexExp2,..., and
indexExpn are expressions yielding
nonnegative integer values
• indexExpi gives the position of the array
component in the ith dimension
Example• #include <iostream>
• using namespace std;
• int main()
• {
• // This array can store upto 12 elements (2x3x2)
• int test[2][3][2];
• cout << "Enter 12 values: n";
•
• // Inserting the values into the test array
• // using 3 nested for loops.
• for(int i = 0; i < 2; ++i)
• {
• for (int j = 0; j < 3; ++j)
• {
• for(int k = 0; k < 2; ++k )
• {
• cin >> test[i][j][k];
• }
• }
• }
• cout<<"nDisplaying Value stored:"<<endl;
• // Displaying the values with proper index.
• for(int i = 0; i < 2; ++i)
• {
• for (int j = 0; j < 3; ++j)
• {
• for(int k = 0; k < 2; ++k)
• {
• cout << "test[" << i << "][" << j << "][" << k << "] = " << test[i][j][k] << endl;
• }
• }
• }
• return 0;
• }
8-18
Summary
• An array is a structured data type with a fixed number of components
– Every component is of the same type
– Components are accessed using their relative positions in the array
• Elements of a one-dimensional array are arranged in the form of a list
• An array index can be any expression that evaluates to a non-negative
integer
• The value of the index must always be less than the size of the array
• The base address of an array is the address of the first array component
• In a function call statement, when passing an array as an actual
parameter, you use only its name
• As parameters to functions, arrays are passed by reference only
• A function cannot return a value of the type array
• In C++, C-strings are null terminated and are stored in character arrays
Resource
• https://www.programiz.com/cpp-programming/m
•
• https://www.tutorialspoint.com/cplusplus/cpp_nu
• http://www.cplusplus.com/doc/tutorial/arrays/
8-20

Weitere ähnliche Inhalte

Was ist angesagt? (20)

Array in c++
Array in c++Array in c++
Array in c++
 
One dimensional 2
One dimensional 2One dimensional 2
One dimensional 2
 
ARRAY
ARRAYARRAY
ARRAY
 
C++ arrays part1
C++ arrays part1C++ arrays part1
C++ arrays part1
 
Array
ArrayArray
Array
 
Array
ArrayArray
Array
 
Arrays
ArraysArrays
Arrays
 
Data Structure and Algorithms Arrays
Data Structure and Algorithms ArraysData Structure and Algorithms Arrays
Data Structure and Algorithms Arrays
 
1 D Arrays in C++
1 D Arrays in C++1 D Arrays in C++
1 D Arrays in C++
 
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
 
Lecture 3 data structures and algorithms
Lecture 3 data structures and algorithmsLecture 3 data structures and algorithms
Lecture 3 data structures and algorithms
 
Arrays
ArraysArrays
Arrays
 
Array
ArrayArray
Array
 
Passing an Array to a Function (ICT Programming)
Passing an Array to a Function (ICT Programming)Passing an Array to a Function (ICT Programming)
Passing an Array to a Function (ICT Programming)
 
Presentation on array
Presentation on array Presentation on array
Presentation on array
 
Arrays Basics
Arrays BasicsArrays Basics
Arrays Basics
 
arrays in c
arrays in carrays in c
arrays in c
 
Unit 6. Arrays
Unit 6. ArraysUnit 6. Arrays
Unit 6. Arrays
 
Array Presentation
Array PresentationArray Presentation
Array Presentation
 
Array ppt
Array pptArray ppt
Array ppt
 

Ähnlich wie C++ Arrays

Ähnlich wie C++ Arrays (20)

6 arrays injava
6 arrays injava6 arrays injava
6 arrays injava
 
Arrays
ArraysArrays
Arrays
 
Arrays
ArraysArrays
Arrays
 
array-191103180006.pdf
array-191103180006.pdfarray-191103180006.pdf
array-191103180006.pdf
 
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
 
ARRAYS.pptx
ARRAYS.pptxARRAYS.pptx
ARRAYS.pptx
 
Arrays
ArraysArrays
Arrays
 
Array
ArrayArray
Array
 
arrays.pptx
arrays.pptxarrays.pptx
arrays.pptx
 
Arrays 1D and 2D , and multi dimensional
Arrays 1D and 2D , and multi dimensional Arrays 1D and 2D , and multi dimensional
Arrays 1D and 2D , and multi dimensional
 
Week06
Week06Week06
Week06
 
Arrays
ArraysArrays
Arrays
 
Introduction to Array & Structure & Basic Algorithms.pptx
Introduction to Array & Structure & Basic Algorithms.pptxIntroduction to Array & Structure & Basic Algorithms.pptx
Introduction to Array & Structure & Basic Algorithms.pptx
 
arrayy.ppt
arrayy.pptarrayy.ppt
arrayy.ppt
 
Fp201 unit4
Fp201 unit4Fp201 unit4
Fp201 unit4
 
Array assignment
Array assignmentArray assignment
Array assignment
 
Array-part1
Array-part1Array-part1
Array-part1
 
Arrays
ArraysArrays
Arrays
 
arrays in c# including Classes handling arrays
arrays in c#  including Classes handling arraysarrays in c#  including Classes handling arrays
arrays in c# including Classes handling arrays
 
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
 

Kürzlich hochgeladen

The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxheathfieldcps1
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfciinovamais
 
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...Shubhangi Sonawane
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdfQucHHunhnh
 
Measures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDMeasures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDThiyagu K
 
Gardella_PRCampaignConclusion Pitch Letter
Gardella_PRCampaignConclusion Pitch LetterGardella_PRCampaignConclusion Pitch Letter
Gardella_PRCampaignConclusion Pitch LetterMateoGardella
 
Gardella_Mateo_IntellectualProperty.pdf.
Gardella_Mateo_IntellectualProperty.pdf.Gardella_Mateo_IntellectualProperty.pdf.
Gardella_Mateo_IntellectualProperty.pdf.MateoGardella
 
Class 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdfClass 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdfAyushMahapatra5
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfagholdier
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsTechSoup
 
Making and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdfMaking and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdfChris Hunter
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...christianmathematics
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhikauryashika82
 
PROCESS RECORDING FORMAT.docx
PROCESS      RECORDING        FORMAT.docxPROCESS      RECORDING        FORMAT.docx
PROCESS RECORDING FORMAT.docxPoojaSen20
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104misteraugie
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...EduSkills OECD
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfJayanti Pande
 

Kürzlich hochgeladen (20)

The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
 
Advance Mobile Application Development class 07
Advance Mobile Application Development class 07Advance Mobile Application Development class 07
Advance Mobile Application Development class 07
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
 
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
 
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
 
Measures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDMeasures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SD
 
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
 
Gardella_PRCampaignConclusion Pitch Letter
Gardella_PRCampaignConclusion Pitch LetterGardella_PRCampaignConclusion Pitch Letter
Gardella_PRCampaignConclusion Pitch Letter
 
Gardella_Mateo_IntellectualProperty.pdf.
Gardella_Mateo_IntellectualProperty.pdf.Gardella_Mateo_IntellectualProperty.pdf.
Gardella_Mateo_IntellectualProperty.pdf.
 
Class 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdfClass 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdf
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
 
Making and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdfMaking and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdf
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
 
PROCESS RECORDING FORMAT.docx
PROCESS      RECORDING        FORMAT.docxPROCESS      RECORDING        FORMAT.docx
PROCESS RECORDING FORMAT.docx
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdf
 

C++ Arrays

  • 2. What Is An Array? • An array is a series of elements of the same type placed in contiguous memory locations that can be individually referenced by adding an index to a unique identifier. • Array Used to store a collection of elements (variables). • That means that, for example, five values of type int can be declared as an array without having to declare 5 different variables (each with its own identifier). Instead, using an array, the five int values are stored in contiguous memory locations, and all five can be accessed using the same identifier, with the proper index. o int array [5]; 8-2 Array int 0 1 2 3 4
  • 3. Basic Array Definition Type of values in list BaseType Id [ SizeExp ] ; Name of list Bracketed constant expression indicating number of elements in list double X [ 100 ] ; // Subscripts are 0 through 99
  • 4. Declaring an array • The statement int num[5]; declares an array num of 5 components of the type int • The components are num[0], num[1], num[2], num[3], and num[4]
  • 5. Array Storage in Memory The definition int tests[ISIZE]; // ISIZE is 5 allocates the following memory 8-5 Element 0 Element 1 Element 2 Element 3 Element 4
  • 6. Processing One-Dimensional Arrays • Some basic operations performed on a one-dimensional array are: – Initialize – Input data – Output data stored in an array – Find the largest and/or smallest element • Each operation requires ability to step through the elements of the array • Easily accomplished by a loop
  • 7. Accessing Array Components (continued) • Consider the declaration int list[100]; //list is an array //of the size 100 int i; • This for loop steps through each element of the array list starting at the first element for (i = 0; i < 100; i++) //Line 1 //process list[i] //Line 2
  • 8. Accessing Array Components (continued) • If processing list requires inputting data into list – The statement in Line 2 takes the from of an input statement, such as the cin statement for (i = 0; i < 100; i++) //Line 1 cin >> list[i];
  • 9. Array Initialization • As with simple variables – Arrays can be initialized while they are being declared • When initializing arrays while declaring them – Not necessary to specify the size of the array • Size of array is determined by the number of initial values in the braces • For example: double sales[] = {12.25, 32.50, 16.90, 23, 45.68};
  • 10. Partial Initialization (continued) • The statement int list[] = {5, 6, 3}; declares list to be an array of 3 components and initializes list[0] to 5, list[1] to 6, and list[2] to 3 • The statement int list[25]= {4, 7}; declares list to be an array of 25 components – The first two components are initialized to 4 and 7 respectively – All other components are initialized to 0
  • 11. How To Declare Arrays • To declare an array in C++, you should specify the following things – The data type of the values which will be stored in the array – The name of the array (i.e. a C++ identifier that will be used to access and update the array values) – The dimensionality of the array: • One dimensional (i.e. list of values ), • Two-dimension array (a matrix or a table), etc. – The size of each dimension • Examples – int x[10];int x[10]; // An integer array named x with size 10 – float GPA[30];float GPA[30]; // An array to store the GPA for 30 students – int studentScores[30][5];int studentScores[30][5]; // A two-dimensional array to store the scores of 5 exams for 30 students
  • 12. One-dimensional Arrays • We use one-dimensional (ID) arrays to store and access list of data values in an easy way by giving these values a common name, e.g. int x[4];int x[4]; // all values are named x x[0] = 10;x[0] = 10; // the 1st value is 10 x[1] = 5;x[1] = 5; // the 2nd value is 5 x[2] = 20;x[2] = 20; // the 3rd value is 20 x[3] = 30;x[3] = 30; // the 4th value is 30 10 5 20 30 x[0] x[1] x[2] x[3]
  • 13. Example: Read Values and Print them in Reverse Order #include <iomanip.h> void main() { int x[5], index; cout << “Please enter five integer values: “; for( index=0; index<5; index++) cin >> x[index]; cout << “The values in reverse order are: “; for (index=4; index>=0; index--) cout << setw(3) << x[index]; cout << endl; }
  • 14. Two-Dimensional Arrays • Two-dimensional arrays are stored in row order – The first row is stored first, followed by the second row, followed by the third row and so on • When declaring a two-dimensional array as a formal parameter – can omit size of first dimension, but not the second • Number of columns must be specified
  • 15. Example • #include <iostream> • using namespace std; • int main() • { • int test[3][2] = • { • {2, -5}, • {4, 0}, • {9, 1} • }; • // Accessing two dimensional array using • // nested for loops • for(int i = 0; i < 3; ++i) • { • for(int j = 0; j < 2; ++j) • { • cou t<< "test[" << i << "][" << j << "] = " << test[i][j] << endl; • } • } • return 0; • } 8-15
  • 16. Multidimensional Arrays • Multidimensional Array: collection of a fixed number of elements (called components) arranged in n dimensions (n >= 1) • Also called an n-dimensional array • General syntax of declaring an n- dimensional array is: dataType arrayName[intExp1][intExp2]...[intExpn]; where intExp1, intExp2, … are constant expressions yielding positive integer values
  • 17. Multidimensional Arrays (continued) • The syntax for accessing a component of an n-dimensional array is: arrayName[indexExp1][indexExp2]...[indexExpn] where indexExp1,indexExp2,..., and indexExpn are expressions yielding nonnegative integer values • indexExpi gives the position of the array component in the ith dimension
  • 18. Example• #include <iostream> • using namespace std; • int main() • { • // This array can store upto 12 elements (2x3x2) • int test[2][3][2]; • cout << "Enter 12 values: n"; • • // Inserting the values into the test array • // using 3 nested for loops. • for(int i = 0; i < 2; ++i) • { • for (int j = 0; j < 3; ++j) • { • for(int k = 0; k < 2; ++k ) • { • cin >> test[i][j][k]; • } • } • } • cout<<"nDisplaying Value stored:"<<endl; • // Displaying the values with proper index. • for(int i = 0; i < 2; ++i) • { • for (int j = 0; j < 3; ++j) • { • for(int k = 0; k < 2; ++k) • { • cout << "test[" << i << "][" << j << "][" << k << "] = " << test[i][j][k] << endl; • } • } • } • return 0; • } 8-18
  • 19. Summary • An array is a structured data type with a fixed number of components – Every component is of the same type – Components are accessed using their relative positions in the array • Elements of a one-dimensional array are arranged in the form of a list • An array index can be any expression that evaluates to a non-negative integer • The value of the index must always be less than the size of the array • The base address of an array is the address of the first array component • In a function call statement, when passing an array as an actual parameter, you use only its name • As parameters to functions, arrays are passed by reference only • A function cannot return a value of the type array • In C++, C-strings are null terminated and are stored in character arrays