Getting Started with Array Data Structure
By: Vikash Kumar
Tags: Cpp, Arrays, Data Structures, Algorithms

Arrays in real life

What are arrays?

Arrays are a collection of individual values separated by a comma each with its own index/location. This collection could be anything: numbers, objects, more arrays, etc. In an array, the values, called elements, are stored in contiguous memory locations.

This means that all the elements are all in a sequence and they share a common border. The number of elements store in an array is referred to as the array’s length.

An array is one of the most efficient ways to store and access a sequence of values or store a collection of data with similar elements. Arrays help to store data that would be otherwise cumbersome to put away.

Imagine you have a clothing store and you need to take stock of the number of items sold on ten different days. Let’s also assume that you need to access and store the mean and median of these values. Without arrays, you would have to create many different variables that would take up a ton of space and consume time.

This could make your code really messy as the number of days increases. Instead, we can use arrays to store and manipulate that data!

Basic structure of arrays

The index of an element in an array is used to identify its location. For instance, if our array contains five numbers: 7, 2, 0, 6, 9. The index of 7 is 0, and that is its location in the array.

The index of 2 (the second element) is 1, and so on. Indexes always start from zero and increase by one.

Declaring an array in C++

Arrays in C++ are more similar to C. In C++, you can create an array by creating the array variable and initializing the elements like so:

int values[] = {78, 90, 62, 88, 93, 50};

Here, the compiler will create an array of size 6, that stores values.

Properties of the Array

Each element is of same data type and carries a same size i.e. int = 4 bytes. Elements of the array are stored at contiguous memory locations where the first element is stored at the smallest memory location. Elements of the array can be randomly accessed since we can calculate the address of each element of the array with the given base address and the size of data element.

for example, in C language, the syntax of declaring an array is like following:

int arr[10]; char arr[10]; float arr[5]

Need of using Array

In computer programming, the most of the cases requires to store the large number of data of similar type. To store such amount of data, we need to define a large number of variables. It would be very difficult to remember names of all the variables while writing the programs. Instead of naming all the variables with a different name, it is better to define an array and store all the elements into it.

Following example illustrates, how array can be useful in writing code for a particular problem.

In the following example, we have marks of a student in six different subjects. The problem intends to calculate the average of all the marks of the student.

Advantages of Array

Array provides the single name for the group of variables of the same type therefore, it is easy to remember the name of all the elements of an array.

Traversing an array is a very simple process, we just need to increment the base address of the array in order to visit each element one by one.

Any element in the array can be directly accessed by using the index.

Memory Allocation of the array

As we have mentioned, all the data elements of an array are stored at contiguous locations in the main memory. The name of the array represents the base address or the address of first element in the main memory. Each element of the array is represented by a proper indexing.

The indexing of the array can be defined in three ways.

0 (zero — based indexing) : The first element of the array will be arr[0]. 1 (one — based indexing) : The first element of the array will be arr[1]. n (n — based indexing) : The first element of the array can reside at any random index number.

In the following image, we have shown the memory allocation of an array arr of size 5. The array follows 0-based indexing approach. The base address of the array is 100th byte. This will be the address of arr[0]. Here, the size of int is 4 bytes therefore each element will take 4 bytes in the memory.

Accessing Elements of an array

To access any random element of an array we need the following information:

Base Address of the array. Size of an element in bytes. Which type of indexing, array follows.

Address of any element of a 1D array can be calculated by using the following formula:

Byte address of element A[i] = base address + size * ( i — first index)

Example :

In an array, A[-10 ….. +2 ], Base address (BA) = 999, size of an element = 2 bytes, find the location of A[-1]. L(A[-1]) = 999 + [(-1) — (-10)] x 2 = 999 + 18 = 1017

2D Array

2D array can be defined as an array of arrays. The 2D array is organized as matrices which can be represented as the collection of rows and columns.

However, 2D arrays are created to implement a relational database look alike data structure. It provides ease of holding bulk of data at once which can be passed to any number of functions wherever required.

How to declare 2D Array

The syntax of declaring two dimensional array is very much similar to that of a one dimensional array, given as follows.

int arr[max_rows][max_columns];

however, It produces the data structure which looks like following.

Above image shows the two dimensional array, the elements are organized in the form of rows and columns. First element of the first row is represented by a[0][0] where the number shown in the first index is the number of that row while the number shown in the second index is the number of the column.

Initializing 2D Arrays

We know that, when we declare and initialize one dimensional array in C programming simultaneously, we don’t need to specify the size of the array. However this will not work with 2D arrays. We will have to define at least the second dimension of the array.

The syntax to declare and initialize the 2D array is given as follows.

int arr[2][2] = {0,1,2,3};

The number of elements that can be present in a 2D array will always be equal to (number of rows * number of columns).