Topic 10: One – Dimension Array

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.

Example
  • ...
    Figure 10.1

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:
dataType arrayName [numberOfElements] = {initialValues};
  • dataType is the type of data that the array variables (elements) will store
  • arrayName is name of array (same rules for naming an array as for naming a variable)
  • numberOfElements is an integer specifying the size of the array (enclosed in square brackets)

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:
arrayName[subscript] = expression;
  • expression can include any combination of constants, variables, and operators
  • Data type of expression must match data type of array; otherwise, implicit type conversion will occur

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.

Example 1: Declare a array to store a list of 100 double-precision voltages
  • const int SIZE = 100;
    double voltages[SIZE];
Example 2: Declare a array to store a list of 10 integer voltages: 89, 75, 82, 93, 78, 95, 81, 88, 77, and 82
  • int voltages[] = {89, 75, 82, 93, 78, 95, 81, 88, 77, 82};

    Note that the compiler will calculate the size of the array based on the number of elements in the initializing list.
Example 3: Write a program with following values in an array named num:10, 0, 20, 0, 30. Then display the values.
  • ...
    Figure 10.2
Output
  • 10 0 20 0 30

Passing a One-Dimensional Array to a Function

You can pass an array to a function by including the array’s name as the actual argument. Unless specified otherwise, scalar variables in C++ are passed by value. Arrays, however, 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.

Address-of operator (&) is not needed in function header or function prototype, since arrays are passed by reference by default

Example 3: Write a program with following values in an array named num:10, 0, 20, 0, 30. Then display the values.
  • ...
    Figure 10.2
Output
  • 10 0 20 0 30

Parallel Array

A parallel array is a structure of an array. It contains multiple arrays of the same size, in which the i-th element of each array is related to each other. All the elements in a parallel array represent a common object or entity.

Example
  • ...
    Figure 10.4
Output
  • ...