Two Dimensional Array in C++

Video Tutorial
FREE
2D Array thumbnail
This video belongs to
C++ Course: Learn the Essentials
14 modules
Certificate
Topics Covered

The two-dimensional array in C++ is an array of arrays. We can visualize the two-dimensional array in C++ as a matrix. The data inside the two-dimensional array in C++ can be visualized as a table (row-column manner).

The location of each place in the two-dimensional arrays can be accessed using two variables, i and j, where i represents the row index and j represents the column index.

Complete C++ Tutorial with Certification for FREE!

Syntax of Two-Dimensional Array in C++

A two-dimensional array can be defined as an array of arrays. A mathematical matrix can be visualized as a two-dimensional array. The syntax of the two-dimensional arrays in C++ is straightforward.

Initializing a 2D array in C++

We can visualize the two-dimensional array in C++ as a matrix. The data inside the two-dimensional array in C++ can be visualized as a table (row-column manner).

Example:

The two-dimensional array can be referred to as one of the simplest forms of a multidimensional array.

Note: The multidimensional arrays are nothing but an array of array of ... arrays(up to the dimension which you want to define).

Learn More About Multidimensional Array in C++

We generally use two variables, namely i and j for referring to a particular value in the two-dimensional arrays or to access any location of the 2D array. For example, in array[i][j], i refers to the current row, and j refers to the current column.

Refer to the example below. (the provided image depicts a 3-row and 3-column matrix).

Initializing a 2D array in Cpp

Let us now learn how to initialize two-dimensional arrays in C++.

We can initialize two-dimensional arrays in C++ in two-dimensional ways, such as:

  1. at the time of declaration.
  2. after the declaration.

Let us first see how we can initialize an array at the time of declaration.

We can also use a single line by putting all the values in a sequential manner like:

We have declared and initialized a three by three matrix of integer type in the above declaration.

Note:

  • In the sequential initialization way, the elements will be filled in the array in order, the first three elements from the left in the first row, the following 3 elements in the second row, and so on.
  • In a second way, i.e., using braces inside the braces, each set of inner braces represents one row. This method of using braces inside a brace is a more convenient and better way of initialization.

Let us now see how we can initialize an array after declaring the 2D array. See, in case of initialization after the declaration, we have a few options, such as using for loops, while loops, and do while loops. Let us see all the ways one by one.

Using for Loops

As we know, whenever we know exactly how many times we want to loop through a block of code, we use the for loop.

Using while Loops

As we know, the while loop executes a code block as long as the specified condition is true.

Using do while Loops

As we know, the do/while loop will always be executed at least once, even if the condition is false, because the code block is executed before the condition is even tested.

Printing a 2D Array in C++

We have seen in the previous section all the different ways of initializing two-dimensional arrays in C++. In the same way, we can print the two-dimensional arrays in C++ using different loops.

Let us try to print the two-dimensional arrays in C++ using the for a loop.

Output:

Let us see how we can print the two-dimensional arrays in C++ using the while loop.

Output:

Taking 2D Array Elements As User Input

We can also input the values of the two-dimensional arrays in C++ from the user. But we cannot do this at the time of declaration, so we again need to use the various loops discussed in the previous sections. Let us see the different ways of taking user input using various loops:

Using for loop

Output:

Using while Loop

Output:

Using do while Loop

Output:

Matrix Addition using Two Dimensional Array in C++

As we have understood two-dimensional arrays in C++, let us now learn how to add two-dimensional arrays in C++.

We can use loops to add two two-dimensional arrays in C++. The addition of two-dimensional arrays in C++ is also known as matrix addition in C++.

We will create a program that will add the values of each row and column of the matrices and save them in another matrix (two-dimensional array). In the end, we will display the resultant matrix (the two-dimensional array that stores the sum of the two matrices).

Let us see the implementation of how we can add matrix or 2D arrays in C++.

Output:

Similarly, we can use the other loops for adding the two matrices in C++.

Accessing each location of Two Dimensional Array in C++

