Arrays

You have been discussing variables, where only one piece of data is used. In real life, one deals with a range or collection of data or values. How do we handle a collection of values?

A collection of values can be handled using arrays.

What are arrays?

Arrays allow us to handle a collection of values. Arrays allow us to refer and to manipulate the values as a collection and as individual values.

Could you define arrays in a different manner?

You can think of arrays as a labeled and numbered list, or as a matrix. There are one dimensional and two dimensional arrays. Two dimensional arrays are used to implement matrix, and will not be discussed.

How does an array get implemented in C?

An array can be implemented by
  • Declaring the type and size of the array
  • Assigning values to the array
  • Manipulating, calling or using the array values

    One important thing to note is that the array index always starts with zero. Thus, the maximum array index is always one less than the array size.

    Consider the following example for the exact syntax.

    /* Project:	Lab6 */
    /* File:	array.C */
    /* Aim:		Numeric Array */
    /* Keywords:	for */
    
    #include 
    
    main ()
    {
    	int array[5];    	/* The declaration of the array */
    	int i=0;            	/* Will be used to as index */
    
    	scanf("%d%d%d%d%d", &array[0], &array[1] 
    	                                  
    &array[2], &array[3], &array[4]); for (i=0; i<5; i=i+1) { printf ("The Array Value of Index %d %d \n", i, array[i]); } }