Topic :Two-Dimensional Array - Declaring | Initializing | Using



Multidimensional Arrays:

Arrays can also be used to represent Multidimensional data, like a chess board, or like a tic-toe board. Multidimensional data just means that we have more than one index for it. The simplest form of the Multidimensional Array is the "Two Dimensional Array."



Two Dimensional Array:

A Two Dimensional Array is, in essence, a list of one-dimensional Arrays. When we declare a one-dimensional Array, we type a set of square brackets after the array identifier. To declare a two-dimensional array, we Need to type two sets of brackets after the Array identifier. and to declare a three-dimensional array, we Need to type three sets of brackets and so on.

in C++ we can define Multidimensional Arrays with any number of dimensions. The ANSI Standard stipulates a minimum of 256 dimensions but the total number of dimensions is in fact limited by the amount of memory available.



Multidimensional Arrays: Infobrother:

Since two dimensional Array is rectangular, there are two index that we must use to access it. one for the row, and one for the column. the first Value is for Row and second for Column.


a[ row ][ column ]


Declaring Two-Dimensional Arrays:

To declare a Two dimensional integer Array of size x,y, we would write something like this:


Data_Type    Array_Name [ x ][ y ];


The Array_size Must be an integer constant greater than zero and type can be any valid C++ Data_type. For Example: to declare an Two dimensional Array for "3 x 4" (Three Rows and 4 Column ) having variable_name "Chess" and data_type "int", use this statement:


int    Chess [ 3 ][ 4 ];


Initializing Two-Dimensional Arrays:

C++ does not need any special syntax to define Multidimensional Arrays. On the contrary, an n-dimensional Array is no different than an Array with only one dimension whose elements are (n-1)-dimensional arrays. Multidimensional Arrays may be initialized by specifying bracketed values for each row. Following is an Array with 3 Rows and each row have 4 columns.



int Chess[ 3 ][ 4 ] = 
{ 
	{ 0, 1, 2, 3 } 
	{ 4, 5, 6, 7 } 
	{ 8, 9, 10, 11 } 
};


The Nested braces, which indicate the intended row, are Optional. The following initialization is equivalent to Previous Example:


int   Chess[ 3 ][ 4 ] =   { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 };


/*Multidimensional Arrays: InfoBrother*/
 
#include <iostream>
using namespace std;
int main ()
{
 
   // an array with 3 rows and 4 columns (3x4).
   int chess[3][4]={ 
		      {0,1,2,3},  //initialization.
		      {4,5,6,7}, 
		      {8,9,10,11}
       		    }; 
 
    // output each array element's value
    for ( int row = 0; row < 3; row++ ) //display Rows: Rows is less than 3:
    for ( int col = 0; col < 4; col++ ) //display Column: Column is less than 4:
    {
 	cout << "chess[" << row << "][" << col << "]: ";
	cout << chess[row][col]<< endl;
    }
 
    return 0;
}


Accessing Two-Dimensional Arrays Elements:

Once an Array is filled with useful values, we can access and use an individual array element in the same manner we would access and use any single variable of the same type. Any Element is accessed by indexing the array name. This is done by using subscripts, i.e. Row index and column index of the Array. For Example:


int Item =  chess[ 2 ][ 3 ];

In the above statement, the Assignment (=) Operator will took 4th Element from the 3rd row of the Array and will assign to variable "Item". (you can verify it in the above diagram.)



/*Accessing Two-Dimensional Arrays Elements: InfoBrother */
 
#include <iostream>
using namespace std;
int main ()
{    
   // an array with 3 rows and 4 columns (3x4).
   int chess[3][4]={ 
  		      {0,1,2,3},  //initialization.
		      {4,5,6,7}, 
		      {8,9,10,11}
		   };
 
   // output each array element's value
   for ( int row = 0; row < 3; row++ ) //display Rows: Rows is less than 3:
   for ( int col = 0; col < 4; col++ ) //display Column: Column is less than 4:
   {
	cout << "chess[" << row << "][" << col << "]: ";
	cout << chess[row][col]<< endl;
   }
 
   int item = chess[2][3]; //it will took 4th element from 3rd row:
   cout<<"\n\n The item stored in chess[2][3] is: "<<item;
 
   return 0;
}






I Tried my Best to Provide you complete Information regarding this topic in very easy and conceptual way. but still if you have any Problem to understand this topic, or do you have any Questions, Feel Free to Ask Question. i'll do my best to Provide you what you need.

Sardar Omar.
InfoBrother





WRITE FOR INFOBROTHER

Advertising






Advertisement