2d or 2 dimensional arrays
TRANSCRIPT
8/7/2019 2D or 2 Dimensional Arrays
http://slidepdf.com/reader/full/2d-or-2-dimensional-arrays 1/20
2D or 2DimensionalArrays
By : Falak Singhal
X1 B
8/7/2019 2D or 2 Dimensional Arrays
http://slidepdf.com/reader/full/2d-or-2-dimensional-arrays 2/20
Introduction with 2d Arrays…
8/7/2019 2D or 2 Dimensional Arrays
http://slidepdf.com/reader/full/2d-or-2-dimensional-arrays 3/20
What is an array???
“An array is a continued memory allocation that can store manyvalues in memory using one common basic identifier and theindividual values are identified by a number of that allocation”
Individual elements are identified by integer subscripts whichare enclosed in square brackets [] following the array variable.The xi in mathematics is written as x[i] in C++ and most other
programming languages.
E.g. : int abc[10] //declares an array of of int type variables undercommon identifier “abc”.
8/7/2019 2D or 2 Dimensional Arrays
http://slidepdf.com/reader/full/2d-or-2-dimensional-arrays 4/20
What is an 2d array???
In common language –” Data that is in rowsand columns is usually stored in 2-dimensionalarrays”
Or“ A 2 dimensional array is an array in whichthere is a collection of one dimensional arrays”
e.g. : int abc[3][2] ; //declares an 2d array of
three 1d
arrays having 2 elements each
8/7/2019 2D or 2 Dimensional Arrays
http://slidepdf.com/reader/full/2d-or-2-dimensional-arrays 5/20
Declaration of 2-dimensional -arrays…
8/7/2019 2D or 2 Dimensional Arrays
http://slidepdf.com/reader/full/2d-or-2-dimensional-arrays 6/20
Two-dimensional arrays aredeclared by specifying the numberof rows then the number of columns in the following fashion-
<datatype> <identifier> [rows]
[columns]e.g. : int abc[5][4];
8/7/2019 2D or 2 Dimensional Arrays
http://slidepdf.com/reader/full/2d-or-2-dimensional-arrays 7/20
The declaration int abc [5][4] will producethe following type of memory allocation :
COLUMN
ROW
0
1
2
3
4
0
1
2
3
8/7/2019 2D or 2 Dimensional Arrays
http://slidepdf.com/reader/full/2d-or-2-dimensional-arrays 8/20
Initializing 2-dimensional -arrays…
8/7/2019 2D or 2 Dimensional Arrays
http://slidepdf.com/reader/full/2d-or-2-dimensional-arrays 9/20
Unless specified, all initial values of arrays aregarbage. You can specify initial values byenclosing each row in curly braces like this:
char ttt[3][3] = {{'x', 'x', 'o'},{'o', 'o', 'x'},{'x','o', ' '} };
Note that - If some elements are omitted in the
initialization list, they are set to zero.
8/7/2019 2D or 2 Dimensional Arrays
http://slidepdf.com/reader/full/2d-or-2-dimensional-arrays 10/20
char ttt[3][3] = {{'x', 'x', 'o'},{'o','o', 'x'},{'x', 'o', ' '}};
8/7/2019 2D or 2 Dimensional Arrays
http://slidepdf.com/reader/full/2d-or-2-dimensional-arrays 11/20
int nums[5][4];
8/7/2019 2D or 2 Dimensional Arrays
http://slidepdf.com/reader/full/2d-or-2-dimensional-arrays 12/20
Processing 2-dimensional-arrays…
8/7/2019 2D or 2 Dimensional Arrays
http://slidepdf.com/reader/full/2d-or-2-dimensional-arrays 13/20
To read or process a 2d array, you need touse nested loops
One loop processes the row and the other,
the columns. If the outer loop is for rowsand the inner loop is for columns, then foreach row index, all columns are processedand then the same process is repeated forthe next row index.
Reading the array
8/7/2019 2D or 2 Dimensional Arrays
http://slidepdf.com/reader/full/2d-or-2-dimensional-arrays 14/20
e.g. : int a[2][3]; //array with 2 rows & 3 columnsint i,j;
for(i=0 ; i < 2 ; i++) //loop to control rows
{
for(j=0 ; j < 3 ; j++) //loop to control columns
{ cout <<“enter element “ ;cin >>a[i][j];
}
}
8/7/2019 2D or 2 Dimensional Arrays
http://slidepdf.com/reader/full/2d-or-2-dimensional-arrays 15/20
a [0] [0]
a [0] [1]
a [0] [2]
The previous code reads values for all elements. It first reads all columns for row 0
and similarly continues…
Single memory allocation
8/7/2019 2D or 2 Dimensional Arrays
http://slidepdf.com/reader/full/2d-or-2-dimensional-arrays 16/20
Application of 2Darrays
8/7/2019 2D or 2 Dimensional Arrays
http://slidepdf.com/reader/full/2d-or-2-dimensional-arrays 17/20
8/7/2019 2D or 2 Dimensional Arrays
http://slidepdf.com/reader/full/2d-or-2-dimensional-arrays 18/20
8/7/2019 2D or 2 Dimensional Arrays
http://slidepdf.com/reader/full/2d-or-2-dimensional-arrays 19/20
void display(int x[3][3])
{
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
cout<<x[i][j]<<'\t';
}cout<<endl;
}
}
8/7/2019 2D or 2 Dimensional Arrays
http://slidepdf.com/reader/full/2d-or-2-dimensional-arrays 20/20
void main()
{
clrscr();
int a[3][3],b[3][3],sumab[3][3];
accept(a);
clrscr();
accept(b);
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
sumab[i][j]=a[i][j]+b[i][j];
}
}
clrscr();
cout<<"I MATRIX"<<endl;
display(a);
cout<<"II matrix"<<endl;
display(b);
display(sumab);
getch();
}