Introduction

An array is defined as a set of homogeneous data items. The ability to use a single name to represent a collection of items and to refer to an item by specifying the item number enables one to develop efficient programs. Consider an example of finding the greatest among three numbers, number1, number2 and number3. The coding will be as given below

Example 3.1

#include <stdio.h>
int main( ) 
{
        int number1, nuber2, number3; 
        int great = 0; 
        printf( "Enter any three numbers \n"); 
        scanf( "%d%d%d", &number1, &number2, &number3); 
        if (number1 <= number2 && number3 <= number)
              great = number2: 
         else
              if (number1 <= number3 && number2 <= number3) 
                    great number3; 
               else
                    great = number1 ;
                    printf( "The greatest of three numbers is %d", great);
}

Finding out the greatest among three numbers may not be a cumbersome task. Imagine the task of finding the greatest among ten numbers. This could be done by setting up a series of if Statements to compare each of the ten numbers.

The same problem could be solved if one were to introduce arrays in the place of individual

numbers - that is, by declaring a homogenous group of number values (integer, in this

case).

In C, we can define a variable called number [10] which represents an entire set of 10

numbers. Each element of the set can then be referenced by means of a number called an

index number of subscript.

Example 3.2

#include <stdio.h>
int main() 
{
    int number [10]:
    int great, i;
    printf ("Enter any ten numbers \n")
    for (i = 0; i < 10; i++)
    {
        scanf ("%d", &number[i]);
    }
    great = number[0];
    for (i = l; i < 10; i++)
    {
        if (great < number[i])
        great = number[i] ;
    }
    printf ("The greatest number is %d", great);
}

The complete set of values is referred to as an array and the individual values are called

elements. An array has the following properties

  • The type of an array is the data type of its elements.
  • The location of an array is the location of its first element.
  • The length of an array is the number of data elements in the array.
  • The size of the array is the length of the array times the size of the element.

Arrays whose elements are specified by one subscript are called single subscripted, linear or single dimensional arrays. Corresponding arrays whose elements are specified by two and three subscripts are called two-dimensional or double subscripted and three dimensional or triple-subscripted arrays respectively.