Data Structure Array: The array is a non-primitive and linear data structure that is a group of similar data items. That is, it can store only one type of data. The array is used to store a group of data objects. The array is a static data structure that means we can allocate memory only in compile-time and cannot convert it to run-time.

Array Memory representation

The following figure represents an int type array in which is eight elements. The index of the array starts at 0 and ends at 7 due to 8 elements.

Array Data Structure

Need of the Array

An array is especially helpful when working with lots of variables of the same data-type. 

For example, let’s take a record of 100 employees, and now that record needs to be stored in the system. To solve this problem, either you have to create the 100 variables of int data-type or create an array of int type.

Program without array:

Program by using array:

See both programs; the first program is complex than the second program because the line of code of the first program is more than the second program. Therefore, Array is the best option when working with lots of variables of the same data-type.

Types of Array

There are three types of Array, as shown in the following:

  1. One dimensional Array
  2. Two-dimensional Array
  3. Multi-dimensional Array

One dimensional Array

One-dimensional arrays are those arrays that contain only one subscript. It is used to store data in the linear form.

One-dimensional Array declaration: 

For example:

Initializing One-Dimensional Array:

The array elements can be initialized after the array is declared. The initializer of the array is defined within braces and separated by commas.                        

Program of one-dimensional Array

Output

Two-dimensional Array

Two-dimensional arrays are those arrays that contain two subscripts. A two-dimensional array is also known as the matrix. The first subscript of the Array is denoted the number of rows in the array, and the second subscript is denoted the number of columns in the array. The total number of elements in a two-dimensional array is ROW*COLOUMN

One-dimensional Array declaration: 

For example:

Initializing a Two-Dimensional Array:

Output

Multi-dimensional Array (3-d)

Multi-dimensional arrays are those arrays that contain more than two subscripts. The multi-dimensional array is also known as the 3-d array.

Advantages of Array

The advantages of the array are the following:

  1. It can be easily implemented.
  2. Different data items of the same type can be displayed by just one name.
  3. It can store multiple data items at the same time.

Disadvantages of Array

The disadvantages of the array are the following:

  1. The array is a static data structure due to which its size is already defined. 
  2. We have to traverse the entire array to delete and insert an element in the array.

Pin It on Pinterest

Share This