SlideShare una empresa de Scribd logo
1 de 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

Más contenido relacionado

La actualidad más candente (20)

concept of Array, 1D & 2D array
concept of Array, 1D & 2D arrayconcept of Array, 1D & 2D array
concept of Array, 1D & 2D array
 
Array in c
Array in cArray in c
Array in c
 
Strings
StringsStrings
Strings
 
Array ppt
Array pptArray ppt
Array ppt
 
Multidimensional array in C
Multidimensional array in CMultidimensional array in C
Multidimensional array in C
 
Sparse matrix and its representation data structure
Sparse matrix and its representation data structureSparse matrix and its representation data structure
Sparse matrix and its representation data structure
 
Arrays in c
Arrays in cArrays in c
Arrays in c
 
arrays in c
arrays in carrays in c
arrays in c
 
2D Array
2D Array 2D Array
2D Array
 
ARRAY
ARRAYARRAY
ARRAY
 
Structure in C
Structure in CStructure in C
Structure in C
 
String in java
String in javaString in java
String in java
 
Strings in Java
Strings in JavaStrings in Java
Strings in Java
 
Arrays in Java
Arrays in JavaArrays in Java
Arrays in Java
 
UNIT I LINEAR DATA STRUCTURES – LIST
UNIT I 	LINEAR DATA STRUCTURES – LIST 	UNIT I 	LINEAR DATA STRUCTURES – LIST
UNIT I LINEAR DATA STRUCTURES – LIST
 
Pre processor directives in c
Pre processor directives in cPre processor directives in c
Pre processor directives in c
 
Looping statements in Java
Looping statements in JavaLooping statements in Java
Looping statements in Java
 
Two dimensional array
Two dimensional arrayTwo dimensional array
Two dimensional array
 
Two dimensional arrays
Two dimensional arraysTwo dimensional arrays
Two dimensional arrays
 
Functions in c language
Functions in c language Functions in c language
Functions in c language
 

Similar a C++ Arrays (20)

6 arrays injava
6 arrays injava6 arrays injava
6 arrays injava
 
Arrays
ArraysArrays
Arrays
 
Unit 6. Arrays
Unit 6. ArraysUnit 6. Arrays
Unit 6. Arrays
 
Arrays
ArraysArrays
Arrays
 
array-191103180006.pdf
array-191103180006.pdfarray-191103180006.pdf
array-191103180006.pdf
 
Arrays in C++
Arrays in C++Arrays in C++
Arrays in C++
 
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
 
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
 

Último

Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Disha Kariya
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAssociation for Project Management
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxiammrhaywood
 
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
 
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
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactdawncurless
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)eniolaolutunde
 
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
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdfSoniaTolstoy
 
Disha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdfDisha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdfchloefrazer622
 
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...fonyou31
 
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.pdfAdmir Softic
 
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
 
Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfArihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfchloefrazer622
 
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...Sapna Thakur
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdfQucHHunhnh
 

Último (20)

Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..
 
Advance Mobile Application Development class 07
Advance Mobile Application Development class 07Advance Mobile Application Development class 07
Advance Mobile Application Development class 07
 
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptxINDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across Sectors
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
 
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
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
 
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 ...
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impact
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)
 
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...
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
 
Disha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdfDisha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdf
 
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
 
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
 
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
 
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"
 
Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfArihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdf
 
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.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