×

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

Types of Linked list

Single linked list  A single linked list is a linked list in which all nodes are connected with each other in sequence. Each node of a singly linked list has two...

7 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.

Convert Sorted List to Binary Search Tree

Implementation // creating the C++ implementation of the following approach: - #include <bits/stdc++.h> using namespace std; /* Create the link list node and see its implementation. */ class L__Nod { public: int record; L__Nod* next; }; /* constructing a new binary...

15 minutes read.

Linear vs Circular Queue: Data Structure

Difference Between Linear and Circular Queue What is Linear Queue? A linear queue is linear data structure which works on first in first out principle. We can say a linear queue is...

3 minutes read.

Convert binary tree to a doubly linked list

Implementation //creating a C++ program for the transition of a binary tree into a linked list. #include <iostream> using namespace std; /* Firstly, let’s create a binary tree that will help us in setting...

4 minutes read.

Height of a binary tree

The height of a binary tree is generally defined as the height or length of the root _nod in the entire binary tree. In simple words, the height of a...

4 minutes read.

Queue operations in Data Structure

Queue - Queue is a linear data structure or first in first out data structure means the first element added in the queue will be removed first and the last...

7 minutes read.

Identical Linked Lists

Identical Linked Lists In this problem, we have given two linked lists, and we need to check whether the given linked lists are identical or not. Identical means they have the...

4 minutes read.

Traversal of binary tree

Traversal of binary tree: A node is visited only once in the traversal of the binary tree. There are three main types of traversal methods in the binary tree. In-order traversalPre-order...

3 minutes read.

Huffman tree in Data Structures

The Huffman trees in the field of data structures are pretty impressive in their work. They are generally treated as the binary tree, which is linked with the least external...

6 minutes read.

Binary search tree traversal in-order pre-order post-order examples

A binary search tree is a type of non-linear tree in which the tree contains at least two nods. It is called binary because of its nature that states bi...

8 minutes read.

Understanding Data Processing

Introduction Data In our everyday lives, any task that we perform online is related to data. Millions of pieces of data are produced every second across the globe. Data production is largely...

4 minutes read.

Strictly binary tree in Data Structures?

What is a strictly Binary Tree in Data Structures? There are various kinds of binary trees that we know exist in data structures, and they all have their purposes. In this...

4 minutes read.

Finding Rank in a Binary Search Tree

Implementation // writing a C++ program to find out the rank and element in the program.  #include <bits/stdc++.h> using namespace std; struct __nod { int record; __nod *Lft, *Rt; int LftSize; }; __nod* new__nod(int record) { __nod *temp = new __nod; temp->record...

6 minutes read.

Circular Linked List

Circular Linked List A circular linked list where all nodes are connected to their next node and last node is connected to the starting node or we can say all nodes...

5 minutes read.

What is a 2-3 Tree in Data Structure?

Tree Data structure The information about the tree is self-explanatory. Trees are ordered and, therefore, not linear. But they are actually designed differently. Tree A node-based data model that represents and...

5 minutes read.

Serialize and Deserialize a Binary Tree

Implementation // Writing a C++ program to check the serialization and deserialization of binary tree.   #include <iosstream> /* A binary tree node contains a key and a pointer to the left and right...

4 minutes read.

Binary Tree Implementation Using Arrays

Implementation Converting a binary tree into a list of arrays is one interesting problem. Let us see that in depth. In this section, we will see the implementation of the binary Trees...

4 minutes read.

Write Main Difference Between Tree and Graph in Data Structures

Graph: The graph has two sets, which are considered V and E. These vertices are also called nodes, and edges are referred to as arcs connecting any two nodes in a...

4 minutes read.

Data Structure Infix to Postfix Conversion

Infix to Postfix Conversion The infix expression is easy to read and write by humans. In present time, we use the infix expression in our daily life but the computers are...

4 minutes read.