Sie sind auf Seite 1von 20

2D or 2Dimensional

Arrays
By : Falak Singhal
X1 B
Introduction with 2d Arrays…
What is an array???

“An array is a continued memory allocation that can store many


values in memory using one common basic identifier and the
individual values are identified by a number of that allocation”
Individual elements are identified by integer subscripts which
are 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 under
common identifier “abc”.
What is an 2d array???

In common language –” Data that is in rows


and columns is usually stored in 2-dimensional
arrays”
Or
“ A 2 dimensional array is an array in which
there 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
Declaration of 2-dimensional -
arrays…
Two-dimensional arrays are
declared by specifying the number
of rows then the number of
columns in the following fashion-
<datatype> <identifier> [rows]
[columns]
e.g. : int abc[5][4];
The declaration int abc [5][4] will produce
the following type of memory allocation :
COLUMN
ROW 0 3
0 1 2
1
2
3
4
Initializing 2-dimensional -
arrays…
Unless specified, all initial values of arrays are
garbage. You can specify initial values by
enclosing 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.
char ttt[3][3] = {{'x', 'x', 'o'},{'o',
'o', 'x'},{'x', 'o', ' '}};
int nums[5][4];
Processing 2-dimensional-
arrays…
Reading the array

To read or process a 2d array, you need to


use nested loops
One loop processes the row and the other,
the columns. If the outer loop is for rows
and the inner loop is for columns, then for
each row index, all columns are processed
and then the same process is repeated for
the next row index.
e.g. : int a[2][3]; //array with 2 rows & 3 columns
int 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];
}
}
The previous code reads values for
all elements. It first reads all columns for row 0
and similarly continues…

a [0] [0] Single memory allocation


a [0] [1]
a [0] [2]
Application of 2D
arrays
Matrices as 2D arrays

Matrix is a useful application


of 2d arrays
Two or more C++ 2d arrays
can be used to perform
mathematical operations on
the arrays visualizing them as
separate matrices.
E.g.: addition of two matrices
#include<iostream.h>
#include<conio.h>
void accept(int a[3][3])
{
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
cout<<"enter the matrix element ";
cin>>a[i][j];
}
}
}
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;
}
}
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();

Das könnte Ihnen auch gefallen