Get free ebooK with 50 must do coding Question for Product Based Companies solved
Fill the details & get ebook over email
Thank You!
We have sent the Ebook on 50 Must Do Coding Questions for Product Based Companies Solved over your email. All the best!

Base Address of a Two-Dimensional Array

Last Updated on May 18, 2023 by Prepbytes

In C++, a two dimensional array is a collection of elements arranged in a grid pattern, where each element is indexed by two indices, one for the row and one for the column. We will look at the base address of a two dimensional array and how to compute the base address of any element in a two-dimensional array in C++.

What is a Two-Dimensional Array?

In C++, a two dimensional array is a collection of elements arranged in a grid pattern, where each element is indexed by two indices, one for the row and one for the column. The base address of a two dimensional array is the memory address of the first element of the array, which is the element at the top-left corner of the array.

It is a sort of multidimensional array. Declaring a two-dimensional array uses the same syntax as displaying a one-dimensional array, int arr[m][n], where m is the number of rows and n is the number of columns.

The base address of a two dimensional array can be obtained by using the name of the array without any indices. For example, if we have a two dimensional array named ‘arr’ with m rows and n columns, we can obtain the base address of the array using the following syntax:

int arr[m][n];
int *base_address = &arr[0][0];

The above image shows the two-dimensional array, the elements are organized in rows and columns. The first element of the first row is represented by arr[0][0] where the number shown in the first index is the number of that row and the number shown in the second index is the number of the column.

How to Calculate the Base Address of any Element in a Two-Dimensional Array?

To understand how to calculate the base address for elements of a two-dimensional array, we must first understand how a two-dimensional array is stored in memory.

Even though a two-dimensional array appears to be substantially different from a one-dimensional array in C++, the way they are kept in memory is not. A 2-D array’s two-dimensional structure is just provided for the user’s convenience to give it a matrix-like structure. These two-dimensional arrays are actually kept in memory similarly to one-dimensional arrays.

So, to store a two-dimensional array in memory, we need to map the elements into a one-dimensional array, and then they are stored in a contiguous manner in the memory for easy access.

How are the Elements of a 2D Array are Stored in the Memory?

There are two main techniques for storing two-dimensional array elements in memory:

  1. Row major order
  2. Column major order

Row Major Order
In row-major ordering, all the rows of the two-dimensional array are stored in the memory contiguously. The memory allocation for the array seen in the preceding image is shown in row-major order as follows:

The first row of the array is completely saved in memory, then the second row, and so on until the last row of the array is completely saved in memory.

Column Major Order
According to the column-major ordering, all the columns of the two-dimensional array are stored in the memory contiguously. The memory allocation for the array seen in the preceding image is shown in column-major order as follows:

The first column of the array is completely saved in memory, then the second column, and so on until the last column of the array is completely saved in memory.

Calculating the Base Address of a Random Element of a Two-Dimensional Array

Because there are two methods for storing the two-dimensional array in memory, there are two formulas for calculating the base address of a random element of the 2D array.

By Row Major Order
If the two-dimensional array is defined as arr[m][n], where m is the number of rows and n is the number of columns, the address of an element arr[i][j] of the array stored in row-major order is estimated as

Address(arr[i][j]) = Base Address of two dimensional array + (i*n + j)* size

where the base address of the two dimensional array is the address of the array’s first member, arr[0][0].

By Column Major Order
If the array is defined as arr[m][n], where m is the number of rows and n is the number of columns, the base address of an element arr[i][j] of the array stored in column-major order is calculated as

Address(arr[i][j]) = ((j*m)+i)*Size + Base Address of two dimensional array

where the base address of the two dimensional array is the address of the array’s first member arr[0][0].

Conclusion
A two-dimensional array in C++ is a grid-like collection of elements indexed by row and column. The base address of a two-dimensional array is the memory address of its first element. To perform address calculation in 2D array of an element, we need to understand how the array is stored in memory. We have learned how are the elements of a 2D array are stored in the memory, and that can be performed in two ways : row-major order (base address + (in + j)size) and column-major order (((jm)+i)Size + base address). Understanding the base address and addressing formulas is crucial for efficient memory access in two-dimensional arrays.

Frequently Asked Questions (FAQs)

Q1. What is a two-dimensional array in C++?
Ans. A two-dimensional array in C++ is a collection of elements arranged in a grid pattern, indexed by both row and column.

Q2. How do I calculate the base address of a two-dimensional array in C++?
Ans. To calculate the base address of a two-dimensional array, you can use the formula: Base Address + (i n + j) size for row-major order, or ((j m) + i) size + Base Address for column-major order.

Q3. Why is understanding the base address important for efficient memory access in two-dimensional arrays?
Ans. Understanding the base address allows you to calculate the memory location of any element in a two-dimensional array, enabling efficient access and manipulation of array elements.

Q4. How are two-dimensional arrays stored in memory in C++?
Ans. Two-dimensional arrays in C++ are stored in memory as contiguous blocks of elements, either in row-major order (rows stored consecutively) or column-major order (columns stored consecutively).

Leave a Reply

Your email address will not be published. Required fields are marked *