like

Tuesday, December 20, 2011

FACTS ABOUT ARRAYS

1] An array is a collection of similar elements. It is also known as a subscripted variable.

2] Before using an array its type and size must be declared. For example
int arr[30];
float a[60];
char ch[25];

3] The first element in the array is numbered as 0, so the last element is 1 less than the seze of the array.

4] However big an array may be, its elements are always stored in contigouos locations.

5] If we desire an array can be initilised at the same place where it is declared. For example,

int num[6] = {2,3,12,5,45,5};
int n[] = {2,4,12,5,45,5};
float press[] = {12.3,34.2,-23.4,-11.3};

6] If the array is initialised where it is declared, mentioning the dimension of the array is optional as in 2nd example above.

7] Till the array elements are not given any specific values, they are supposed to contain garbage values.

8] In 'C' there is no check to see if the subscript used for an array exceeds the size of the array. Data entered with a subscript exceeding the array size will simply be placed in memory outside the array; porbably on top of other data, or on the porgram itself. This will lead to unpredictable results, to say the least, and there will be no error message to warn you that you are going beyond the array size. In some cases the computer may just hang. Thus, the following program may turn out to be suicidal;:

/* program */
main()
{
int num[40];
for(i = 0;i<= 100;i++)
num[i] = i;
}

So do remember that, to see to it that we do not reach beyond the array size is entirely the programmer's botheraation and not the compiler's.

While accessing array elements call by reference method is faster as compared to accessing elements by subscripts. However for convenience in programming we should observe the following :

Array elements should be accessed using pointers, if the elements are to be accessed in a fixed order, say form beginning to end, or form end to beginning, or every alternate element or any such definite logic.

It would be easier to access the elements using a subscript if there is no fixed logic in accessing the elements. However, in this case also, accessing the elements by pointers would work faster than subscripts.

No comments:

Post a Comment