ISI 121
COMPUTER PROGRAMMING
LECTURE 5 & PRACTICALS
OBJECTIVES
•Introduction to Arrays.
•Declaring Arrays.
•Initializing Arrays.
•Multidimensional Arrays.
•Strings and Arrays.
Introduction to Arrays
•So far we have used variables to store values in memory for later reuse.
•We now explore a means to store multiple values together as one unit, the array.
•An array is a fixed number of elements of the same type stored sequentially in memory.
•An array is a container object that holds a fixed number of values of a single type.
•Therefore, an integer array holds some number of integers, a character array holds some
number of characters, and so on.
•The size of the array is referred to as its dimension.
Introduction to Arrays
•A proper illustration of an array can be seen on the figure below:
Declaring Arrays
•To declare an array in C++, we write the following:
type arrayName[dimension];
•To declare an integer array named arr of four elements, we write int arr[4];
•The elements of an array can be accessed by using an index into the array.
•An index describes the position of an element within an array.
•Arrays in C++ are zero-indexed, so the first element has an index of 0.
•So, to access the third element in arr, we write arr[2]; The value returned can then be used
just like any other integer.
Initializing Arrays
•Like normal variables, the elements of an array must be initialized before they can be used;
otherwise we will almost certainly get unexpected results in our program.
•There are several ways to initialize the array.
•One way is to declare the array and then initialize some or all of the elements:
int arr[4];
arr[0] = 6;
arr[1] = 0;
arr[2] = 9;
arr[3] = 6;
Initializing Arrays
•Another way is to initialize some or all of the values at the time of declaration:
int arr[4] = { 6, 0, 9, 6 };
•Sometimes it is more convenient to leave out the size of the array and let the compiler
determine the array's size for us, based on how many elements we give it:
int arr[] = { 6, 0, 9, 6, 2, 0, 1, 1 };
•Here, the compiler will create an integer array of dimension 8.
Initializing Arrays
•Arrays can also be initialized with values that are not known beforehand:
Initializing Arrays
•Note that when accessing an array the index given must be a positive integer from 0 to n-1,
where n is the dimension of the array.
•The format for accessing an array member is as simple as shown below:
arrayName[index]
•The index itself may be directly provided, derived from a variable, or computed from an
expression:
arr[5];
arr[i];
arr[i+3];
Initializing Arrays
•Note that when accessing an array the index given must be a positive integer from 0 to n-1,
where n is the dimension of the array.
•The format for accessing an array member is as simple as shown below:
arrayName[index]
•The index itself may be directly provided, derived from a variable, or computed from an
expression:
arr[5];
arr[i];
arr[i+3];
•In C++ it is syntactically correct to exceed the valid range of indices for an array.
•This can create problems, since accessing out-of-range elements do not cause compilation
errors but can cause runtime errors.
Initializing Arrays
•Note that you cannot assign one array to another in C++, the following is illegal:
int a[10], b[10];
// do something
// assign all elements of array b to array a
a = b; // error -- illegal
•Instead, you have to do the assignments for each element:
int i;
// assign all elements of array b to array a
for(i=0; i<10; i++) a[i] = b[i];
How an array is represented in
Memory
•In C++, any array is mapped to a contiguous memory location.
•All memory elements reside next to each other.
•The lowest address corresponds to the first element, and the highest address to the last
element.
•Example
int a[]={7,6,5,4,3,2,1,0}
•The memory representation of this array looks like
Arrays as parameters in
functions
•Arrays can also be passed as arguments to functions.
•When declaring the function, simply specify the array as a parameter, without a dimension.
•The array can then be used as normal within the function.
•Consider the following example:
Arrays as parameters in
functions
Arrays as parameters in
functions
•The function sum takes a constant integer array and a constant integer length as its arguments
and adds up length elements in the array.
•It then returns the sum, and the program prints out Sum: 28.
•It is important to note that arrays are passed by reference and so any changes made to the array
within the function will be observed in the calling scope.
Multidimensional Arrays
•Multidimensional arrays can be described as "arrays of arrays".
•For example, a bi-dimensional array can be imagined as a bi-dimensional table made of
elements, all of them of a same uniform data type.
Multidimensional Arrays
•Thus, a two-dimensional array may be created by the following:
type arrayName[dimension1][dimension2];
•The array will have dimension1 x dimension2 elements of the same type and can be thought of
as an array of arrays.
•The first index indicates which of dimension1 subarrays to access, and then the second index
accesses one of dimension2 elements within that subarray.
•Initialization and access thus work similarly to the one-dimensional case:
Multidimensional Arrays
•Consider the example below:
This could also be achieved as shown below:
int twoDimArray[2][4] = { 6, 0, 9, 6, 2, 0, 1, 1 };
int twoDimArray[2][4] = { { 6, 0, 9, 6 } , { 2, 0, 1, 1 } };
Multidimensional Arrays
•Thus, a two-dimensional array may be created by the following:
type arrayName[dimension1][dimension2];
•The array will have dimension1 x dimension2 elements of the same type and can be thought of
as an array of arrays.
•The first index indicates which of dimension1 subarrays to access, and then the second index
accesses one of dimension2 elements within that subarray.
•Initialization and access thus work similarly to the one-dimensional case: