Chapter 4-Arrays
Chapter 4-Arrays
2023
The variables you have worked with so far are designed to hold only one value at a time. An
array works like a variable that can store a group of values, all of the same type. The values are
stored together in consecutive memory locations. An array is a sequence of objects all of which
have the same type. The objects are called the elements of the array and are numbered
consecutively 0, 1, 2, 3, . . . . These numbers are called index values or subscripts of the array.
The term “subscript” is used because as a mathematical sequence, an array would be written with
subscripts: a0, al, a2, . . . . These numbers locate the element’s position within the array, thereby
giving direct access into the array.
4.1. Array Definition
Like other normal variables, the array variable must be defined before its use. The syntax for
defining an array is:
Data type ArrayName[array size];
In the definition, the array name must be a valid C++ variable name, followed by an integer
value enclosed in square braces. The integer value indicates the maximum number of elements
the array can hold. The following are some valid array definition statements:
int marks[100]; //integer array of size 100
float salary[25]; //floating-point array of size 25
char name[50]; //character array of size
double[10]; // double array of size 10
N.B. Arrays of any data type can be defined.
Here is a definition of an array of integers: int hours[6]; The name of this array is hours. The
number inside the brackets is the array’s size declarator. It indicates the number of elements, or
values, the array can hold. The hours array can store six elements, each one an integer. This is
depicted in Figure below.
hours array: Enough memory to hold six int values
int hours[SIZE];
Once an array variable is defined, its elements can be accessed using an index. The syntax for
accessing array elements is:
ArrayName[index];
Even though an entire array has only one name, the elements may be accessed and used as
individual variables. This is possible because each element is assigned a number known as a
subscript. A subscript is used as an index to pinpoint a specific element within an array. To
access a particular element in the array, specify the array name followed by an integer constant
or variable (array index) enclosed within square braces. The array index indicates the element of
the array which has to be accessed. For example the expression
hours[4]
accesses the fifth element of the array hours. Note that, in an array of N elements the first
element is indexed by 0, the second element is indexed by 1, and so forth and the last element is
Page | 2
Programming I
2023
Note:
Subscript numbering in C++ always starts at zero. The subscript of the last element in an array is
one less than the total number of elements in the array. This means that in the array shown
above, the element hours[6] does not exist. The last element in the array is hours[5] .
Individual array elements are processed like any other type of variable. Processing array
elements is no different than processing other variables. For example, the following statement
multiplies hours[3] by the variable rate:
pay = hours[3] * rate;
And the following are examples of pre-increment and post-increment operations on array
elements:
Page | 3
Programming I
2023
4.6.2. Disadvantages
Page | 4
Programming I
2023
Page | 5