All You Need to Know About Two-Dimensional Arrays

Two-dimensional arrays or multi-dimensional arrays are arrays where the data element's position is referred to, by two indices. The name specifies two dimensions, that is, row and column.

What Are Two-Dimensional Arrays?

Two-dimensional arrays can be defined as arrays within an array. 2D arrays erected as metrics, which is a collection of rows and columns. It is common to design 2D arrays to accomplish a database that is similar to the data structure.

2d-arrays

Want a Top Software Development Job? Start Here!

Full Stack Developer - MERN StackExplore Program
Want a Top Software Development Job? Start Here!

The Need For Two-Dimensional Arrays

Using 2d arrays, you can store so much data at one moment, which can be passed at any number of functions whenever required.

Picture this, a class consists of 5 students, and the class has to publish the result of all those students.

You need a table to store all those five students' names, subjects' names, and marks.

For that, it requires storing all information in a tabular form comprising rows and columns.

A row contains the name of subjects, and columns contain the name of the students.

That class consists of four subjects, namely English, Science, Mathematics, and Hindi, and the names of the students are first, second, third, fourth, and fifth.

need-of-two-dimensional-array

Declaration of Two-Dimensional Arrays

The syntax of two-dimensional arrays is:

Data_type name_of the array[rows][index];

Here is one example:

  int multi_dim[2][3];

syntax-of-two-dimensional-array_2

In the above example, the name of the 2d array is multi_dim  consisting of 2 rows and three columns of integer data types.

Want a Top Software Development Job? Start Here!

Full Stack Developer - MERN StackExplore Program
Want a Top Software Development Job? Start Here!

Initialization of Two-Dimensional Arrays

There are two methods to initialize two-dimensional arrays.

Method 1

int multi_dim[4][3]={10,20,30,40,50,60,20,80,90,100,110,120};

Method 2

int multi_dim[4][3]={{10,20,30},{40.50,60},{70,80,90},{100,110,120}};

Here are two methods of initialization of an element during declaration. Here, the second method is preferred because the second method is more readable and understandable so that you can clearly visualize that multi_dim 2D arrays comprise four rows and three columns.

Accessing Two-Dimensional Arrays

Accessing two-dimensional arrays can be done using row index value and column index value.

Name_of_the arrays[row_index][column_index];

int multi_dim[4][3]={{10,20,30},{40,50,60},{70,80,90},{100,110,120}};

Suppose, in this example, you want to access element 80.

Multi_dim[2][1];

accessing-2d-arrays_3

Note: indexing always starts with zero.

Printing the Elements in a Two-Dimensional Array

Printing elements of a two-dimensional array can be done using two for loops.

printing-two-dimensional-array

Next Steps

Next, you will need to learn about more types of data structure like a linked list, stack, queue, tree, and graph, and many more.

If you are perhaps looking to learn further about data structures and gain a comprehensive understanding of data engineering, Simplilearn’s Full Stack Developer - MERN Stack will be a great fit for you. This world-class bootcamp offers you the right skills and applied training you need to become a job-ready data engineering expert today. Take a look, this can prove to be the game changer for your career.

If you have any questions about this topic, please feel free to leave them in the comments below. Our team of experts will answer all your queries for you at the earliest!

About the Author

SimplilearnSimplilearn

Simplilearn is one of the world’s leading providers of online training for Digital Marketing, Cloud Computing, Project Management, Data Science, IT, Software Development, and many other emerging technologies.

View More
  • Acknowledgement
  • PMP, PMI, PMBOK, CAPM, PgMP, PfMP, ACP, PBA, RMP, SP, OPM3 and the PMI ATP seal are the registered marks of the Project Management Institute, Inc.