Introduction to Arrays | ||||
|
||||
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 | ||||
|
||||
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 | ||||
A 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) |