Introduction to Arrays | Types of Arrays and their Representation

08 min read

An Array is a Linear data structure which is a collection of data items having similar data types stored in contiguous memory locations. By knowing the address of the first item we can easily access all items/elements of an array. Arrays and its representation is given below.

arrays-introduction

  • Array Index: The location of an element in an array has an index, which identifies the element. Array index starts from 0.
  • Array element: Items stored in an array is called an element. The elements can be accessed via its index.
  • Array Length: The length of an array is defined based on the number of elements an array can store. In the above example, array length is 6 which means that it can store 6 elements.

When an array of size and type is declared, the compiler allocates enough memory to hold all elements of data.

For example, an array face [10] will have 10 elements with index starting from 0 to 9 and the memory allocated contiguously will be 20 bytes. The compiler knows the address of the first byte of the array only. Also, the address of the first byte is considered as the memory address for the whole array

Normal variables (a1, a2, a3, ….) can be used when we have a small number of elements, but if we want to store a large number of elements, it becomes difficult to manage them with normal variables. Representing many elements with one variable name is the basic idea of arrays.

 

Why does Array Indexing start with 0?

Let's try to understand this by taking one example. Assume, we can declare an array of size 10 in the following way.

int a[10];

Here a itself is a pointer which contains the memory location of the first element of the array. Now for accessing the first element, we will write a[0] which is internally decoded by the compiler as *(a + 0).

In the same way, the second element can be accessed by a[1] or *(a + 1). As a contains the address of the first element so for accessing the second element we have to add 1 to it. That's why here we have written *(a +1). In an array, the index describes the offset from the first element, i.e. the distance from the first element.

Now let's assume that array indexing starts at 1 instead of 0. In this case for accessing the first element, we have to write a[1] which is internally decoded as *(a + 1 – 1).

Observe here that we have to perform one extra operation i.e. subtraction by 1. This extra operation will greatly decrease the performance when the program is big. That's why to avoid this extra operation and improve the performance, array indexing starts at 0 and not at 1.

 

Array Operation

Now that we know the basic idea behind an array, let us now look at the various operations that can be performed on arrays.

  • Traverse − Print all the elements in the array one by one.
  • Insertion − Adds an element at the given index.
  • Deletion − Deletes an element at the given index.
  • Search − Searches an element in the array using the given index or the value.
  • Update − Updates an element at the given index.

 

Types of Arrays

The various types of arrays are as follows.

  • One dimensional array
  • Multi-dimensional array

One-Dimensional Array

A one-dimensional array is also called a single dimensional array where the elements will be accessed in sequential order. This type of array will be accessed by the subscript of either a column or row index.

Multi-Dimensional Array

When the number of dimensions specified is more than one, then it is called as a multi-dimensional array. Multidimensional arrays include 2D arrays and 3D arrays.

Types of arrays - two dimensional arrays

A two-dimensional array will be accessed by using the subscript of row and column index. For traversing the two-dimensional array, the value of the rows and columns will be considered. In the two-dimensional array face [3] [4], the first index specifies the number of rows and the second index specifies the number of columns and the array can hold 12 elements (3 * 4).

Similarly, in a three-dimensional array, there will be three dimensions. The array face [5] [10] [15] can hold 750 elements (5 * 10 * 15).

 

Declaration/Initialisation of Arrays

//  A sample program for Array Declaration
#include <stdio.h>
int main()
{
int one_dim [10];    # declaration of 1D array
int two_dim [2][2];  #declaration of 2D array
int three_dim [2][3][4] = { { {3, 4, 2, 3}, {0, -3, 9, 11}, {23, 12, 23, 2} }, { {13, 4, 56, 3}, {5, 9, 3, 5}, {3, 1, 4, 9} } }; #declaration of 3D array. Here the elements are also defined.
return 0;
}
 
 
 
 
POST A NEW COMMENT
     
  • Input (stdin)

    Output (stdout)


    Input (stdin)

    Your Output (stdout)

    Expected Output

    Compiler Message

    Input (stdin)

    2    3

    Your Output (stdout)

    5

    Expected Output

    5

    Compiler Message

    5

    Error