Session 7

| Arrays Session 7 Objectives Explain array elements and indices Define an array Explain array handling in C Explain how an array is initialized Explain string / character arrays Explain two dimensional arrays Explain initialization of two dimensional arrays Array Elements & Indices Each member of an array is identified by unique index or subscript assigned to it The dimension of an array is determined by the number of indices needed to uniquely identify each element An index is a positive integer enclosed in [ ] placed immediately after the array name An index holds integer values starting with zero An array with 11 elements will look like - Player[0], player[1], player[2], . Player[10] . Defining an Array-1 An array has some particular characteristics and has to be defined with them These characteristics include – Storage Class Data Types of the elements in the Array Array Name Which indicates the location of the first member of the array Array Size a constant evaluating to a +ve value Defining an Array-2 An array is defined in the same way as a variable is defined. The only change is that the array name is followed by one or more expressions, enclosed within square brackets [], specifying the array dimension. Storage_Class data_types array_name[size] int player[11]; Norms with Arrays All elements of an array are of the same type Each element of an array can be used wherever a variable is allowed or required Each element of an array can be referenced using a variable or an integer expression Arrays can have their data types like int, char, float or double Array Handling in C-1 An array is treated differently from a variable in C Two arrays, even if they are of the same type and size cannot be tested for equality It is not possible to assign one array directly to another Values cannot be assigned to an array on the whole, instead values are assigned to the elements of the array Array Handling in C-2 /* Input values are accepted from the user into the array .