0% found this document useful (0 votes)
23 views6 pages

Two Dim Array

A two-dimensional array is a matrix structure consisting of rows and columns where data values are stored in a specified sequence. It can be initialized using two methods: simple value initialization or nested braces, and elements are accessed using row and column indexes. Additionally, two-dimensional arrays can store character data by enclosing characters in apostrophes.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views6 pages

Two Dim Array

A two-dimensional array is a matrix structure consisting of rows and columns where data values are stored in a specified sequence. It can be initialized using two methods: simple value initialization or nested braces, and elements are accessed using row and column indexes. Additionally, two-dimensional arrays can store character data by enclosing characters in apostrophes.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Two-Dimensional Array

• A two-Dimensional array is a matrix structure. It consists of a number


of Rows and Columns. Each data value is stored in matrix format
according to the sequence of the data values specified. A Two-
Dimensional Array can be visualized by looking at the below image:
Initializing a 2D Array
• In order to understand how to initialize a Two Dimensional array with
data values, we follow the following syntax,
data_type array_name [x][y] ;

where,
data_type = Type of data to be stored in the array
x = number of rows
y = number of columns
Note – Elements of a 2D array are generally represented
in the format arr[i][j] where ‘i’ is the number of rows and ‘j’
is the number of columns of the array.

# Two-dimensional Array initialization


int arr[3][2] = {100, 10, 200, 20, 300, 30} ; OR
int arr[3][2] = {{100, 10}, {200, 20}, {300, 30}} ; //The values will be filled row
wise
• The first method is a simple value initialization, where the values are
stored in the array from left to right. i.e. first 2 elements will be stored
in Row 1, the next two in row 2, and so on.
• The second method uses the nested braces method. Here every set
of braces represents a row. Each nested braces value will be stored in
separate rows. In the above example, there are 3 inner braces, so
there will be 3 rows in the Two-dimensional Array.
Accessing Elements of 2D Array

• Just like the Indexing method used for accessing elements of a One-
Dimensional Array, Two-Dimensional Arrays also use the Indexing
method. The only difference is that the row and column indexes are
used to access array elements.
# To print all the elements of a 2D Array #include
using namespace std;
int main() { int arr[4][2] = { { 10, 11 }, { 20, 21 }, { 30, 31
}, { 40, 41 } };
cout << "Elements of 2D Array are : \n" ;
for (int x = 0; x < 4 ; x++) // Outer for loop for traversing
rows
{ for(int y = 0; y < 2 ; y++) // Inner for loop for traversing
columns
{ cout << "\t" << arr[x][y] ; // Print individual array element
}
cout << endl;
} }
Two-Dimensional Character Array
Two-dimensional arrays are not only used to store numeric forms of data but can also store character data type
values. In order to store characters in a two-dimensional array, we need to encase them in the Apostrophe mark
i.e. ‘ ‘. Following is an example of how a 2D Array is used to store character data type –

using namespace std;


int main()
{ char val [3][3]= { {'c', 'a', 't'}, {'m', 'a', 't'}, {'f', 'a', 't'} };
cout << val[2][0] ;
}

You might also like