Figure 10.1 shows an array of integer values named c. This array contains 12 ele-ments. Any one of these elementsmay be referred to by giving the name of the array fol-lowed by the position number of the element in square brackets (). The first element in every array is the zeroth element. Thus, the first element of array c is referred to as c, the second element of array c is referred to as c, the seventh element of array c is referred to as c and, in general, theith element of array c is referred to as c[i-1]. Array names follow the same conventions as other identifiers.
The position number in square brackets is called a subscript (or an index). A subscript must be an integer or an integer expression. If a program uses an expression as a subscript, then the expression is evaluated to determine the value of the subscript. For example, if we assume that variable a is equal to 5 and that variable b is equal to 6, then the statement
c[ a + b ] += 2;
adds 2 to array element c[ 11 ]. Note that a subscripted array name is a left-hand-side ex-pression—it can be used on the left side of an assignment to place a new value into an array
element. It can also be used on the right side of an assignment to assign its value to another left-hand side expression.
Let us examine array c in Fig. 10.1 more closely. The array’s name is c. The length of array c is 12 and can be found using by the following expression:
sum = c[ 0 ] + c[ 1 ] + c[ 2 ];
To divide the value of the seventh element of array c by 2 and assign the result to the vari-able x, we would write
x = c[ 6 ] / 2;
Copyright © 2018-2020 BrainKart.com; All Rights Reserved. Developed by Therithal info, Chennai.