Introduction to Data Structure And Algorithm (Part 2):-(Arrays)

Akash Salvi
GDSC DYPCOE
Published in
5 min readJun 16, 2020

What is Array?

An array is a data structure, which can store a fixed-size collection of elements of the same data type. An array is used to store a collection of data, but it is often more useful to think of an array as a collection of variables of the same type.

Why do we need arrays?

It is used to represent multiple data items of same type by using only single name. It can be used to implement other data structures like linked lists, stacks, queues, trees, graphs etc. 2D arrays are used to represent matrices. Most of the Data Structures are implemented using array.

How to declare an array:-

⫸ d_type arr_name[arr_size];

For example, take an integer array ‘arr’.

⫸ int arr[6];

arr[ ] is used to denote an array named ‘arr’

So, arr[6] means that ‘arr’ is an array of 6 integers. Here, 6 is the size of the array i.e., there are 6 elements of array ‘arr’.

⫸ int arr[ ] = { 2,3,15,8,48,13 };

OR

⫸ int arr[6] = { 2,3,15,8,48,13 };

Few Important Points related to Arrays:-

  1. Arrays have 0 as the first index not 1. In this example, arr[0] is the first element.
  2. If the size of an array is n, to access the last element, (n-1) index is used. In this example, arr[5] is the last element.
  3. Suppose the starting address of arr[0] is 2450a. Then, the next address, arr[1] , will be 2454a, address of arr[2] will be 2458a and so on. It's because the size of int is 4 bytes.(the address increases by 4 bytes). Similarly for different datatype ,address increases as per the size of datatype.

There Are 4 Important concepts in Array in C++:

  1. Passing arrays to functions : You can pass to the function a pointer to an array by specifying the array’s name without an index.
  2. Return array from functions : C++ allows a function to return an array.
  3. Pointer to an array : You can generate a pointer to the first element of an array by simply specifying the array name, without any index.
  4. Multi-dimensional arrays : C++ supports multidimensional arrays. The simplest form of the multidimensional array is the two-dimensional array.

#Here is a basic code of take 5 inputs from user and store it in array and calculate the sum of all elements present in array:

Passing an entire Array in a Function:

Their is a simple trick , to pass an array in a function we will pass the address of array, that is the address of the first element of the array.Thus, by having the pointer of the first element, we can get the entire array.

⪼ Now we will perform sum of array using function:

Return array from functions :

⪼ Basic code to Sort an Array using Function.

Pointer to Arrays:

As we all know that pointer is a variable whose value is the address of some other variable i.e., if a variable y points to another variable x means that the value of the variable ‘y’ is the address of ‘x’.

Similarly, if we say that a variable y points to an array n, then it means that the value of ‘y’ is the address of the first element of the array i.e., n[0]. So, y is the pointer to the array n.

If p is a pointer to the array marks, then it means that p(or name)points to marks[0].

⫸ int marks[50];
⫸ int *p;
⫸ p = age;

Now, since p points to the first element of the array age, *p is the value of the first element of the array.

Since *p refers to the first array element, *(p+1) and *(p+2) refers to the second and third elements respectively and so on.

So , *p is marks[0] , *(p+1) is marks[1] , *(p+2) is marks[2] , so on…

For Example:

Multi-dimensional arrays :

In C/C++, we can define multidimensional arrays in simple words as array of arrays. Data in multidimensional arrays are stored in tabular form (in row major order).

⪼ General form of declaring N-dimensional arrays:

data_type  array_name[size1][size2]....[sizeN];data_type: Type of data to be stored in the array(i.e int,float,...) Here data_type is valid C/C++ data typearray_name: Name of the array
size1, size2,... ,sizeN: Sizes of the dimensions

⪼ Example:

Two dimensional array:
int two_d[10][20];

Three dimensional array:
int three_d[10][20][30];

Total number of elements that can be stored in a multidimensional array can be calculated by multiplying the size of all the dimensions.

For example:
The array int x[5][10] can store total ( 5 * 10 ) = 50 elements.
Similarly array int x[5][10][20] can store total ( 5 * 10 * 20 ) = 1000 elements

2D Array in C++:

Two Dimensional array in C++ is an array that consists of more than one rows and more than one column. In 2-D array each element is refer by two indexes. Elements stored in these Arrays in the form of matrices. The first index shows a row of the matrix and the second index shows the column of the matrix.

Syntax of Two-Dimensional Array:-

data_type  array_name[Number of rows][Number of columns];

For example:-

int matrix [3][3]; /*compiler will generate a 2-D array of a matrix which consists of 3 rows and 3 columns.*/

In order to store values in a C++ two dimensional arrays the programmer have to specified the number of row and the number of column of a matrix. To access each individual location of a matrix to store the values the user have to provide exact number of row and number of column.

How to enter data in a Two Dimensional Arrays:-

Nested loop is used to enter data in 2-D arrays. It depends upon the programmer which loop he wants to use it could be While loop or it could be a For loop. The outer loop acts as the number of rows of a matrix and the inner loop acts as the number of columns of a matrix. Similarly, to display loops are used.

We can initialize 2D-array while declaration:

int x[3][4] = {{0,1,2,3}, {4,5,6,7}, {8,9,10,11}};
//Each set of inner braces represents one row.

You can Practice Array Concept online on:

  1. Leetcode
  2. Hackerblocks
  3. Hackerrank

Advantages of an Array in C/C++:

  1. Random access of elements using array index.
  2. Use of less line of code as it creates a single array of multiple elements.
  3. Easy access to all the elements.
  4. Traversal through the array becomes easy using a single loop.
  5. Sorting becomes easy as it can be accomplished by writing less line of code.

Disadvantages of an Array in C/C++:

  1. Allows a fixed number of elements to be entered which is decided at the time of declaration. Unlike a linked list, an array in C is not dynamic.
  2. Insertion and deletion of elements can be costly since the elements are needed to be managed in accordance with the new memory allocation.

next article coming soon with new Data Structure …

Check Previous Article :

“Introduction to Data Structure And Algorithm in C++ for Beginners:(Part 1)”

--

--