Introduction To Computing Lecture 4
Introduction To Computing Lecture 4
Array Problems
by subhabrata das Assam Engineeing College
Why Array?
Many applications require the processing of multiple data items that have common characteristics (e.g., a set of numerical data, represented by x1, x2, . . . ,xn). In such situations it is often convenient to place the data items into an array, where they will all share the same name (e.g., x). The individual data items can be characters, integers, floating-point numbers, etc. However, they must all be of the same type and the same storage class.
Array
Each array element (i.e., each individual data item) is referred to by specifying the array name followed by one or more subscripts, with each subscript enclosed in square brackets. Each subscript must be expressed as a nonnegative integer. In an n-element array, the array elements are x [ 0], x [ 1 ] , x [ 2], . . . ,x [ n - 1 ] , as illustrated in Figure.
The value of each subscript can be expressed as an integer constant, an integer variable or a more complex integer expression.
Defining Array
In general terms, a one-dimensional array definition may be expressed as
Storage class data-type array[ expression] ;
Where Storage-class is the storage class of the array, data-type is the data type, array is the array name, and expression is a positivevalued integer expression which indicates the number of array elements. The storage-class is optional; default values are automatic for arrays that are defined within a function or a block, and external for arrays that are defined outside of a function.
Defining Array
Automatic arrays, unlike automatic variables, cannot be initialized. However, external and static may definitions can include the assignment of initial values if desired. The initial values must appear in the order in which they will be assigned to the individual array elements, enclosed in braces and separated by commas. The general form is
storage-class data-type array[expression] = { value1, value2, . . . , valuen) ;
where value1 refers to the value of the first array element, value2 refers to the value of the second element, and so on. The appearance of the expression, which indicates the number of array elements, is optional when initial values are present.
Defining Array
Defining Array
Defining Array
Exercise
Exercise
Exercise