Module 5. Arrays

 

Lesson 15

ARRAYS in ‘C’ – PART I

(Single-Dimensional Arrays: Declaring and Assigning Initial Values)

15.1  Arrays

Arrays are the data structure, which hold multiple variables of the same data type; and each element of the set can then be referenced by means of a number called index number or subscript. In array elements, index or subscript begins with number zero. An array variable name like other variables in ‘C’ must be declared before it is used. In other words, an array in ‘C’ can be defined as a number of memory locations each of which can store the same data type and that can be referenced through the same variable name. In array elements, index or subscript begins with number zero and takes values up to 1 less than maximum size of the array. The general syntax:

type variable-name[Size];

The keyword ‘type’ specifies the data type of the elements that will be contained in the array, such as int, float or char and the Size (enclosed between square braces) indicates the maximum number of elements that can be stored inside the array, i.e., from ‘0’ to (Size-1). For instance, consider the following statement:

float height[50];

This example declares the variable, height to be an array containing 50 elements of floating-point data type. Any subscripts within the range 0 to 49 are valid, e.g., height[0] refers to the first element and height[49] refers to the last element of the array.

15.2  Declaring Arrays

Arrays may consist of any of the valid data types. Arrays are declared along with the other variables in the declaration section of the programme.

An illustration

int numbers[100];

numbers[2]=10;

--numbers[2];

printf(“The third element of the array, numbers is %d\n”, numbers[2]);

Output:

The third element of the array, numbers is 9.

The above programme segment declares an array, numbers of size 100 elements; assigns the value of 10 to the third element of the array, numbers; followed by decrementing this value by one; and finally prints the value of the third element of the array, numbers

15.3 Assigning Initial Values to Arrays

External and static variables can be initialised if so desired. The initial values are enclosed in curly brackets and must appear in the same order as they will be assigned elements. The general syntax is:       

type array _name[size] = {element_1, element_2, …, element­­_n}

An illustration

int x;

static int values[] = {1,2,3,4,5,6,7,8,9};

for(x = 0; x < 9; ++x)

printf("Values [%d] = %d\n", x, values[x]);

Output:

Values [0] = 1

Values [1] = 2

.

.

.

Values [8] = 9.

This programme declares an array, values. Note that inside the square brackets, there is no variable to indicate as to how big the array will be. In this case, ‘C’ initialises the array to the number of elements that appear within the square braces. For instance, the array, values consists of nine elements (0 to 8).

Example 1:

Programme to demonstrate one dimensional array. It accepts three numbers to be entered through the keyboard at run-time; stores these numbers in a one-dimensional array a; and finally, prints the array a.

#include <stdio.h>

#include <conio.h>

void main()

{

      int a[3], i;;

      clrscr();

      printf("\n\t Enter three numbers : ");

      for(i=0; i<3; i++)

      {

            scanf("%d", &a[i]);  // read array

      }

      printf("\n\n\t Numbers are : ");

      for(i=0; i<3; i++)

      {

            printf("\t %d", a[i]);  // print array

      }

      getch();

}

Test Output

Enter three numbers : 9 4 6

Numbers are :     9     4     6

Example 2:

Programme to sort a given array of numbers into ascending order using insertion sort method (See Example-6 of Lesson-2 for pseudo code).

#include<stdio.h>

#include<conio.h>

   void insertion(int a[],int n)

   {

    int i,j,x,k;

    for(i=1;i<=n-1;i++)

    {

       j=i;

       x=a[i];

       while(a[j-1]>x && j>0)

       {

           a[j]=a[j-1];

           j=j-1;

       }

       a[j]=x;

       printf("\n\n The array after pass no.%d: ",i);

       for(k=0;k<=n-1;k++)

          printf("%4d",a[k]);

    }//end for.

   } //end function.

   void main()

   {

     int a[1000],n,i;

     clrscr();

     printf("\n\nEnter an integer value for total nos. of

             elements to be sorted: ");

     scanf("%3d",&n);

     for(i=0;i<=n-1;i++)

     {

       printf("\n\nEnter an integer value for element

              no.%d:",i+1);

       scanf("%4d",&a[i]);

     }

     insertion(a,n);

     printf("\n\n\nFinally sorted array is : ");

     for(i=0;i<=n-1;i++)

        printf("%4d",a[i]);

   }//end programme.

Test Output:

Enter an integer value for total no.s of elements to be sorted: 7

Enter an integer value for element no.1: 12

Enter an integer value for element no.2: 9

Enter an integer value for element no.3: 3

Enter an integer value for element no.4: 4

Enter an integer value for element no.5: 22

Enter an integer value for element no.6: 45

Enter an integer value for element no.7: 12

 The array after pass no.1:    9  12   3   4  22  45  12

 The array after pass no.2:    3   9  12   4  22  45  12

 The array after pass no.3:    3   4   9  12  22  45  12

 The array after pass no.4:    3   4   9  12  22  45  12

 The array after pass no.5:    3   4   9  12  22  45  12

 The array after pass no.6:    3   4   9  12  12  22  45

 Finally sorted array is :    3   4   9  12  12  22  45