238 Arrays Chapter 7 number of the element (Web site developers)

238 Arrays Chapter 7 number of the element in square brackets ([]). The first element in every array is the zeroth element. Thus, the first element of array cis referred to as c[0], the second element of array c is referred to as c[1], the seventh element of array c is referred to as c[6] and so on. The ith element of array c is referred to as c[i-1]. Array names follow the same conventions as other variable names, as discussed in Chapter 3, Introduction to C# Programming. The position number in square brackets is more formally 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, the program evaluates the expression first to determine the subscript. For example, if variable ais equal to 5 and 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 an lvalue it can be used on the left side of an assignment to place a new value into an array element. Let us examine array c in Fig. 7.1 more closely. The name of the array is c. Every array in C# knows its own length. The length of the array is determined by the expression: c.Length The array s 12 elements are referred to as c[0], c[1], c[2], , c[11]. The value of c[0] is -45, the value of c[1] is 6, the value of c[2] is 0, the value of c[7] is 62 and the value of c[11] is 78. To calculate the sum of the values contained in the first three elements of array c and to store the result in variable sum, we would write sum = c[ 0 ] + c[ 1 ] + c[ 2 ]; Name of array (Note that all elements of this array have the same name, c) Position number (index or subscript) of the element within array c c[ 0 ] c[ 1 ] c[ 2 ] c[ 3 ] c[ 4 ] c[ 5 ] c[ 6 ] c[ 7 ] c[ 8 ] c[ 9 ] c[ 10 ] c[ 11 ] -45 6 0 72 1543 -89 0 62 -3 1 6453 78 Fig. 7.1 A 12-element array.
Please visit our professional web hosting services to find out about cheap and reliable webhost service that will surely answer all your demands.

Leave a Reply