LAB 13 Arrays Ii: 13.1 Objectives
LAB 13 Arrays Ii: 13.1 Objectives
LAB 13 Arrays Ii: 13.1 Objectives
ARRAYS II
13.1 OBJECTIVES:
The simplest form of the multidimensional array is the two-dimensional array. A two-dimensional (2D)
array is, in essence, a list of one-dimensional arrays. To declare a 2D integer array of size x,y, you would
write something as follows:
type arrayName[x][y] ;
This can be visualized as a two dimensional display with the first index giving the number of rows and
the second index giving the number of columns. For example:
The above statement will take 4th element from the 3rd row of the array.
INITIALIZATION
Let’s look at the way how 2D array can be initialized. Following is an array with 3 rows and each row has
4 columns.
int a[3][4] = {
{0, 1, 2, 3}, /* initializers for row indexed by 0 */
{4, 5, 6, 7}, /* initializers for row indexed by 1 */
{8, 9, 10, 11} /* initializers for row indexed by 2 */
};
1
The nested braces, which indicate the intended row, are optional. The following initialization is equivalent
to previous example:
Activity 1
Run the code and observe the output of the following.
int main()
{
int my_array[4][4], index1, index2;
return 0;
This function accepts a parameter of type "array of int" called arg and an integer “S” which will be
providing the information of the size of the array. Let us suppose that we have an array declared as:
To pass this array to the function procedure the call would be like this:
2
When a 2D array parameter is given in a function heading or function declaration, the size of the first
dimension is not given, but the remaining dimension size must be given in square brackets. Since the
first dimension size is not given, you usually need an additional parameter of type int that gives the size
of this first dimension. Below is a function declaration with a two-dimensional array parameter p:
Activity 2
Write a C++ program to store the temperature of two different cities for a week and then display it.
Declare and use the following array for this purpose:
int temperature[CITY][WEEK];