Introduction to 2-D Arrays in C++

Introduction to 2-D Arrays

On this page we will discuss about Introduction to 2-D arrays in C++ . Multidimensional Arrays can be defined in simple words as an array of arrays. Data in multidimensional arrays are stored in tabular form (in row-major order).

The 2D array is organized as matrices which can be represented as the collection of rows and columns.

Introduction to 2-D array

Introduction to 2-D Arrays in C++

The elements of 2D arrays can be randomly accessed. Similar to one-dimensional arrays, we can access the individual cells in a 2D array by using the indices of the cells. There are two indices attached to a particular cell, one is its row number while the other is its column number.

Syntax : 

The syntax of declaring a two-dimensional array is very much similar to that of a one-dimensional array, given as follows.

datatype variable_name[rows][column];

Two Dimensional Arrays In C++

Size of 2-D array

The total number of elements that can be stored in a multidimensional array can be calculated by multiplying the size of all the dimensions.

Example: The array int a[5][6] can store total (5*6) = 30 elements.

Initializing 2D Arrays

  • DIRECT METHOD

    data_type[ ][ ] variable_name = { {R1C1, R1C2, ….}, {R2C1,R2C2, ….} };

    Example: int [ ][ ] arr = { { 2 ,  4 } ,  {6, 8 } };

  • USING LOOPS

    We can use loops for initializing 2d array like
    for (int i = 0; i < 2; i++)
      {
        for (int j = 0; j < 2; j++)
          {
    	int a = x[i][j];
          }
      }

C++ Code to show how to initialize and print 2-D array

Run
#include<iostrem.h>
using namespace std;
 
int main()
{
    // an array with 3 rows and 2 columns.
    int x[2][3] = {{1,2,3}, {4,5,6}};
 
    // output each array element's value
    for (int i = 0; i < 2; i++)
    {
        for (int j = 0; j < 3; j++)
        {
            cout << "Element at x[" << i << "][" << j << "]: ";
            cout << a[i][j]<<endl;
        }
    }
 
    return 0;
}

Output :

Element at x[0][0]: 1
Element at x[0][1]: 2
Element at x[0][2]: 3
Element at x[1][0]: 4
Element at x[1][1]: 5
Element at x[1][2]: 6

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription

Introduction to Trees

Binary Trees

Binary Search Trees

Traversals

  • Traversal in Trees
  • Tree Traversals: Breadth-First Search (BFS) : C | C++ | Java
  • Tree Traversals: Depth First Search (DFS) : C | C++ | Java
  • Construct a Binary Tree from Postorder and Inorder

B – Trees

AVL Trees

  • AVL Trees
    • AVL Trees: Introduction
    • AVL Tree Insertion : C | C++ | Java
    • AVL Tree Deletion : C | C++ | Java
    • Insertion in a Binary Tree (Level Order) – C | C++ | Java
    • Searching in Binary Tree – C | C++ | Java
    • Searching in a Binary Search Tree – C | C++ | Java

Complete Programs for Trees

  • Depth First Traversals – C | C++ | Java
  • Level Order Traversal – C | C++Java
  • Construct Tree from given Inorder and Preorder traversals – C | C++Java
  • Construct Tree from given Postorder and Inorder traversals – C | C++Java
  • Construct Tree from given Postorder and Preorder traversal – C | C++Java
  • Find size of the Binary tree – C | C++Java
  • Find the height of binary tree – C | C++Java
  • Find maximum in binary tree – C | C++Java
  • Check whether two tree are identical- CC++Java
  • Spiral Order traversal of Tree- CC++Java
  • Level Order Traversal Line by Line – C | C++Java
  • Hand shaking lemma and some Impotant Tree Properties.
  • Check If binary tree if Foldable or not.- CC++Java
  • check whether tree is Symmetric – C| C++Java.
  • Check for Children-Sum in Binary Tree- C|C++Java
  • Sum of all nodes in Binary Tree- CC++ | Java
  • Lowest Common Ancestor in Binary Tree- CC++ | Java

Introduction to Trees

Binary Trees

Binary Search Trees

Traversals

  • Traversal in Trees
  • Tree Traversals: Breadth-First Search (BFS) : C | C++ | Java
  • Tree Traversals: Depth First Search (DFS) : C | C++ | Java
  • Construct a Binary Tree from Postorder and Inorder

B – Trees

AVL Trees

  • AVL Trees
    • AVL Trees: Introduction
    • AVL Tree Insertion :  C | C++ | Java
    • AVL Tree Deletion : C | C++ | Java
    • Insertion in a Binary Tree (Level Order) – C | C++ | Java
    • Searching in Binary Tree – C | C++ | Java
    • Searching in a Binary Search Tree – C | C++ | Java

Complete Programs for Trees

  • Depth First Traversals – C | C++ | Java
  • Level Order Traversal – C | C++Java
  • Construct Tree from given Inorder and Preorder traversals – C | C++Java
  • Construct Tree from given Postorder and Inorder traversals – C | C++Java
  • Construct Tree from given Postorder and Preorder traversal – C | C++Java
  • Find size of the Binary tree – C | C++Java
  • Find the height of binary tree – C | C++Java
  • Find maximum in binary tree – C | C++Java
  • Check whether two tree are identical- CC++Java
  • Spiral Order traversal of Tree- CC++Java
  • Level Order Traversal LIne by Line – C | C++Java
  • Hand shaking lemma and some Impotant Tree Properties.
  • Check If binary tree if Foldable or not.- CC++Java
  • check whether tree is Symmetric  C| C++Java.
  • Check for Children-Sum in Binary Tree- C|C++Java
  • Sum of all nodes in Binary Tree- CC++ | Java
  • Lowest Common Ancestor in Binary Tree. CC++ | Java