ARRAY
ARRAY
ARRAYS
❖Arrays are a type of data structure that stores a
set of a particular data, which has been placed in
contagious memory location.
❖Arrays should be looked at more than just a set of
data, but a set of variables of the same data type.
❖The lowest address corresponds to the first
element and the highest address to the last
element.
ARRAYS
❖In C++, an array is a variable that can store multiple values of the
same type. For example,
❖Suppose a class has 27 students, and we need to store the grades
of all of them. Instead of creating 27 separate variables, we can
simply create an array:
Double grade[27];
❖Here, grade is an array that can hold a maximum of 27 elements
of double type.
❖In C++, the size and type of arrays cannot be changed after its
declaration.
DECLARING ARRAYS
❖To declare an array, define the variable type, specify the name of the
array followed by square brackets and specify the number of elements
it should store:
dataType arrayName[arraySize];
string cars[4];
double balance[10];
int x[6];
NOTE: The elements field within square brackets [], representing the
number of elements in the array, must be a constant expression, since
arrays are blocks of static memory whose size must be determined at
compile time, before the program runs.
INITIALIZING AN ARRAY
❖You can initialize C++ array elements either one by
one or using a single statement as follows −
string letters[2][4];
int x[3][4];
DECLARING A MULTIDIMENSIONAL
ARRAY
❖Here, x is a two-dimensional array. It can hold a maximum of 12
elements.
int x[3][4];
❖We can think of this array as a table with 3 rows and each row has
4 columns as shown below.
Initializing a Multidimensional
Array
1. Initialization of two-dimensional array
int test[2][3] = { {2, 4, 5}, {9, 0, 19}};
This array has 2 rows and 3 columns, which is why we have two rows
of elements with 3 elements each.
Initializing a Multidimensional
Array
2. Initialization of three-dimensional array
int test[2][3][4] = { { {3, 4, 2, 3}, {0, -3, 9, 11}, {23, 12, 23, 2} },
{ {13, 4, 56, 3}, {5, 9, 3, 5}, {5, 1, 4, 9} } };
The first dimension has the value 2. So, the two elements comprising
the first dimension are:
The second dimension has the value 3. Notice that each of the
elements of the first dimension has three elements each:
{3, 4, 2, 3}, {0, -3, 9, 11} and {23, 12, 23, 2} for Element 1.
{13, 4, 56, 3}, {5, 9, 3, 5} and {5, 1, 4, 9} for Element 2.
Initializing a Multidimensional
Array
2. Initialization of three-dimensional array
int test[2][3][4] = { { {3, 4, 2, 3}, {0, -3, 9, 11}, {23, 12, 23, 2} },
{ {13, 4, 56, 3}, {5, 9, 3, 5}, {5, 1, 4, 9} } };
Finally, there are four int numbers inside each of the elements of the
second dimension:
{3, 4, 2, 3}
{0, -3, 9, 11}
... .. ...
... .. ...
TWO-DIMENSIONAL
ARRAY
Two-Dimensional Array
❖The simplest form of the multidimensional array is the two-
dimensional array. A two-dimensional array is, in essence, a
list of one-dimensional arrays. To declare a two-dimensional
integer array of size x,y, you would write something as follows
−
type arrayName [ x ][ y ];
❖Where type can be any valid C++ data type
and arrayName will be a valid C++ identifier.
❖A two-dimensional array is also called a matrix.
Two-Dimensional Array
❖A two-dimensional array can be think as a table, which will have x
number of rows and y number of columns. A 2-dimensional array a,
which contains three rows and four columns can be shown as below
−
❑The above initialization won’t give us a clear picture of the array. For better
visualization, we can initialize the same array as below.
int 3DArray[2][2][4] = {
{ {1, 3, 6, 5}, {8, 9, -2, 4} },
{ {5, 10, 34, 56}, {23, -56, 10, 37} }
};
Sample Program using a 3D
Array
Print three-dimensional
array with their indexes