Technology Inside Out!

Index ¦ Archives ¦ Atom ¦ RSS

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

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

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:

int mat[3][3];

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

Now, it would be declared as follows:

float mat[4][6];

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:

for(int i = 0; i <= row; i++ )
{
  for(int j = 0; j <= col; j++)
  {
     scanf("%d",&matrix[i][j]);
  }
}

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.

int mat[3][3]; //Declaring 3 x 3 , 2D matrix

int i,j; //Declaring variables to be used in loop

for( i = 0; i < 3; i++ )  // loop for updating row number
{
  for( j = 0; j < 3; j++)  // loop for updating col number
  {
     scanf("%d",&mat[i][j]);
  }
}

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.

// We have assumed to initialized the array by taking input from user as
// shown in code snippet above.

// Traversing
for ( i = 0; i < 3; i++)
{
  for( j = 0; j < 3; j++)
  {
    //Traversing here and applying the printing on console operation
    printf("%d",mat[i][j]);
  }
}

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:

int matrix [3][3][3]; //Declaring 3D Array which is 3 x 3 x 3

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

int matrix [3][4][5][6]; // Declaring Matrix named matrix of type int and
                         // size 3 x 4 x 5 x 6

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:

float mat[3][4][5]; // Declaring array mat of type float with dimensions
                    // 3 x 4 x 5

// Filling up data in array by taking values from user

for( i = 0; i < 3; i++)
{
  for( j = 0; j < 4; j++)
  {
    for( k = 0; k < 5; k++)
    {
      //Taking input from user here corresponding to each dimension
      scanf("%f",&mat[i][j][k]);
    }
  }
}

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:

// This snippet assumes that we've declared and filled up the array as shown // above.

// Traversing and output

for( i = 0; i < 3; i++)
{
  for( j = 0; j < 4; j++)
  {
    for( k = 0; k < 5; k++)
    {
      //Taking input from user here corresponding to each dimension
      printf("%f",mat[i][j][k]);
    }
  }
}

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

© The Geeky Way. Built using Pelican. Theme by Giulio Fidente on github.

Disclaimer Privacy policy