Professional Documents
Culture Documents
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;
}
}
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.