You are on page 1of 2

Experiment no. 3.

TO PERFORM DECLARATION, INITIALIZATION AND ACCESSING OF


TWO DIMENSIONAL ARRAYS

OBJECTIVES:
· To declare, initialize and access two dimensional arrays.
· To write simple programs utilizing two dimensional arrays in C.

THEORY:
Arrays a kind of data structure that can store a fixed-size sequential collection of elements of the same
type. A two-dimensional array is like a table and two-dimensional arrays are sometimes referred to as
matrices.

Declaration
Two dimensional arrays are defined in much the same manner as one-dimensional arrays, except that
each array name must be accompanied by two size specifications. For a two-dimensional array, the size
is specified by two positive integer expressions, enclosed in square brackets. In general terms, a two-
dimensional array declaration may be expressed as:
data- type array[ rows ] [ columns ] ;

Initialization
Arrays may be initialized when they are declared. The initialization data is placed in curly {} braces
following the equals sign. An array may be partially initialized, by providing fewer data items than the
size of the array. The initialization maybe expressed as:
array[ rows ][ columns ] = { {value01, value02, value03, …}, {value11, value12, value13}… };

Accessing
A specific element in an array is accessed by an index. In general terms, an element of a one
dimensional can be accessed as:
array[ row_index ][ column_index] ;

DEMONSTRATION:
Program 1 : To print the elements entered in 2d array.

#include<stdio.h>
#define r 3
#define c 4
int main()
{
int arr[r][c], i, j;

for(i = 0; i < r; i++)


{
for(j = 0; j < c; j++)
{
printf("Enter arr[%d][%d]: ", i, j);
scanf("%d", &arr[i][j]);
Experiment no. 3.2

}
}

printf("\nEntered 2-D array is: \n\n");

for(i = 0; i < r; i++)


{
for(j = 0; j < c; j++)
{
printf("%3d ", arr[i][j] );
}
printf("\n");
}
return 0;
}

Output:

Discussion:
In the above program, we declared, initialized and accessed a two-dimensional array and printed the
elements stored in 2D array.

Conclusion:
Hence, from the above experiment we are declare, initialize and access two dimensional arrays and use
them to write programs in C programming language.

You might also like