×

Introduction to 2D-Arrays

Two Dimensional Array

Technical Definitions

An array of arrays is a common definition for a two-dimensional array. A matrix is another name for a two-dimensional array. A matrix looks like a table with rows and columns.

What is the syntax for declaring a two-dimensional array?

Defining a 2D array is fairly similar to declaring a 1D array in terms of syntax.

Syntax.

int arr[max_rows][max_columns];

As demonstrated below, it generates the following data structure:

Two Dimensional Array

How do you get data from a 2D array?

In the same way that data may be accessed using simply an index in a one-dimensional array, the indices of the cells can be used to access the cells individually in a two-dimensional array. A single cell has two indices: one is its row number, and the other is its column number.

The value recorded in any cell of an array is stored using the following syntax —

int x = a[i][j];

The row and column indices are I and j, respectively.

Creating a 2D array —

When declaring a one-dimensional array, we don't need to mention its size, but this isn't the case with a two-dimensional array. We must define at least the row size, or the second dimension, for a 2D array.

2D array declaration syntax —

int arr[2][2] = {1,2,3,4}

In a 2D array, the number of items is always equal to (number of rows * number of columns).

Consider the following scenario:

#include<stdio.h>
void main()
{
    int array[4][4],i,j;
    for (i=0; i<4; i++)
    {
        for (j=0; j<4; j++)
        {
            printf(“Enter value for array[%d][%d]: “ i,j);
            scanf(“%d”,&array[i][j]);
        }
    }
}

One-Dimensional (1D) Array vs. Two-Dimensional (2D) Array

Concept.One-Dimensional (1D) ArrayTwo-Dimensional (2D) Array
MeaningA one-dimensional array stores a single list of related data components.A list of lists or an array of arrays is kept in a two-dimensional array.  
SizeTotal Bytes = sizeof(datatype of array variable)* size of array is the size of a one-dimensional (1D) array.Total Bytes= sizeof(datatype of array variable)* size of first index* size of second index is the size of a two-dimensional (2D) array.
DimensionA one-dimensional array (sometimes known as a 1D array) has only one dimension.The dimension of a two-dimensional (2D) array is two.
Row column matrixIn a one-dimensional (1D) array, there is no row column matrix.In a two-dimensional (2D) array, there is a row and column matrix.

Addressing of 2-D Array

Row Major Order:

In an array, row major ordering is as follows:-

Row major ordering sends subsequent components to successive memory locations, travelling across the rows and then down the columns.

If the items of an array are stored in a Row-Wise form, in simple terms. The following diagram illustrates this mapping:

For a two-dimensional row-major ordered array, the formula to determine the Address (offset) is:

Two Dimensional Array
Two Dimensional Array

Address of A[I][J] = Base Address + W * ( C * I + j)

Where Base Address is the address of an array's first element.

  • W stands for data type's weight (size).
  • C stands for the total number of columns.
  • I stands for the row number
  • J stands for the column number of the element whose address has to be determined.

Column Major Ordering:

Column-major ordering in an array is when the elements of an array are stored in a Column Wise form. As you progressed through successive memory regions in row-major ordering, the rightmost index rose the fastest. The leftmost index advances the fastest in column-major ordering.

A column-major ordered array is seen in the diagram below.

Two Dimensional Array
Two Dimensional Array

Address of A[I][J] = Base Address + W * ( R * J + I)

Where Base Address is the address of an array's first element.

  • W stands for data type's weight (size).
  • R stands for the total number of rows.
  • I stands for the row number
  • J stands for the column number of the element whose address has to be determined.

Related Topics

Find the nth node from the end of a Linked List

Find the nth node from the end of a Linked List In this problem, we have given a singly linked list and a number 'n,' and we need to find the...

3 minutes read.

Bitonical Sort

