×

Two dimension array

C++ Two dimension (2D) Array

Two dimension (2D) array is an array of arrays. It is represented in the form of row and column. The elements of 2D array are accessed through the index of row and column.

Declaration of 2D Array

data_type array_Name[size_row][size_column];
In the above declaration, data_type is C++ data type, array_Name is the name of array and size_row is the total size of row and size_column is total size of the column of array elements.
int matrix[2][3];

Initialization of 2D Array

A 2D array is initialized with data type followed by array name and total row size and column size of elements assignment into array.
int matrix[2][3] = { 6,5,2,3,7,12};
Another best way to initialize the 2D array is as follow:
int matrix[2][3] = { {6,5,2},{3,7,12}};

Access of Array

Elements of an array are accessed through the index (indices) of that element. The index of array starts from[00] and end at [row_size-1column_size-1] of array. Suppose we want to access the individual elements of a 2D array, this can be done by passing index of the element in array name.
matrix[00]=6;  
matrix[01]=5;  
matrix[02]=2;  
matrix[10]=3;  
matrix[11]=7;  
matrix[1][2]=12;

Two dimension Array example

Two dimension array are accessed by using two loops (nested loop). The external loop access row of array whereas inner loop access column of the array.
#include <iostream>  
using namespace std;  
int main()   
{  
    int matrix[2][3] = { {6,5,2},{3,7,12}};  
    for (int i = 0; i < 2; i++)   
    {  
        for(int j=0; j<3; j++ ){  
            cout<< matrix[i][j]<<endl;  
        }  
    }   
    return 0;  
}
Output:
6
5
2
3
7
12

Related Topics

C++ String Class and its Applications

The String class is available in C++. The character array is represented by the C string. The string class in C++ has a few different attributes. It contains several functions...

4 minutes read.

Processing strings using std string stream in C++

A string class object called std: string stream is used to streamline into various variables, just as files can pour into strings. This class's things make use of a string...

3 minutes read.

Counting Frequencies of Array Elements in C++

We have an array of integer items with duplicate values, and our objective is to compute the frequencies of the different elements in the array. Methods: Methods that can be used to...

3 minutes read.

Factory Method for Designing Pattern in C++

In C++, the factory method is a type of conditional design pattern. The factory method is related to creating a new object in C++. With the help of a factory...

3 minutes read.

Reverse a Number in C++

In this tutorial, you'll learn how to utilize a C++ program to reverse a number entered by a user at runtime. Because there are various ways to write a C++...

3 minutes read.

strcat() vs strncat() in C++

In this tutorial, we will explore about strcat() and strncat() in the most usable language C++. We will also look at the difference between them. strcat() C++ is a computer language with...

4 minutes read.

Dynamic _Cast in C++

C++ is one of the most powerful programming languages. We can write object-oriented and structured programming with the help of C++. In this article, we will learn about the dynamic...

3 minutes read.

How to calculate size of string in C++

What is string in C++? In C++, a string is a sequence of characters. The string data type is part of the Standard Template Library (STL) and is defined in the...

4 minutes read.

Virtual base class in C++

Consider in a C++ program, there are 4 classes named class A, class B, class C, and class D. If class B and class c inherit properties from class A....

3 minutes read.

C++ find missing in the second array

Given two arrays A and B of sizes n and m. Find the elements from array A that are not present in array B. Example Input : a[] = {1, 2, 3, 5,...

3 minutes read.

How to Use Getline in C++

What is getline in C++? Similar to the cin function, which allows the programmer to take input from the user getline() function also takes input from the user. But in this...

4 minutes read.

Initialize Vector in C++

Initialize Vector in C++  The following comparison operators are defined for vector and those are given below. ==, <, <=, !=, >,>=  This allows you to access the element of a vector using...

3 minutes read.

How to Reverse a String in C++ using While Loop

A while loop or while statement repeats all code of its body as long as a specific condition is satisfied. The loop ends if or when the condition no longer...

6 minutes read.

C++ Close file

C++ File Handling close() Function A file which is opened while reading or writing in file handling must be closed after performing an action on it. The close() function is used to close...

2 minutes read.

Array of sets in C++

Instead of defining distinct variables for each item, arrays are used to hold numerous values in a single variable. A collection of data objects stored in a continuous way is...

6 minutes read.

Functors in C++

Functors are not a very popular thing among beginner or intermediate-level programmers. But this thing is very useful and helpful. The name functor suggests us some similarities with function. It...

3 minutes read.

Hashing in C++

Before understanding hashing, we need to know what the use of hashing is. Let us consider an example of a library which consists of many books.Having many books, searching for...

6 minutes read.

Function Pointer in C++

Definition: A function pointer in C++ is the same as a usual pointer which is used to point some variables. Function pointers are used to point functions or say store the...

4 minutes read.

C++ Tricks for Competitive Programming

If you are interested in Computer science or Information technology, you must have heard about competitive programming. Competitive programming is a way to improve your problem-solving skills. There are various...

7 minutes read.

Reverse a String using Stack C/C++

Reverse the given string using stack. To turn "tutorialandexample" into "elpmaxednalairotut," for instance. Here is a straightforward stack-based technique for reversing strings. Algorithm: 1) Make a stack that is empty. 2) Push each character...

3 minutes read.