We have already looked at how to use two indices to access any location of the 2D array. To output all the elements of the two-dimensional arrays in C++, we can use nested loops such as for, while, or do while. We will require two loops. The first loop will traverse the rows, and the second one will traverse the columns.

Let us see the implementation to understand the context better.

Output:

How to Enter Data in Two Dimensional Array in C++

We can use nested loops to insert data inside the two-dimensional arrays in C++.

Let us see the implementation to see how we can insert data in two-dimensional arrays in C++.

Output:

Pointer to a 2D Array in C++

A pointer is a variable that stores the memory address of other objects or variables. Pointers are used extensively in both C and C++ for three main purposes: to allocate new objects on the heap, and to pass functions to other functions.

So, if we can have a pointer to another variable, we can also have a pointer to the two-dimensional arrays in C++.

We will first declare a 2D array and a pointer (*p[number_of_columns]). Herepis a pointer storing an array's address.

Using pointers, we can use nested loops to traverse the two dimensional array in C++. The inner for loop prints out the individual elements of the array matrix[i] using the pointer p. Here, (*p + j) gives us the address of the individual element matrix[i][j], so using *(*p+j) we can access the corresponding value.

Let us see the implementation of a pointer to a 2D Array in C++ to understand the context better.

Output:

Passing 2-D Array to a Function

As we know, we can pass a variable as an argument(s) in a function. Similarly, we can pass two-dimensional arrays in C++.

C++ does not allow us to pass an entire array as an argument to a function. However, we can pass a pointer to an array by specifying the array's name without an index.

For passing a 2D array to a function, we have three ways. Let us discuss them one by one.

  1. By specifying the size of the row and column: We can pass a 2D array to a function by specifying the size of columns of a 2D array. One of the important things to remember here is that the size of rows is optional but the size of the column should not be left empty else the compiler will show an error. A 2D array is stored in the memory in a single line. So, to say the compiler where should it break the row indicating the following numbers to be in the next rows we are supposed to provide the column size. And breaking the rows appropriately will automatically give the size of the rows.

Syntax:

  1. By passing an array containing pointers: We can also pass a pointer to an array.

Syntax:

  1. By passing a pointer to a pointer: A pointer to a pointer is a form of a chain of pointers. Typically, a pointer contains the address of a variable. When we define a pointer to a pointer, the first pointer contains the address of the second pointer, which further points to the location that contains the element's value.

Syntax:

How to Initialize Two-Dimensional Integer Array?

We can initialize two-dimensional arrays of integers in C++ in two-dimensional ways, such as:

  1. at the time of declaration.
  2. after the declaration.

We have already seen how to initialize the two-dimensional array in C++ at the time of declaration and initialization after declaration using loops.

Let us code a program to see both ways.

Output:

Example of a Two-Dimensional Integer Array

In the previous section, we have seen how we can initialize a two-dimensional integer array in C++. Let us take one more example of a two-dimensional integer array to understand the context better.

Example:

Output:

How to initialize a Two-Dimensional Character Array?

We have already seen how to initialize the two-dimensional integer array in C++ at the time of declaration and initialization after declaration using loops.

Let us code a program to see both ways.

Output:

Example of Two-Dimensional Character Array

In the previous section, we have seen how we can initialize a two dimensional character array in C++. Let us take one more example of two dimensional character array to understand the context better.

Example:

Output:

Conclusion

  • The two-dimensional array in C++ is an array of arrays. We can visualize the two-dimensional array in C++ as a matrix.
  • The data inside the two-dimensional array in C++ can be visualized as a table (row-column manner).
  • We generally use two variables, namely i and j, for referring to a particular value in the two-dimensional arrays in C++ or to access each location of the 2D array.
  • We can have a pointer to the two-dimensional arrays in C++.
  • We can pass a 2D array to a function by specifying the size of columns of a 2D array. We can also pass a pointer to an array.
  • We can also define a pointer as a pointer to pass in a function. When we define a pointer to a pointer, the first pointer contains the address of the second pointer, which further points to the element's location.

Read More: