Arrays (2 - Dimension)

AbhinavAbhinav
4 min read

It's a collection of 1-D arrays.


Some insights about 1-D arrays

In the figure above, you can understand the terminology. Now, let's say you want to go to address 1012, which is A[3]. How would you do that?

The logic is quite simple. Start from the base address and add the index multiplied by the size of the data type. This will give you the value 1012. Try it yourself.

It's something like this: base Address + I*(4 bytes) ==> 1000 + 3×4 = 1012.

Now, how do you represent it in value form? Just put an * in front of the address. So, it will be like *(A + 3), which will give you the value at the 3rd index, and in this case, it is equal to 5.


All the concepts mentioned above also apply to 2-D arrays, but there is a slight difference: now we have rows and columns. Before we go further I have a Question!

Why does the index start from 0 in an array?

As we have seen above, the name of an array represents the base address of that array. Therefore, if we start with 1, we wouldn't be able to access the first element of the array. That's why the index starts at 0.


Representation of 2-D Arrays:

Data-type nameOfArray[sizeOfRows][sizeOfColumns] = {values separated by comma};

Example: int cups[2][2] = {1,3,5,2}; // Assigning more values than the defined size will cause an error. However, assigning less values is acceptable.

Data-type nameOfArray[sizeOfRows][sizeOfColumns] = {{},{},{}}; // The number of curly brackets indicates the number of rows. Inside each curly bracket, you can define the elements for the columns. You can fill all of them, or if you leave them empty, they will automatically be set to 0. However, if you declare a 2-D array like int A[2][3]; without assigning values, the values will be garbage.

Example: int cups[3][2] = {{1,2},{3,5},{6,7}};

The two examples above show how to represent 2-D arrays.


How to Find the Address in 2-D Arrays:

Let’s revisit address 1012: think of it like a 1-D array. First, consider the base address, or starting address, which is 1000. To reach the second row, skip first row. Then, move through the columns one by one inside the second row. Skip one column again, and now you are at address 1012.

If you focus on the logic above, it is written like this: Base Address + (row to skip * size of row) + (column to skip * size of data type) = destination address(1012). \==> which is written as int A[1][1]; Cross check yourself.

So, how does it happen? Basically, the i and j in the array tell you to skip that many rows and columns, respectively.

Now, let's say you want to access the value at that address. What should you do? It's similar to working with a 1-D array. A+1 will move you to the 2nd row. Use an asterisk (*) to access it, then add 1 again to move to the 2nd column. Use the asterisk (*) once more to get the value. ie *(*(A+1)+1) will give you value 5(according to figure diagram values).


Question Leetcode: 463 (Island Perimeter)

The logic is simple: read the question and try to develop a solution. What I'm doing is checking for water in all four directions whenever I find land.

int array[rows][cols]; // array 2-D
int perimeter = 0; // initialise the perimeter
// two for loops are required to make a 2-D array

for (int i = 0; i < rows ; i++){
    for(int j = 0; j < cols ; j++){
        if(array[rows][cols] == 1){ // find land
            //look for water in 4 direction top, bottom, lefe and right
        // corner cases are either i = 0 , j = cols , i = rows and j = 0;
    if( i == 0 || array[i][j-1] == 0 ) perimeter++; // left check
    if( j == cols-1 || array[i][j+1] == 0 ) perimeter++; // right check
    if( i == 0 || array[i-1][j] == 0 ) perimeter++; // top check
    if( i == rows-1 || array[i+1][j] == 0 ) perimeter++; // bottom check
            }
    }
}

For more details, check this image.

Here's a representation of arrays: (same thing with different representation )

It follows the commutative law of mathematics. Previously, we saw that *(A+1) will give you a value, and you can also write it as *(1+A). Both are the same. It is also equal to A[1], and therefore, it can also be written as 1[A].


Conclusion:

Never be afraid of 2-D arrays. Treat them as continuous 1-D arrays and focus on the logic. The code will naturally come together for you.

0
Subscribe to my newsletter

Read articles from Abhinav directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Abhinav
Abhinav

I am B-Tech 2nd year Student (Electrical Engineering)