A group of related variables with the same data type is referred to as an array . Variables in an array are stored in consecutive locations in the computer’s internal memory. Each variable in an array has the same name and data type.
You distinguish one variable in a one-dimensional array from another variable in the same array by using a unique integer, called a subscript. A subscript indicates a variable’s position in the array and is assigned by the computer when the array is created. The first variable in a one-dimensional array is assigned a subscript of 0, the second a subscript of 1, and so on.
You refer to a variable in an array by the array’s name immediately followed by a subscript enclosed in square brackets (e.g., sales[0]). The last subscript in an array is always one less than the total number of variables in the array since the subscripts begin at 0.
Syntax:You may initialize array elements by entering one or more values, separated by commas, in braces. Assigning initial values is referred to as populating the array . The values used to populate an array should have the same data type as the array variables. Otherwise, implicit type conversion is performed. Most C++ compilers initialize uninitialized numeric array elements to 0.0 or 0 (depending on data type). Automatic initialization is only done if you provide at least one value in the initialValues section.
You can use an assignment statement or the extraction operator to enter data into an array element
Syntax:To display the contents of an array, you need to access each of its elements.
Use a loop along with a counter variable that keeps track of each subscript in the array.
You can pass an array to a function by including the array’s name as the actual argument. Arrays are passed by reference by default, because it is more efficient.
Passing an array by value would require copying the entire array, which could be very large. Passing an array by reference allows the computer to pass the address of only the first array element. Since elements are stored in contiguous memory locations, computer can use this address to locate remaining elements in the array.