Arrays

An array is a data structure composed of a fixed number of components of the same type which are organised in a linear sequence. A component of an array is selected by assigning an integer value to its index (or subscript) which identifies the position of the component in the sequence.

In C, the arrays are intimately related to pointers. All sorts of array manipulations can also be performed by using pointers which will be discussed later.


8.1 ARRAY DECLARATION

The syntax of array declaration is as follows,

{} data type [expression]{[expression]};

Example 8.1 :

int arr[10];

The above declaration defines an array called arr of size 10, that is arr consists of 10 elements of same data type int. The elements occupy consecutive cells (every cell houses one element) and form an ordered set of elements. Each element can be identified by its position in the array, and that is called the subscript of an array. The first element is at position 0 and nth element can be found in the (n-1)th position.

The name of the array is arr, which contains the address of the first element(i.e. &arr[0]) of the array. However, an array name such as arr differs from an ordinary pointer variable (like int *p;), because, it is static in nature and cannot point to a new memory location other than what it is pointing to.

arr[0] arr[1] arr[9]





























More examples of array declarations :

static float grade[10];

char name[20];

8.2 ARRAY MANIPULATION

The most convenient way of performing array manipulation is to use the for repetitive construct in order to access every element of the array.

The following example illustrates the usage of an array in implementing addition of two vectors :

Example 8.2 :

# include

# define dimension 100

typedef int vector[dimension];

main()

{

vector vect_1,vect_2,result_vect;

int i;

printf(“Enter the vector dimension :”);

scanf(“%d”,&n);

fflush(stdin);

/*accepting values for two arrays vect_1, vect_2 */

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

{

printf(“Give vector element # %d :”,i + 1);

scanf(“%d %d”,&vect_1[i],&vect_2[i]);

fflush(stdin);

result_vect[i]=0;

/* initialising the elements of the array result_vect to 0 */

}

/*vector addition and creation of the resultant vector */

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

result_vect[i] += (vect_1[i] + vect_2[i] );

/* displaying the resultant matrix */

printf(“

”);

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

{

printf(“%d ”,result_vect[i]);

}

printf(“
”);

}

Array initialisation can be performed in the following way,

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

But this definition cum initialisation is permitted only in the form of external variable definition. To include the statement inside a function storage class clause static has to be used as a prefix. 1. Describe the output generated by every of the following program:

(a) # include

main ()

{

int a, sum = 0;

static int x[10] = { 9, 8, 7, 6, 5, 4, 3, 2, 0};

for (i = 0; i <>

if ((i % 2) ==0)

sum + = x[i];

printf (“%d”,sum);

}

(b) # include

# define ROWS 3

# define COLS 4

int x [ROWS] [COLS] = {12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1};

main()

{

int i, j, max;

for (i = 0; i <>

{

max =9999;

for (j = 0; j

if (x [i][j] <>

max=z[i][j];

printf(“%d”, max);

}

}

2. Identify the errors in the following C program (if any) which initialises an array such that every of its ten elements is assigned with 0 value.

main()

{

int num_arr[10], i=0;

:

for(;++i <>

}

3. Identify the array defined in every of the following statements. Indicate what values are assigned to the individual array elements.

(a) char game[7] = {‘C’, ‘R’, ‘I’, ‘C’, ‘K’, ‘E’, ‘T’};

(b) char match[]=“Football”;

4. Write an appropriate array definition for every of the following cases:

(a) Define a one dimensional, integer array called A with 10 elements and initialise the array elements with 2,5,8,11,........, 29.

(b) Create a one dimensional, four element character array called object and assign the characters ‘C’, ‘I’, ‘R’,’C’, ‘L’ and ‘E’ to the array elements.

(c) Define a one dimensional, six element floating point array called flt_const having following initials values :

2.005, -3.05452, -1e-4, 340.179, 0.3e8, 0.023415

8.3 STRING - CHARACTER ARRAYS

A string constant, enclosed within a pair of double quotes, consists of 0 (empty string) or more characters terminated by a null (‘

Related Posts by Categories

0 comments:

Post a Comment