Wednesday, 27 February 2013

Arrays



 Arrays

Arrays are widely used data type in ‘C’ language. It is a collection of elements of similar data type. These similar elements could be of all integers, all floats or all characters. An array of character is called as string whereas and array of integer or float is simply called as an array. So array may be defined as a group of elements that share a common name and that are defined by position or index. The elements of an arrays are store in sequential order in memory.
There are mainly two types of Arrays are used:

  • One dimensional Array
  • Multidimensional Array

One dimensional Array
So far, we've been declaring simple variables: the declaration
                    int i;
declares a single variable, named i, of type int. It is also possible to declare an array of several elements. The declaration
                    int a[10];
declares an array, named a, consisting of ten elements, each of type int. Simply speaking, an array is a variable that can hold more than one value. You specify which of the several values you're referring to at any given time by using a numeric subscript. (Arrays in programming are similar to vectors or matrices in mathematics.) We can represent the array a above with a picture like this:
In C, arrays are zero-based: the ten elements of a 10-element array are numbered from 0 to 9. The subscript which specifies a single element of an array is simply an integer expression in square brackets. The first element of the array is a[0], the second element is a[1], etc. You can use these ``array subscript expressions'' anywhere you can use the name of a simple variable, for example: 
 
                    a[0] = 10;
                    a[1] = 20;
                    a[2] = a[0] + a[1];
Notice that the subscripted array references (i.e. expressions such as a[0] and a[1]) can appear on either side of the assignment operator. it is possible to init


int a[10] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
The list of values, enclosed in braces {}, separated by commas, provides the initial values for successive elements of the array.

The subscript does not have to be a constant like 0 or 1; it can be any integral expression. For example, it's common to loop over all elements of an array:
                    int i;
 
                    for(i = 0; i < 10; i = i + 1)
                                         a[i] = 0;
This loop sets all ten elements of the array a to 0.
Arrays are a real convenience for many problems, but there is not a lot that C will do with them for you automatically. In particular, you can neither set all elements of an array at once nor assign one array to another; both of the assignments
                    a = 0;                                                   /* WRONG */
and
                    int b[10];
                    b = a;                                                   /* WRONG */
are illegal.
To set all of the elements of an array to some value, you must do so one by one, as in the loop example above. To copy the contents of one array to another, you must again do so one by one:
                    int b[10];
 
                    for(i = 0; i < 10; i = i + 1)
                                         b[i] = a[i];
Remember that for an array declared
                    int a[10];
there is no element a[10]; the topmost element is a[9]. This is one reason that zero-based loops are also common in C. Note that the for loop
                    for(i = 0; i < 10; i = i + 1)
                                         ...
does just what you want in this case: it starts at 0, the number 10 suggests (correctly) that it goes through 10 iterations, but the less-than comparison means that the last trip through the loop has i set to 9. (The comparison i <= 9 would also work, but it would be less clear and therefore poorer style.)


No comments:

Post a Comment