Sometimes we need to store data in the form of rows &
column (or matrix).
For example: there are 5 classes and in each class there are
60 students.
So we can use two-dimensional array. Yes! It is possible to
initialize two-dimensional array.
Declaration of 2D array:
data_type
array_name[row_size][column_size] ;
int Marks[4][3];
Row/comlumn
|
Column 0
|
Column 1
|
Column 2
|
Row 0
|
Marks[0][0]
|
Marks[0][1]
|
Marks[0][2]
|
Row 1
|
Marks[1][0]
|
Marks[1][1]
|
Marks[1][2]
|
Row 2
|
Marks[2][0]
|
Marks[2][1]
|
Marks[2][2]
|
Row 3
|
Marks[3][0]
|
Marks[3][1]
|
Marks[3][2]
|
This was the representation of 2D array. But in the memory
of the computer they are stored in some different manner (given below).
|
|
|
|
|
|
|
|
|
|
|
|
(0,0) (0,1) (0,2) (1,0) (1,1) (1,2) (2,0) (2,1) (2,2) (3,0) (3,1) (3,2)
Initialization of Two-dimensional Arrays:
There are multiple ways to initialize a 2D array.
1)
Same as one dimensional array:
int marks[2][3]= {1,2,3,4,5,6};
2)
Initialize row by row:
int marks[2][3]={ {1,2,3},{4,5,6} };
3)
Initialize each element one by one:
Marks[0][0]=1; marks[0][1]=2;
Marks[0][2]=3; marks[1][0]=4;
Marks[1][1]=5; marks[1][2]=6;
4)
You can also leave the size at the time of
initialization:
int marks[][3]={ {1,2,3},{4,5,6}};
5)
If you want to initialize all the members of the
array:
int marks[2][3]={0};
6)
If some values are missing then it is
automatically set to Zero:
int marks[2][3]={ {4,5,6} };
No comments:
Post a Comment