Arranging an unordered collecttion of things into asignificant order. •Comparision Based Model: Bubble Sort, Selection Sort -->Non-Comparison Based. Model: Bucket Sort or on the other hand a Count Sort Bitonic Sort: Bitonic sort Algorithm was made...

5 minutes read.

Diameter of a Binary Tree

Implementation We will now witness the implementation of the diameter of a binary tree. // Creating a recursive and challenging C program that will help us determine the diameter of a binary...

4 minutes read.

Given a Binary Tree, Check if it's balanced

Implementation /*Creating a C++ program that will help us identify whether the given tree is height-balanced or not.  */ #include <bits/stdc++.h> using namespace std; /* A particular binary tree node consists of data with some...

4 minutes read.

Queue Data Structure

Queue in DS: The queue is a non-primitive and linear data structure. It works on the principle of FIFO (First In First Out). That is, the element that is added...

4 minutes read.

Shell Sort

Shell Sort: Shell sort is a sorting algorithm. It is an extended version of the insertion sort. In this sorting, we compare the elements that are distant apart rather than the...

5 minutes read.

Data Structures Algorithms

What is an Algorithm? An algorithm is a sequence of steps used to complete a job or get a desired result. It is similar to programming building elements that let cell...

4 minutes read.

Function to Create a Copy of Binary Search Tree

Implementation // creating a new hashmap in the language C++ that will help us clone a binary tree with arbitrary pointers.  #include<iostream> #include<unordered_map> using namespace std; /* A given binary tree has a record, a...

9 minutes read.

Bubble Sort vs Heap Sort

In this article, we are going to compare the two most common sorting techniques, Bubble Sort and Heap sort. Before discussing their differences, let us first discuss the idea of...

7 minutes read.

Tree in Data Structure

Tree A tree is a non-linear data structure by which hierarchical data is displayed. As we know that there are many trees in the forest, similarly the data structure also contains...

3 minutes read.

Check if a Singly Linked List is Palindrome

Check if a Singly Linked List is Palindrome In this section, we have given a singly linked list, and we need to check whether the given list is a palindrome. Example:           1...

3 minutes read.

Rearrange a linked list into alternate fashion first and the last element

Rearrange a linked list into alternate fashion first and the last element This article will explain how to rearrange the linked list into alternate fashion first and the last element. Here,...

3 minutes read.

Priority Queue in Data Structure

Priority Queue A priority queue is a special kind of queue, in priority queue we give some priority to an element and according to this priority an element can be served...

3 minutes read.

What is a Threaded Binary Tree?

When we consider those binary trees that are interlinked with each other, we do come across the fact that the fields present in there do consist of NULL values that...

3 minutes read.

All About Minimum Cost Spanning Trees in Data Structure

Data management is called database management. This allows the computer to sort or organize the data for efficient retrieval. A data model is a system that stores, manages, and optimizes...

7 minutes read.

Asynchronous advantage actor-critic (A3C) Algorithm

The Asynchronous advantage actor-critic (A3C) Algorithm is one of the latest algorithms developed by the Artificial Intelligence division, Deep Mind at Google. It is used for the Deep Reinforcement Learning...

3 minutes read.

Pairwise swap elements of a given linked list

Pairwise swap elements of a given linked list In this problem, we have given a linked list, and we need to pairwise swap elements of the given linked list. Example:                                     Input:1 ->3...

4 minutes read.

Binary Search Tree

Binary Search Tree: A binary search tree is a type of tree in which every node is organized in the sorted order. It is also called an ordered binary tree. Properties...

4 minutes read.

DFS (Depth-first search) Algorithm: Data Structure

What is DFS (Depth-first search)? The depth first search is a graph traversal algorithm. The idea behind this algorithm is backtracking and it is a kind of recursive algorithm. In the...

3 minutes read.

Time Complexity of Selection Sort in Data Structure

What is Time Complexity? The term “Time complexity” can be defined as the number of times executions made of a particular sequence of instructions and not the total amount of time...

3 minutes read.