LAB 13 Arrays Ii: 13.1 Objectives

Download as pdf or txt
Download as pdf or txt
You are on page 1of 3

LAB 13

ARRAYS II
13.1 OBJECTIVES:

- To introduce the concept of 2D arrays


- To learn how to pass an array to the functions

13.2 PRE-LAB READING

13.2.1 TWO-DIMENSIONAL ARRAYS

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:

int board [7] [7];


When we type the above statement the compiler will generate a 2D array named as board, which
consists of 7 rows and 7 columns. An element in 2-dimensional array is accessed by using the subscripts,
i.e., row index and column index of the array.

int val = board [2] [3];

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:

int a[3][4] = {0,1,2,3,4,5,6,7,8,9,10,11};

Activity 1
Run the code and observe the output of the following.

int main()
{
int my_array[4][4], index1, index2;

for (index1 = 0; index1 < 4; index1++)


{
for (index2 = 0; index2 < 4; index2++)
my_array[index1][index2] = index2;
}

for (index1 = 0; index1 < 4; index1++)


{
for (index2 = 0; index2 < 4; index2++)
cout << my_array[index1][index2] << " ";
cout << endl;
}

return 0;

13.2.2 PASSING ARRAYS TO FUNCTIONS


To pass an array as a parameter for a function, the parameter can be defined as an array with empty
brackets, omitting the actual size of the array. Instead the size of the array is used as a separate
parameter. For example:

void procedure (int arg[], int S);

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:

int myarray [40];

To pass this array to the function procedure the call would be like this:

procedure (myarray, 40);

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:

void get_page( char p[][100], int size_dimension_1);

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];

You might also like