Search This Blog

Wednesday, 28 December 2011

Creating and Using Arrays in C!

0 comments

ARRAY HANDLING IN C!
An array is a group of logically related items of same data type, addressed by a common name. All the elements of an array are stored in contiguous (physically adjacent) memory locations. Array is defined abstractly as a finite ordered set of homogeneous elements.
The word ‘finite’ in the above definition indicates that there is a specific number of elements in the array. The word ‘ordered’ means the elements of the array are arranged so that there is a zeroth, first, second, third and so forth. By ‘homogeneous’ we mean that all the elements in the array must be of the same type.
Using array, we can store and manipulate a collection of data items of same data type. For instance, storing and retrieving the marks of ten subjects can be easily done with the help of an array of integers with ten elements:
int marks[10];
The array declared above takes ten consecutive memory locations, each 2 bytes long for storing 10 integers referring marks of 10 subjects.
Array is also considered as a derived data type, because it is derived from a primitive data type provided by C. The simplest form of an array is one-dimensional array. An example for one-dimensional array is a String of characters of size n, where n is the number of characters in the string.
Single and Multidimensional Arrays
Arrays are basically classified into Single and Multidimensional arrays, based on the number of dimensions (directions) they have for representing the data logically. Though the elements of an array are represented logically as a vector (single dimensional array) or a matrix (two-dimensional array), they are stored internally (physically) sequentially in consecutive memory locations. Multi dimensional arrays (for instance, cube) use more than two dimensions for representing their data logically.
Declaring and Initializing an Array
Array must be declared first, before its use in a program. The syntax of declaring an array is shown below:
Datatype ArrayName[array_size1], [[array_size2], [array_size3], …, [array_sizeN];
Array declaration involves three details:
Data type is any one of the primitive or user-defined type
Name is a valid identifier
Array size is an integer evaluates to a positive (+ive) integer and is enclosed in square brackets.
Consider the following C statement, which declares an array of 100 integers:
int a[100];
The following are some more examples for valid array declarations:
              float salary[25];
       char name[50];
       int marks[4][3];
Arrays can be initialized at the point of their declaration: The syntax for assigning values to one-dimensional array at the time of its declaration is as follows:
Data-type Array-name[size] = {list of values separated by comma};
For instance, the statement:
int age[5] = {19, 21, 16, 11, 50};
defines an array of 5 integers and then initializes its elements one by one with the values given with in braces. As the array index starts from 0, age[0] is assigned with 19, age[1] with 21, age[2] with 16, age[3] with 11 and age[4] with 50.
The array size may be omitted, when the array is initialized with values during its definition as follows:
int age[] = {19, 21, 16, 11, 50];
In such cases, the compiler assumes the array size to be equal to the number of elements enclosed within the curly braces. Hence, the above statement declares the array age with 5 elements and initializes them with the corresponding initial values.

Here is a Presentation on Creating and Using Arrays in C!
Initializing Two-dimensional Array
The syntax for initializing two-dimensional array is as follows:
Data-type Array-name[row-size][col-size] =
{{elements of row 1}, {elements of row 2}, …, {elements of row N}}
For instance, consider the following statement:
int a[3][3] = {{1,2,3}, {4,3,1}, {3,1,2}};
This statement defines a two-dimensional array named a of order 3X3 and initializes its elements row by row.  The inner braces can be omitted, permitting the members to be written in one continuous sequence as follows:
int a[3][3] = {1,2,3,4,3,1,3,1,2};
Manipulating an Array
There are two basic operations that can be done on arrays: Storing and Extracting items from array.  The storage operation is a function which accepts an array a, an index i, and an element x and stores the value of x in a[i].  This operation is expressed as a[i] = x; ie., the value of x is assigned to the ith of array a.
The extraction operation accepts an array a, and an index i, and returns an element of the array.  It is expressed in C as a[i].  The following piece of code illustrates this:
int i, marks[10], total=0;
for(i=0; I < 10; i++)
{
printf (“Enter the Mark %d ”, i);
scanf(“%d”, &marks[i]);
total += marks[i];
}
In this sample code, three variables are declared: an array of marks, and two ordinary variables namely i and total. The variable i is used as an index for accessing various elements of array a.  The other variable total is used for storing the result of totaling all the marks storing in array a.
There are certain limitations in storing the elements of an array: it can store only a fixed number of elements. Its size or range is set by two limits called Upper Bound (UB) and Lower Bound (LB). The smallest element of an array’s index is called its Lower Bound and in C is always 0, and the highest element is called its Upper Bound.
If L is the lower bound of an array and U is the upper bound, the number of elements in the array, called its range is given by (U-L+1). For instance, if L is 0, and the U is 99, then the range is 99-0+1 = 100. An important feature of a C array is that neither the upper bound nor the lower bound may be changed during the time of program execution.

Leave a Reply