2D array in C++ language ,define the concept of c++ Two-Dimensional array .with example .and also Accessing Array Components concept.and Processing Two-Dimensional Arrays.
2. Array:
Consecutive group of memory locations
― All of which have the same type
Index:
Position number used to refer to a specific
location/element
Place in square brackets
― Must be positive integer or integer expression
First element has index zero
4. Syntax:
The syntax for declaring a two-dimensional array
is:
DataType ArrayName[rowsize][colsize];
where rowsize and colsize are expressions yielding
positive integer values
Example: int data[3][4];
― Data is a TWO Dimensional array of size 3*4
elements
5. The two expressions rowsize and colsize specify
the number of rows and the number of columns,
respectively, in the array
Two-dimensional arrays are sometimes called
matrices or tables
Multiple arrays of the same type can be declared
in a single declaration
―Use a comma-separated list of names and
sizes
8. Accessing Array Components
The syntax to access a component of a two-
dimensional array is:
arrayName[indexexp1][indexexp2]
where indexexp1 and indexexp2 are expressions
yielding nonnegative integer values
indexexp1 specifies the row position and
indexexp2 specifies the column position
10. Processing Two-Dimensional
Arrays:
A two-dimensional array can be processed
in three different ways:
1. Process the entire array
2. Process a particular row of the array, called
row processing
3. Process a particular column of the array,
called column processing
11. Each row and each column of a two-dimensional
array is a one-dimensional array
When processing a particular row or column of a
two-dimensional array
― we use algorithms similar to processing one-
dimensional arrays
18. Example
Write a function in c++ which accepts a 2D array of integers
and its size as arguments and displays the elements of middle
row and the elements of middle column.
const int S = 5;
void DisplayMidle( int A[S][S], int S)
{ int mid = S/2;
int i;
cout << “ n Middle row”;
for (i=0 ; i<S; i++)
cout << A[ mid ][ i ]<< “ “;
cout<< “ n Middle Column ”;
for (i = 0; i<S; i++)
cout<<A[i][ mid ]<< “ “
cout << endl;
}