Friday, December 27, 2024
Google search engine
HomeData Modelling & AIDifference Between one-dimensional and two-dimensional array

Difference Between one-dimensional and two-dimensional array

Array is a data structure that is used to store variables that are of similar data types at contiguous locations. The main advantage of the array is random access and cache friendliness. There are mainly three types of the array:

One Dimensional Array: 

  • It is a list of the variable of similar data types.
  • It allows random access and all the elements can be accessed with the help of their index.
  • The size of the array is fixed.
  • For a dynamically sized array, vector can be used in C++.
  • Representation of 1D array:

Two Dimensional Array:

  • It is a list of lists of the variable of the same data type.
  • It also allows random access and all the elements can be accessed with the help of their index.
  • It can also be seen as a collection of 1D arrays. It is also known as the Matrix.
  • Its dimension can be increased from 2 to 3 and 4 so on.
  • They all are referred to as a multi-dimension array.
  • The most common multidimensional array is a 2D array.
  • Representation of 2 D array:

Difference Table:

Basis One Dimension Array Two Dimension Array
Definition Store a single list of the element of a similar data type. Store a ‘list of lists’ of the element of a similar data type.
Representation Represent multiple data items as a list. Represent multiple data items as a table consisting of rows and columns.
Declaration

The declaration varies for different programming language:

  1. For C++,  
    datatype variable_name[row]
  2. For Java,  
    datatype [] variable_name= new datatype[row]

The declaration varies for different programming language:

  1. For C++, 
    datatype variable_name[row][column]
  2. For Java,  
    datatype [][] variable_name= new datatype[row][column]
Dimension One Two
Size(bytes) size of(datatype of the variable of the array) * size of the array size of(datatype of the variable of the array)* the number of rows* the number of columns.
Address calculation. Address of a[index] is equal to (base Address+ Size of each element of array * index).

Address of a[i][j] can be calculated in two ways row-major and column-major

  1. Column Major: Base Address + Size of each element (number of rows(j-lower bound of the column)+(i-lower bound of the rows))
  2. Row Major: Base Address + Size of each element (number of columns(i-lower bound of the row)+(j-lower bound of the column))
Example

int arr[5];  //an array with one row and five columns will be created.

{a , b , c , d , e}

int arr[2][5];  //an array with two rows and five columns will be created.

               a  b  c  d  e

               f  g   h  i   j

Applications of Arrays:

  • 2D Arrays are used to implement matrices.
  • Arrays can be used to implement various data structures like a heap, stack, queue, etc.
  • They allow random access.
  • They are cache-friendly.
Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!

RELATED ARTICLES

Most Popular

Recent Comments