Arrays
Arrays
PRG1002 - Programming I
Introduction to
Arrays
Collection of items
● Arrays in C++
Contents
● Declaring arrays
● Initializing arrays
● Multidimensional arrays
Arrays in C++
● An array is a collection of items of the same type stored in contiguous memory locations.
● Each item in the array can be individually referenced by using an index to a unique identifier.
● For example, five values of type int can be declared as an array without having to declare five different
variables.
0 1 2 3 4 Indices
numbers
int
● From the above example, each panel represents an element of the array, they are of type int. These
elements are numbered(indexed) from 0 to 4, with 0 being the first while 4 being the last; In C++, the
index of the first array element is always zero.
Declare and initialize
Declaring an Array
● So, the numbers array with five elements of type int can be declared as:
int numbers[5];
0 1 2 3 4 Indices
numbers
int
Initializing arrays
● By default, the arrays are left uninitialized. This means none of its elements are set to any particular
value.
● If we want to initialize an array of numbers with the value ‘0’, we can use empty braces:
● This creates an array of five int values, each initialized with a value of zero:
0 1 2 3 4 Indices
numbers 0 0 0 0 0
int
Initializing arrays
● Elements in an array can also be explicitly initialized to specific values, by enclosing these values in
braces {}.
● It is important that the number of values between braces {} is not greater than the size of the array.
● For example,
0 1 2 3 4 Indices
numbers 40 55 63 17 22
int
Initializing arrays
● If declared with fewer values, the remaining elements in the array are set to their default values.
● For example:
0 1 2 3 4 Indices
numbers 40 55 63 0 0
int
Initializing arrays
● We could also leave the square brackets [] empty, this will tell the compiler to pick a size based on the
number of values initialized to the array within{}.
● For example:
● After the above declaration, the array numbers would be 4 integers long, since we provided only four
initial values
0 1 2 3 Indices
numbers 40 55 63 17
int
Accessing elements
Accessing array elements
● Elements in an array can be accessed by using the array index. The syntax is:
name[index]
● Array index starts at 0 and goes till size of array minus 1. For example, the size of array is 5, the index
will be 0, 1 ,2 ,3 ,4.
Index of the element to
be accessed
numbers [1];
Name of the array
variable
0 1 2 3 4 Indices
numbers 40 55 63 17 22
int
Accessing array elements
● The statement below stores the value 100 in the third element of numbers array.
numbers[2] = 100;
0 1 2 3 4 Indices
numbers 40 55 100 17 22
int
Accessing array elements
● The statement below copies the value of the fourth element of numbers array to a variable called x.
x = numbers[3];
0 1 2 3 4 Indices
x 17 numbers 40 55 100 17 22
int
Accessing array elements
● The following copies the value of the second element of numbers array to the fifth element of
numbers array.
int numbers[5] = { 40, 55, 100, 17, 22 };
numbers[3] = numbers[1];
0 1 2 3 4 Indices
numbers 40 55 100 55 22
int
Accessing array elements
● We can also use a loop, like a for loop, to iterate through all the items in the array.
● For example,
● Get input from the user to find an item from the array.
● Search through the array, and count the numbers of times the item is found.
● It can be described as “array of arrays”, or “nested arrays”. The syntax of multidimensional array is:
type name[size1][size2]...[sizeN];
● For example, the following declarations creates a three dimensional integer array.
int numbers[2][3][3] = { { {1, 2, 3}, {11, 12, 23}, {21, 22, 23} },
{ {4, 5, 6}, {14, 15, 16}, {24, 25, 26} } };
● The total number of elements that can be stored in a multidimensional array can be calculated by
multiplying the size of all dimensions.
For example: The array numbers can store total (2 * 3 * 3) = 12 elements.
Two-Dimensional array
● A two-dimensional array can be imagined as a table, which will have x number of rows and y number of
columns. We could write a two-dimensional array as follows -
type name[x][y];
● For example, the following declarations create two-dimensional array of 3 per 5 elements of type int.
0 1 2 3 4
0
● The way to reference the second element vertically and fourth element horizontally in an expression
would be:
int x = twoDNum[1][3];
Activity - Table of integer values
● Create an empty two-dimensional array (represents a table) that has 3 rows and 2 columns.
○ Input from the user for all 6 integer values.
● Display all 6 integer values on the screen, with their row and column.
○ Example: “row 1 col 1: 4”
● Arrays are used to store multiple values in a single variable, or a collection of values.
● Arrays are declared with a type, name, and size (specified in square brackets []).
● The index starts from 0 and goes up to the size of the array minus one.