In the previous post we learn about what is array and what is one dimensional array.In this post we will learn what is two dimensional array.
What is Array:
- Array is a collection of similar type of data type.
- Array is used to store more than one values of same data type under a single variable name in a continuous memory location.
What is two dimensional Array:
- Two dimensional array consist of number of rows and number of columns.
- Each row consist of specified number of columns.
- For accessing element in two dimensional array we have to provide array name followed by row number and column number.
- If we skip the column number then specified row number and first column that is zero index value is accessed.
- If we skip both column and row then first row (index zero) and first column (index zero) value is accessed.
How to declare two dimensional Array:
- To declaring two dimensional array we have to write data type followed by followed by array name followed by total number of rows and followed by total number of columns in each row.
- Row and column numbers are specified in separate square brackets ([ ]).
Syntax: Data type Array name [row][column];
Example:int num[5][5];
How to initialize two dimensional Array:
- We can assign the values to array elements at the time of declaration of array.
- Each rows values are enclosed within separate inner parenthesis and two values are separated by comma (,).
Syntax: data type arrayname [row][column]={0th row elements}
{1st row elements}
.....
};
Example: int num [5][5]={{1,2,3,4,5},{2,4,6,7,8},{5,6,7,8,9,}};
.....
};
Example: int num [5][5]={{1,2,3,4,5},{2,4,6,7,8},{5,6,7,8,9,}};
0 comments:
Post a Comment