Data Structures and Algorithms

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 14

Array

What Is C++ Array?


An array can be defined as a group or collection of similar kinds of elements or data
items that are stored together in contiguous memory spaces. All the memory locations
are adjacent to each other, and the number of elements in an array is the size of the
array. For example, imagine the parking lot of a mall that allows only parking for two-
wheelers. Now this collection of similar types of wheelers in the parking lot can be
called an array.

 Element − Each item stored in an array is called an element.


 Index − Each location of an element in an array has a numerical index, which
is used to identify the element.

Initialization and Declaration of an Array


Arrays can be declared in various ways in different languages. For illustration, let's take C array
declaration.
Facts about Array in C/C++:
 Index starts with 0.
 Array length is 10 which means it can store 10 elements.
 Each element can be accessed via its index. For example, we can fetch an
element at index 6 as 9.
 Elements of an array have consecutive addresses. For example, suppose the
starting address of a [0] is 2120.
Then, the address of the next element a [1] will be 2124, the address of a [2] will
be 2128, and so on.
Here, the size of each element is increased by 4. This is because the size
of int is 4 bytes.

Types of Arrays in C++

There are 3 types of an array in C++:

 One-dimensional array
 Two-dimensional array
 Multidimensional array
One-Dimensional Array:

In this type of array, it stores elements in a single dimension. And, in this array, a single
specification is required to describe elements of the array.

Fig: One-dimensional array

The diagram above shows that it arranged all the elements row-wise in a single dimension,
one after the other.

Now, take an example of a one-dimensional array.

Fig: Example of a one-dimensional array

In this example, you are printing the elements 1,7,9,4,5 using for loop, and depicted below is
the output of this example.
Two-Dimensional Array:

In this type of array, two indexes describe each element, the first index represents a row, and
the second index represents a column.

Fig: Two-dimensional array

As you can see, the elements are arranged row-wise and column-wise; in a two-dimensional
array, there are i number of rows and j number of columns. The above figure is a
representation of a 3 x 3 matrix, which means there are three rows and three columns in the
array.

Now let’s look at an example of a two-dimensional array.

Fig: Example of a two-dimensional array


In this example, you are printing a two-dimensional array of three rows and three columns;
you need to use two for loops. The first loop, i.e., i loop, runs for the row from 0 to 3, and the
second loop, i.e., j loop, runs for the column from 0 to 3.

And below is the output of this example.

Fig: Output

Multidimensional Array:

The simplest example of a multidimensional array is a 2-d array; a two-dimensional array also
falls under the category of a multidimensional array. This array can have any number of
dimensions.
The syntax for declaring a multidimensional array is:

Syntax:

Datatype array_name [size 1] [size 2] . . .. [size n];

Here size1 size2 up to so on size n describes the number of dimensions; in the case of a 2-d
array, there are only two dimensions, a multidimensional array can have any number of
dimensions.

Example:

int array[5][10][4];
Basic Operations

Following are the basic operations supported by an array.


 Traverse − print all the array elements one by one.
 Insertion − Adds an element at the given index.
 Deletion − Deletes an element at the given index.
 Search − Searches an element using the given index or by the value.
 Sort − Sorting an array.
 Update − Updates an element at the given index.
In C, when an array is initialized with size, then it assigns defaults values to its elements in following
order.

Data Type Default Value

bool false

char 0

int 0

float 0.0

double 0.0f

void

wchar_t 0
Traverse Operation

In traversing operation of an array, each element of an array is accessed exactly for once for
processing. This is also called visiting of an array.

Example
Let LA is a Linear Array (unordered) with N elements.

When we compile and execute the above program, it produces the following result −

Output:

https://codepict.com/traverse-operation-in-array/
Insertion Operation
Insert operation is to insert one or more data elements into an array. Based on the requirement, a
new element can be added at the beginning, end, or any given index of array.
Here, we see a practical implementation of insertion operation, where we add data at the end of the
array −

Example
Following is the implementation of the above algorithm –

Output:

https://codepict.com/insert-an-element-in-an-array/
Deletion Operation
Deletion refers to removing an existing element from the array and re-organizing all elements of an
array.

Algorithm to Delete an element from an Array:


 Step 01: Start
 Step 02: [Initialize counter variable. ] Set i = pos - 1
 Step 03: Repeat Step 04 and 05 for i = pos - 1 to i < size
 Step 04: [Move ith element backward (left). ] set a[i] = a[i+1]
 Step 05: [Increase counter. ] Set i = i + 1
 Step 06: [End of step 03 loop. ]
 Step 07: [Reset size of the array. ] set size = size - 1
 Step 08: Stop

Example
Following is the implementation of the above algorithm –

Output:

https://codepict.com/delete-an-element-from-an-array/
Search Operation
You can perform a search for an array element based on its value or its index.

Algorithm to Search an element in an Array:


 Step 01: Start
 Step 02: [Initialize counter variable. ] Set i = 0
 Step 03: Repeat Step 04 and 05 for i = 0 to i < n
 Step 04: if a[i] = x, then jump to step 07
 Step 05: [Increase counter. ] Set i = i + 1
 Step 06: [End of step 03 loop. ]
 Step 07: Print x found at i + 1 position and go to step 09
 Step 08: Print x not found (if a[i] != x, after all the iteration of the above for loop. )
 Step 09: Stop

Example
Following is the implementation of the above algorithm −

Output:

https://codepict.com/search-an-element-in-an-array/
Sort operation
Sort operation in Array or sorting an array simply means, arranging the elements of an array in
some specific (definite) logical order.

Algorithm to Sort the elements of an Array:


 Step 01: Start
 Step 02: Repeat Step 03 to 07 for i = 0 to i < n
 Step 03: Repeat Step 04 and 05 for j = i + 1 to j < n
 Step 04: Check a statement using the if keyword, If arr[i] > arr[j], Swap arr[i] and arr[j].
 Step 05: [Increase counter. ] Set j = j + 1
 Step 06: [End of step 03 loop. ]
 Step 07: [Increase counter. ] Set i = i + 1
 Step 08: [End of step 02 loop. ]
 Step 09: Stop

Example
Following is the implementation of the above algorithm −
Output:

https://codepict.com/sort-the-elements-of-an-array/

Update Operation
Update operation refers to updating an existing element from the array at a given index.

Algorithm to Update an element in an Array:


 Step 01: Start
 Step 02: Set a[pos-1] = x
 Step 03: Stop

Example
Following is the implementation of the above algorithm −
Output:

https://codepict.com/update-an-element-in-an-array/

You might also like