Introduction to Arrays

Introduction to Arrays
An array is a data structure used to process multiple elements with the same data type when a number of such elements are known. You would use an array when, for example, you want to find out the average grades of a class based on the grades of 50 students in the class. Here you cannot define 50 variables and add their grades. This is not practical. Using an array, you can store grades of 50 students in one entity, say grades, and you can access each entity by using subscript as grades[1]grades[2]. Thus you have to define the array of grades of the float data type and a size of 50. An array is a composite data structure; that means it had to be constructed from basic data types such as array integers.
An array is a fixed-sizedhomogeneous, and widely-used data structure. By homogeneous, we mean that it consists of components which are all of the same type, called element type or base type. And by fixed sized, we mean that the number of components is constant, and so does not change during the lifetime of the structure. An array is also called a random-access data structure, because all components can be selected at random and are equally accessible. An array can be used to structure several data objects in the programming languages. A component of an array is selected by giving its subscript, which is an integer indicating the position of the component in the sequence. Therefore, an array is made of the pairs (valueindex); it means that with every index, a value is associated. If every index is one single value then it is called a one-dimensional array, whereas if every index is a n-tuple {i1, i2, i3,….., in}, the array is called a n-dimensional array.
Program
#include <stdio.h>

main()

{

int a[5];  \\A

for(int i = 0;i<5;i++)

{

a[i]=i;\\B

}

printarr(a);

}

void printarr(int a[])

{

for(int i = 0;i<5;i++)

{

printf(“value in array %d\n”,a[i]);

}

}

Explanation
  1. Statement A defines an array of integers. The array is of the size 5—that means you can store 5 integers.
  2. Array elements are referred to using subscript; the lowest subscript is always 0 and the highest subscript is (size –1). If you refer to an array element by using an out-of-range subscript, you will get an error. You can refer to any element as a[0]a[1]a[2], etc.
  3. Generally, you can use a for loop for processing an array. For the array, consecutive memory locations are allocated and the size of each element is same.
  4. The array name, for example, a, is a pointer constant, and you can pass the array name to the function and manipulate array elements in the function. An array is always processed element by element.
  5. When defining the array, the size should be known.
Memory Representation
An array is represented in memory by using a sequential mapping. The basic characteristic of the sequential mapping is that every element is at a fixed distance apart. Therefore, if the ith element is mapped into a location having an address a, then the (i + 1)th element is mapped into the
memory location having an address (a + 1), as shown in Representation of an array.
The address of the first element of an array is called the base address, so the address of the the ith element is Base address + offset of the ith element from base address where the offset is computed as:
Offset of the ith element = number of elements before the ith * size of each element.
If LB is the lower bound, then the offset computation becomes:
offset = (i – LB) * size.
Representation of Two-Dimensional Array
two-dimensional array can be considered as a one-dimensional array whose elements are also one-dimensional arrays. So, we can view a two dimensional array as one single column of rows and map it sequentially as shown in image below . Such a representation is called a row-major representation.
Row-major representation of a two-dimensional array
The address of the element of the ith row and the jth column therefore is:
addr(a[i, j]) = ( number of rows placed before ith row * size of a row) + (number of elements placed before the jth element in the ith row * size of element)
where:
Number of rows placed before ith row = (i – LB1), and LB1 is the lower bound of the first dimension.
Size of a row = number of elements in a row * a size of element.
Number of elements in a row = (UB2 – LB2+1), where UB2 and LB2 are the upper and lower bounds of the second dimension, respectively.
Therefore:
addr(a[i, j]) = ((i – LB1) * (UB2 – LB2+1) * size) + ((j – LB2)*size)
It is also possible to view a two-dimensional array as one single row of columns and map it sequentially as shown in image below. Such a representation is called a column-major representation.
Column major representation of a two-dimensional array
The address of the element of the ith row and the jth column therefore is:
addr(a[i, j]) = ( number of columns placed before jth column * size of a column) + (number of elements placed before the ith element in the jth column * size of each element)
Number of columns placed before jth column = (j – LB2) where LB2 is the lower bound of the second dimension.
Size of a column = number of elements in a column * size of element Number of elements in a column = (UB1 – LB1 + 1), where UB1 and LB1 are the upper and lower bounds of the first dimension, respectively.
Therefore:
addr(a[i, j]) = ((j – LB2) * (UB1 – LB1+1) * size) + ((i – LB1)*size)