The document discusses two dimensional arrays in C++, including what they are, how to declare and initialize them, and providing an example. A two dimensional array can be thought of as a grid or table with rows and columns that is declared as an array of arrays. The document demonstrates how to declare and initialize a two dimensional array at compile time by specifying the data type, array name, number of rows and columns, and initializing the values.
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 ratings0% found this document useful (0 votes)
14 views10 pages
Two Dimensoinal Array
The document discusses two dimensional arrays in C++, including what they are, how to declare and initialize them, and providing an example. A two dimensional array can be thought of as a grid or table with rows and columns that is declared as an array of arrays. The document demonstrates how to declare and initialize a two dimensional array at compile time by specifying the data type, array name, number of rows and columns, and initializing the values.
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/ 10
UNIT1 :
Two Dimensional Array in C+++
Adjectives of this unit - What is two dimensional array ? - Declaration of two dimensional array . - Initializing two dimensional array . - Example of two dimensional array.
T.SUB: Abdulmalik A Alsarori
Two Dimensional Array in C++
The two-dimensional array can be defined as
an array of arrays. The 2D array is organized as matrices which can be represented as the collection of rows and columns. However, 2D arrays are created to implement a relational database lookalike data structure. It provides ease of holding the bulk of data at once which can be passed to any number of functions wherever required. Two Dimensional Array in C++ A one-dimensional array can be seen as data elements organised in a row. A two- dimensional array is similar to a one- dimensional array, but it can be visualised as a grid (or table) with rows and columns. Declaration of two dimensional Array in C++
To declare an array in C++, you need to
define - The data type of elements in the array - The name of the array - The size of the array Syntax: Data_type Array_Name [rows][columns]; Note : size of rows and columns are any positive integer or constant variable. Example For Example: define size of rows =3 define size of columns =3 int arr[SIZE]; Suppose, we have array int score[2][3] as shown in below figure: Initializing Two Dimensional Array we can initialize one-dimensional array at compile time and at run time. Compile Time Initialization: Arrays can be initialized at the time they are declared. This is also known as compile-time initialization. - int a[2][3] = { {0, 2, 1} , /* row at index 0 */ {4, 3, 7} , /* row at index 1 */ }; Initializing Two Dimensional Array There are two ways in which a Two-Dimensional array can be initialized. - First Method. - Better Method. First method
elements in the braces from left to right are stored in the table also from left to right. The elements will be filled in the array in the order, first 4 elements from the left in first row, next 4 elements in second row and so on. Better Method int x[3][4] = {{0,1,2,3}, {4,5,6,7}, {8,9,10,11}};
This type of initialization make use of nested
braces. Each set of inner braces represents one row. In the above example there are total three rows so there are three sets of inner braces. Initializing Two Dimensional Array Run Time Initialization: An array can be explicitly initialized at run time. This approach is usually applied for initializing large arrays. example: int x[3][2]; for (int i = 0; i < 3; i++) { for (int j = 0; j < 2; j++) { cout << "Element at x[" << i << "][" << j << "]: "; cout << x[i][j]<<endl; } } Initializing Two Dimensional Array matrix x output