My Blog
Articles related to programming, computer science, technology and research.

21/08/2016 Categories: C++ Programming.

Arrays

Introduction

An array is a group of elements forming a complete unit. Characteristics of an array are as follows:

• An array is a collection of elements.
• All elements in an array are of the same type.
• Collection of elements forms a complete set.

Elements in an array are stored in order and sequentially inside the memory.

Need for arrays

Think that we have to store five values in a program. We can declare five variables to store those five values as follows:

int var1;

int var2;

int var3;

int var4;

int var5;

We can declare an array which can store five integers to solve the above problem as follows:

int numbers[5] = {0};

In the above statement, numbers is the array name and 5 is the number of elements we can store in the array. All the 5 elements in the array will have an initial value of 0 which is specified by {0}.

Now, let’s think that we have to store 5000 values. It is inappropriate to declare 5000 variables to store those values. Instead we can declare a single array which can store 5000 values as follows:

int numbers[5000] = {0};

The arrays which you have seen above are called as static arrays because the number of elements the array contains and its memory is fixed at the compilation time.

Declaring and initializing static arrays

In C++ the syntax for declaring a static array is as follows:

type  array-name[no. of elements];

Consider the following example for array declaration:

int  nums[10];

In the above statement nums is the name of the array which is capable to store 10 integers and the data type of all the elements is int.

We can declare and initialize all the elements in the array as shown in the below example:

int  nums[5] = {1, 4, 2, 3, 6};

We can initialize all the elements in an array to a default value as shown in the below example:

int  nums[5] = {10};

We can initialize only some of the elements in an array as shown below:

int  nums[5] = {2, 6};

In the above statement, the first two elements are initialized to 2 and 6. Remaining elements contain garbage value or some compilers might initialize them to 0.

We can also leave out the size of array in its declaration. But we have to specify the values as shown below:

int  nums[ ] = {2, 4, 6, 8, 10};

In the above statement compiler automatically deduces the size of array nums as 5 based on the number of values.

We can also initialize array elements individually as shown in below example:

int nums[3];

nums[0] = 10;

nums[1] = 20;

nums[2] = 30;

Accessing and modifying array elements

Elements of an array can be accessed using the index or subscript. The index value of arrays starts from 0. The index of Nth element is N-1. For example index of 5th element is 4. Syntax for accessing an array element is as follows:

array-name[index]

Consider the following program which demonstrates reading and displaying array elements:

Note: Remember that while accessing elements outside the array bounds does not give any errors. Such access might lead to undesirable behavior and often might lead to program crash. So, it is a good practice to check whether the array access is within the bounds or not.

Multidimensional Arrays

Until now the arrays we discussed are one-dimensional arrays. C++ also supports storing data in multiple dimensions. A two-dimensional array can represent tabular data i.e., in rows and columns. Syntax for declaring a two-dimensional array is as follows:

type  array-name[rows][coulumns];

We can initialize a two-dimensional array at the time of declaration itself as shown below:

int  data[3][3] = {{1,2,3},{4,5,6},{7,8,9}};

In the above statement, data is a two-dimensional array having three rows and three columns. We can imagine a two-dimensional array as an array of arrays. Consider the following program which demonstrates reading and displaying elements in a two-dimensional array:

Observe how nested for loops are used to read and print the array elements effectively.

Dynamic Arrays

Consider you are writing a program to store student records which are bound to increase over time. There is now way to assume the size of the array before hand. In such cases static arrays are a bad choice. Instead, use dynamic arrays.

Dynamic arrays can be created in C++ programs using vector class which is available in the header file vector. Since vector uses template syntax and they are not at discussed, let’s look at a program which demonstrates creating and using a vector:

In the above program, size() function returns the size of the vector (array) and push_back() function inserts the given element at the end of the vector (array).

Strings

It is common in many programs to work with strings. A string is a collection of characters. Those who are familiar with C language know that strings are maintained using character arrays. Also people face many problems with the string termination character ‘\0’ in C strings.

C++ eliminates such difficulties altogether by providing pre-defined string class. It is available in the header file string.

Creating strings

We can create strings in two ways using the string class. First way is create a variable with type string and assign a string literal to it as shown below:

string  str = “hello”;

Second way is to use the string constructor which accepts a single string parameter to initialize the string variable as shown below:

string  str(“hello”);

Consider the following program which demonstrates reading strings as well as concatenating, copying, and finding the size of the string.