0% found this document useful (0 votes)
16 views

Chapter 4-Arrays

Uploaded by

ShaggY
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views

Chapter 4-Arrays

Uploaded by

ShaggY
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 5

Programming I

2023

Chapter Four Arrays in C++


Introduction

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

Element 0 Element 1 Element 2 Element 3 Element 4 Element 5


Page | 1
Programming I
2023

Chapter Four Arrays in C++


An array’s size declarator must be a constant integer expression with a value greater than zero. It
can be either a literal, as in the previous example, or a named constant, as shown here:

const int SIZE = 6;

int hours[SIZE];

Memory Requirements of Arrays


The size of an array can be calculated by multiplying the number of bytes needed to store an
individual element by the number of elements in the array.
Example Array Size Declarators
Array declaration Number of elements size of each element size of the array
char letter[26]; 26 1 byte 26 bytes
short ring[100]; 100 2 bytes 200 bytes
int mile[84]; 84 4 bytes 336 bytes
float temp[12]; 12 4 bytes 48 bytes
double distance[1000]; 1000 8 bytes 8,000 bytes

4.2. Accessing Array Elements

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

Chapter Four Arrays in C++


indexed by N-1. The six elements in the hours array would have the subscripts 0 through 5. This
is shown in Figure below:
Subscripts (index values)
0 1 2 3 4 5

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] .

4.3. Array initialization

Arrays may be initialized when they are defined as follows:


DataType array-name[size]={list of values separated by comma};
For instance the statement: int age[5]={19,21,16,1,50}; defines an array of integers of size 5. In
this case, the first element of the array age is initialized with19, second with 21 and so on. A
semicolon always follows the closing brace. The array size may be omitted when the array is
initialized during array definition as follows:
int age[]={19,21,16,1,50};
in such case the compiler assumes the array size to be equal to the number of elements enclosed
within the curly braces.

4.4. Processing Array Contents

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

Chapter Four Arrays in C++


int score[5] = {7, 8, 9, 10, 11};
++score[2]; // Pre-increment operation on the value in score[2]
score[4]++; // Post-increment operation on the value in score[4]
Note: When using increment and decrement operators, be careful not to confuse the subscript
with the array element. The following example illustrates the difference.
amount[count]--; // This decrements the value stored in amount[count].
amount[count--]; // This decrements the variable count, but does nothing to the value stored in
amount[count].
4.5. Loop through an Array
 You can loop through the array elements with the for loop.
 The following example outputs all elements in the cars array:
Example:

string cars[4] = {"Volvo", "BMW", "Ford", "Mazda"};


for(int i = 0; i < 4; i++) {
cout << cars[i] << "\n";
}

4.6. Advantages and Disadvantages of an Array in C++


4.6.1. Advantages
 Random access of elements using array index.
 Use of less line of code as it creates a single array of multiple elements.
 Easy access to all the elements.
 Traversal through the array becomes easy using a single loop.
 Sorting becomes easy as it can be accomplished by writing less line of code.

4.6.2. Disadvantages

Page | 4
Programming I
2023

Chapter Four Arrays in C++


 Allows a fixed number of elements to be entered which is decided at the time
of declaration. Unlike a linked list, an array in C++ is not dynamic.
 Insertion and deletion of elements can be costly since the elements are needed
to be managed in accordance with the new memory allocation.

Page | 5

You might also like