You are on page 1of 23

Bachelor of Information Technology

PRG1002 - Programming I

Introduction to
Arrays

Collection of items
● Arrays in C++
Contents
● Declaring arrays

● Initializing arrays

● Accessing array elements

● 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

● A typical declaration for an array in C++ is:


type name[size];

● From the above declaration,


○ type should be of a valid type (such as int, float, double, etc…),
○ name is a valid identifier, and
○ the size specifies the size of the array, it is always enclosed in square brackets ([])

● 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:

int numbers[5] = {};

● 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,

int numbers[5] = {40, 55, 63, 17, 22};

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:

int numbers[5] = {40, 55, 63};

● The elements without a value are filled with zeroes.

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:

int numbers[] = {40, 55, 63, 17};

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

int numbers[5] = { 40, 55, 63, 17, 22 };

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.

int numbers[5] = { 40, 55, 100, 17, 22 };

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,

// initializing an integer array that holds 5 values


int numbers[5] = { 40, 55, 100, 17, 22 };
// using a for-loop to print out all the numbers
for (int i = 0; i < 5; ++i)
{
cout << numbers[i] << endl;
}
Activity - List of Integer values

● Create a C++ project that goes by the name ListOfNumbers.

● Create an empty array that can hold ten integers.


○ Input from the user for all 10 integer values.

● Display all 10 integer values on the screen.

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

● Build and Run the program.


Multidimensional arrays
Multidimensional arrays

● 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} } };

● To access 12 from the array.

cout << numbers[0][1][1] << endl; // Result is 12.

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

int twoDNum[3][5] = {};


Two-dimensional array

● Table represents a two-dimensional array of 3 per 5 elements is -

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 a C++ project that goes by the name TableOfNumbers.

● 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”

● Build and Run the program.


Summary

● 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 []).

● All elements of an array are of the same type.

● We can access an element of an array by referring to an index number.

● The index starts from 0 and goes up to the size of the array minus one.

● C++ also supports the creation of multidimensional arrays.


○ Array of arrays
○ Two dimensional arrays are most commonly used
References

Geeksforgeeksorg. 2015. GeeksforGeeks. [Online]. [5 August 2019]. Available from:


https://www.geeksforgeeks.org/arrays-in-c-cpp/

W3schoolscom. 2019. W3schoolscom. [Online]. [5 August 2019]. Available from:


https://www.w3schools.com/cpp/cpp_arrays.asp

Tutorialspointcom. 2019. Tutorialspointcom. [Online]. [5 August 2019]. Available from:


https://www.tutorialspoint.com/cplusplus/cpp_arrays

Cppedu. 2019. Cppedu. [Online]. [5 August 2019]. Available from:


https://www.cpp.edu/~elab/ECE114/Array.html

You might also like