0% found this document useful (0 votes)
21 views

ARRAY

Arrays allow storing multiple values of the same type. A one-dimensional array stores elements in contiguous memory locations. Multidimensional arrays can be thought of as arrays of arrays, with each element accessed via multiple indices. Common examples include two-dimensional arrays which represent matrices, and three-dimensional arrays which can represent cubes of data. Elements in multidimensional arrays are initialized and accessed using multiple indices, one for each array dimension.

Uploaded by

marianneb.dulfo
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views

ARRAY

Arrays allow storing multiple values of the same type. A one-dimensional array stores elements in contiguous memory locations. Multidimensional arrays can be thought of as arrays of arrays, with each element accessed via multiple indices. Common examples include two-dimensional arrays which represent matrices, and three-dimensional arrays which can represent cubes of data. Elements in multidimensional arrays are initialized and accessed using multiple indices, one for each array dimension.

Uploaded by

marianneb.dulfo
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 33

ARRAYS

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 −

double balance[5] = {1000.0, 2.0, 3.4, 17.0, 50.0};


int num [5] = { 16, 2, 77, 40, 12071 };
string cars[4] = {"Volvo", "BMW", "Ford", "Mazda"};
INITIALIZING AN ARRAY
❖The number of values between braces {} shall not be greater than the
number of elements in the array. For example, int num [5] = { 16, 2,
77, 40, 12071 };, num was declared having 5 elements (as specified by
the number enclosed in square brackets, []), and the braces {} contained
exactly 5 values, one for each element. If declared with less, the remaining
elements are set to their default values (which for fundamental types, means
they are filled with zeroes). For example:
int num [5] = { 16, 2, 77};,
0 1 2 3 4
int num 16 2 77 0 0
ACCESSING ARRAY ELEMENTS
❖In C++, each element in an array is associated with a number. The
number is known as an array index. We can access elements of an
array by using those indices.
// syntax to access array elements
array[index];
SAMPLE PROGRAM USING ARRAYS
SAMPLE PROGRAM USING ARRAYS
Take Inputs from User and Store Them in an Array

• Once again, we have used a for loop to


iterate from i = 0 to i = 4. In each
iteration, we took an input from the user
and stored it in numbers[i].
• Then, we used another for loop to print
all the array elements.
This example outputs the index of each
element together with its value:
MULTIDIMENSIONAL
ARRAYS
Multidimensional Arrays
❖A multi-dimensional array can be termed as an
array of arrays that stores homogeneous data in
tabular form. Data in multidimensional arrays is
generally stored in row-major order in the memory.
❖The multidimensional array is also known as
rectangular arrays in C++. It can be two dimensional
or three dimensional. The data is stored in tabular
form (row ∗ column) which is also known as matrix.
DECLARING A MULTIDIMENSIONAL
ARRAY
❖To declare a multi-dimensional array, define the variable type,
specify the name of the array followed by square brackets which
specify how many elements the main array has, followed by another
set of square brackets which indicates how many elements the sub-
arrays have:

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:

Element 1 = { {3, 4, 2, 3}, {0, -3, 9, 11}, {23, 12, 23, 2} }


Element 2 = { {13, 4, 56, 3}, {5, 9, 3, 5}, {5, 1, 4, 9} }
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 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

❖Thus, every element in array a is identified by an element name of


the form a[ i ][ j ], where a is the name of the array, and i and j are
the subscripts that uniquely identify each element in a.
Initializing a 2D Array
❖Multidimensioned arrays may be initialized by specifying
bracketed values for each row. Following is an array with 3 rows
and each row have 4 columns.
Accessing a 2D Array Elements
❖An element in 2-dimensional array is accessed
by using the subscripts, i.e., row index and
column index of the array. For example:

int val = a[2][3];


Taking 2D Array Elements
As User Input
Three-dimensional
Arrays
Three-dimensional Array
❑A three-dimensional (3D) array is a collection of two-
dimensional (2D) arrays.
❑In a 3D array, there are three dimensions (subscripts). The first
shows block size, the second shows row size, and the third shows
column size. The dimensions of a 2D array are represented by
the row and column sizes.
❑The block size, on the other hand, indicates the number of 2D
arrays. For example, if a 3D array's dimension is 3*4*2, it is a 3
2D array with 4 * 2 dimensions. Furthermore, it refers to a three-
by-two-dimensional array with four rows and two columns.
Three-dimensional Array
❑Syntax:
data_type array_name[size1][size2][size3];
Example: Here 3DArray is a three-dimensional array, having a
maximum of 24 elements.
int 3DArray[2][3][4];
❑The maximum number of elements contained in an array is
obtained by multiplying the size of all the dimensions.
Example: In 3DArray[2][3][4], The maximum element is obtained
by multiplying 2, 3, 4, i.e. 24.
Initialization of 3D Array
❑We Can Initialize a 3-Dimensional Array in Many Ways. Below Are the
Examples for Reference.
int 3DArray[2][2][4] = {1, 3, 6, 5, 8, 9, -2, 4, 5, 10, 34, 56, 23, -56, 10, 37};

❑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

You might also like