Arrays

An array is finite homogeneous collection of elements stored in contiguous memory locations referred by same name.
  • This definition has the following parts: 
  • Array consists of homogenous collection of elements. That is, all the data items stored in array are of same type.
  • All elements are stored in contiguous locations. That is, all elements are stored one after other in successive locations of memory (based on size of type of data items). Of course, fixed amount of memory is allocated to an array.  
All elements refer to same name. That is, each element can be identified with the same name including different index value (subscript value). Hence, an array is also called as a subscripted variable.  By using, these index values, an element in the array can be accessed directly.
Based on number of square brackets (subscripts or dimensions), arrays are broadly classified into two categories:

I. One-dimensional arrays.
II. Multi-dimensional arrays.

One-dimensional array consists of only one subscript. Multi-dimensional arrays consist of more than one subscript. E.g., two-dimensional array consists of 2 subscripts; 3-dimensional array consists of 3 subscripts and so on. Usually, one-dimensional numeric array consists of numbers as individual elements where as multi-dimensional numeric array consists of arrays as individual elements. Hence, multi-dimensional array is also called as array of arrays.

One-Dimensional array: 

 Declaring one-dimensional array:
Just like an ordinary variable, an array should be declared before it is used.
A one-dimensional array can be declared as follows:


$ < $storage class$ > $    $ < $data type$ > $   $ > $[size];


In this syntax, $ < $ storage class $ > $ is any one of auto, static, extern or register. It specifies the storage location of an array, the default values of all elements, scope and life time of an array.


$ < $ data type $ > $ is any basic data type such as int, float, char or double or any derived data type such as long int, short int and so on.
$< $array_name$ > $ is any valid identifier.
Size is any non-negative integer constant or integer expression.


1. int a[20]; // one-dimensional array capable of holding 20 elements of type int
2. # define MAXSIZE 30
       float b[MAXSIZE]; //1-D array capable of holding 30 elements of type float
3. int k=20;
           int c[k];    //Error: Array size can’t be declared as a variable.

 Initializing one-dimensional array:

Initializing One-dimensional array is the process of assigning values to its all contiguous locations or to some of them with some initial values for avoiding garbage values those are already in it.  It can be directly done with the help of assignment operator.

A one-dimensional array is initialized as follows: array declaration follows assignment operator and a list of values that are separated by commas enclosed with in curly braces. This process is also called as “initialization at compile-time”.

Ex: int a[7]={12,45,54,2,87,76,3};

Once the above array gets initialized, the memory map for it will be as follows:


From this memory map, we can observe that an array subscript value (or index) starts from 0 and ends with n-1, where n is the size of the array. Usually, the first element’s location is called the base address of the array. Each element is uniquely identified with the help of array name and its subscript value – 1 or base address+ (pos-1) * sizeof(type). . e.g., 4th element of array ‘a’ is identified with a[3] or 1000+(4-1)*2=1006.

 Reading and printing one-dimensional array:

A One-Dimensional array’s locations can be filled with input values from keyboard with the help of scanf() statement. Because an array consists of more than one element, we should require a loop counter for keeping track of index values. The loop counter is always an integer value that begins with 0 and ends with n-1. The loop counter should be incremented by 1 for keeping track of next immediate indexes. For each indexed element, scanf() statement expects an input value from  programmer. This process can be depicted with the help of the following statement:

for(i=0;i$ < $;i++)  
scanf(“%d”,&a[i]);

The above statement first reads a[0], then a[1], a[2], a[3]…and so on. This process is also called as “initialization at run-time”.

Once an array is read, it can be accessed directly or sequentially with the help of printf() statement. If we want an element at particular position, then we should give the following argument to printf() : a[position-1].

E.g., we want the element at 5th position, then we should give a[4] to printf() statement:


        printf(“Element at 5th position is %d”,a[4]);

If we want to access all the elements in the array sequentially, then we can use printf() statement along with a loop as follows:

printf(“\n The array elements are:”);
for(i=0;i
$ > $n;i++)
printf(“%d\t”,a[i]);

Two-Dimensional arrays: 

A Two-Dimensional array looks like array name followed by two subscripts. It is very helpful to represent matrices in mathematics or tables in business. The first subscript is used to denote the Number of rows and second is used to denote the number of columns. The total number of elements a two-dimensional array holds is the product of values of both subscript values.

 Declaring Two-Dimensional array: 

Just like an ordinary variable, an array should be declared before it is used. A two-dimensional array can be declared as follows:



$ < $data type$ > $  $ < $array_name$ > $[row][column];


In this syntax,
is any basic data type such as int, float, char or double or any derived data type such as long int, short int and so on.
$ < $data type$ > $ is any basic data type such as int, float, char or double or any derived data type such as long int, short int and so on.
$ < $array_name$ > $ is any valid identifier.

row and column values are  any non-negative integer constants or integer expressions.
Ex:  int a[5][3]; // two-dimensional array capable of holding 15 (5*3) elements of type int
       # define ROW 10
       # define COLUMN 5
float b[ROW][COLUMN]; //2-D array capable of holding 15 elements of type float

Initializing Two-Dimensional array: 

Initializing Two-dimensional array is the process of assigning values to its all contiguous locations or to some of them with some initial values for avoiding garbage values those are already in it.  It can be directly done with the help of assignment operator.
A Two-dimensional array is initialized as follows: array declaration follows assignment operator and a list of values that are separated by commas enclosed with in curly braces.
Ex: int a[3][4]={{12,45,54,2},{87,76,3,4},{54,64,34,23}};
Once the above array gets initialized, the values will be stored in memory. The following memory map depicts this thing:
From this memory map, we can observe that a Two-Dimensional array’s row’s subscript value as well as column’s subscript’s value start from 0 and end with n-1.

 Reading and printing Two-Dimensional array: 

A Two-Dimensional array’s locations can be filled with input values from keyboard with the help of scanf() statement. Because an array consists of more than one element, we should require loop counters for keeping track of index values for both rows and columns. The loop counters are always integer values those begin with 0 and ends with n-1. The loop counters should be incremented by 1 for keeping track of next immediate indexes. For each indexed element, scanf() statement expects an input value from  programmer. This process can be depicted with the help of the following statement:

         for(i=0;i$ < $m;i++)     //  m: No.of rows i: loop counter for rows
                   for(j=0;j$ < $n;j++)     //  n: No.of columns  j: loop counter for columns
                            scanf(“%d”,&a[i][j]);

The above statement first reads a[0][0], then a[0][1], a[0][2], a[0][3]…and so on.
Once an array is read, it can be accessed directly or sequentially with the help of printf() statement. If we want an element at particular position, then we should give the following argument to printf() : a[row-1][column-1]. E.g., we want the element at 3rd row and 2nd column position, then we should give a[2][1] to printf() statement:

printf(“Element at 3rd row and 2nd column position is %d”,a[2][1]);
We can access all the elements in the array sequentially. For this, we use printf() statement along with two loops as follows:

printf(“\n The array elements are:”);
 for(i=0;i$ < $m;i++)
 for(j=0;j$ < $n;j++)
                                                 printf(“%d\t”,a[i][j]);