Learning About 2D And Multidimensional Arrays

Well, I’m glad you reached at this point, we’ve already discussed about 1D arrays and the memory organization, and in this post, we’ll know about 2D arrays and Multidimensional Arrays. They are not very much different from 1 D arrays as we looked in the last tutorial. So, let’s start:

Learning about 2D and multidimensional arrays in C

Learning about 2D and multidimensional arrays in C

In this tutorial, we’ll cover the following topics:



2 Dimensional Arrays

Without saying anything much about 2D arrays here, let’s first have a look at the memory organization in 2D arrays:

Memory Organization of 2D Arrays in memory

Memory Organization of 2D Arrays in memory

Clearly, it’s much like 1D arrays but this time the contiguous memory blocks have to be occupied in two directions considered as along x and y co-ordinates.

With this basic memory organization picture in mind, let’s now see how we declare a 2D Array.

Declaration of 2D Array:

Let’s take an example that you want to create a 3 x 3 Matrix named mat of integer data type, so we would declare as:

The first number in square brackets is for the number of rows and the next one is for number of columns. For instance, now say we need to create a matrix of say 4 rows and 6 columns, of float data type. it would look like this:

Matrix memory representation 4 X 6

Matrix memory representation 4 X 6

Now, it would be declared as follows:

Now let us come back to our original mat, which is a 3 x 3 matrix and see how to fill it.

Putting in data to 2D Arrays:

Since we need to refer a location by both the row number and the column number, we need to fill in each block of memory by traversing through each row first and then each column in that row, then come to next row, traverse each column and so on…

Here we would need two loops, one for updating the row number and the next to update the column number. Since we are moving from left to right for each row, remember that we will update the row value less frequently than the column value.

So, basic syntax would be something like this:

Here row and col would correspond to the total number of rows and columns in the matrix. Inside the loop, we’re just taking input from the user such that the value of j i.e. our column would update more frequently from left to right than the value of i i.e. our row.

Now, let’s start accepting values from user for our for our matrix named mat which is a 3 x 3 matrix.

Traversing and Printing 2D Array:

Next, we need to traverse this array, it would be done as:

Traversing means just processing through each element of the array and apply an operation.


Multidimensional Array :

Extending what we’ve learned about 1D and 2D arrays, there can be 3D, 4D, 5D and even 100D arrays, though the complexity would multiply at each level, so we don’t use beyond 2D arrays generally. In some complex application, we may use 3D arrays, but beyond that, because of the increase in the complexity, they are hardly used.

Declaration of Multidimensional Arrays:

So, for declaring a 3 D array, you just have to define one more dimension as follows:

Similarly, for a 4 D array,  declaration would be :

So, generally, whatever dimension you want, you can add while declaring.

Putting in data to Multidimensional Array:

Very similar to filling up a 2 D array,  a 3D array can be filled up by using 3 loops (one for iterating through each dimension). Remember, the inner loop will always has a fast frequency to iterate than the loop enclosing it and so on…

Let’s fill up a 3D array of type float here as an example:

Similarly, just increase the number of loops depending on the dimensions. Thus, if you have created an N dimension array, it would take N loops to fill it.

Traversing and Output of a  Multidimensional Array:

In the same way as explained above, you can print a multidimensional array by iterating through it using the same no. of loops as the dimension of the array as:

I hope this had been a bit informative for you and would help you. Next, we’ll be going to discuss about Structures in C language.

Have any doubts? Just leave a comment below or give me a shout on twitter @ErSanyamKhurana


Leave a Reply

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