Logic programming on multidimensional arrays

Now we will take a look at how we can print all the values of the entire multidimensional array used in the previous section, that is, the a array.

If we analyze the declaration of the array, we will see that two for loops will be required to print the entire array, one for rows and one for columns.

We want the controller to scan the complete first row, then the second row, and finally the third. So we add an outer for loop for the rows and set the length limit to the number of rows in the array, in this case two rows. The outer for loop for the rows will look like the following:

for(int i=0;i<2;i++)

This for loop will actually loop twice since we set the limit to 2 for rows. The first loop will scan the first row and the second loop will scan the second row. Now for each loop, we need to scan the three columns present in that specific row. To do this, we add an inner for loop that will scan every column and we set the limit to the number of columns in the array, which is 3 for this example. The inner for loop for the columns will look like the following code:

for(int j=0;j<3;j++)

Finally to print the array, we add a print statement in the internal for loop to display all the values. The final code will be as follows:

for(int i=0;i<2;i++) //row
{
for(int j=0;j<3;j++) //coloumn
{
System.out.println(a[i][j]);
}
}

Let's try to understand what we have written here. The control will start from the outer for loop; this outer for loop is executed twice because it has been set to less than 2. After entering the outer for loop for the first time, it enters the inner for loop; this loop is executed three times because j has been set to less than 3.

Let's debug it and take a look at a few steps in the code to understand these loops better. The following are the steps that will be performed while debugging the code:

  1. The controller executes the outer loop for the first time and the value of i has been initialized to 0, this means that the value of the x axis is set at 0. The controller will look at the first row since 0 indicates that the first row is being accessed.
  1. It moves to the inner for loop and executes it, the initial value of j has been been initialized to 0; this means that the value of the y axis is set to 0. The controller will look at the first row and first column, since it was already on the first row because of the outer loop. The inner loop sent the controller to look at the first column.
  2. a will take the value of the first row and first column as the values of i and j were initialized to 0, a[0][0]. So the output for this execution will be the first row and first column, which is 2 in this example.
  3. The controller moves to the inner for loop again as the condition for the loop is still satisfied because j gets iterated to 1, which is less than 3; this means that the value of the y axis is set to 1 and it will access the second column. The controller will look at the first row and second column since it was already on the first row because of the outer loop and the inner loop sent the controller to look at the second column.
  4. a will take the value of the first row and second column as the values of i and j are set to 0 and 1, a[0][1]. So the output for this execution will be the first row and second column, 4 in this example.
  5. The controller moves to the inner for loop again as the condition for the loop is still satisfied because j gets iterated to 2, which is less than 3. This means that the value of the y axis is set to 2 and it will access the third column. The controller will look at the first row and third column since it was already on the first row because of the outer loop and the inner loop sent the controller to look at the third column.
  6. a will take the value of the first row and third column as the values of i and j are set to 0 and 2, a[0][2]. So the output for this execution will be the first row and third column, which is 5 in this example.
  7. When the controller goes to the inner loop now, it won't be able to execute it because after j gets iterated again the value will be 3, which is not less than the limit we had set for the loop. So the controller exits the inner for loop and goes back to the outer loop and iterates the value of i to 1; this means that the value of the x axis is set to 1The controller will look at the second row since 1 indicates that the second row is being accessed.
  8. Steps 2, 3, 4, 5, 6, and 7 are repeated again, but this time the value of i, which is the x axis, is set to 1; that means the second row will be accessed. All the values in the second row are displayed according to the steps specified previously, until we reach the third column of the matrix.
  1. The controller will exit the inner loop after accessing the third column as j will be iterated to 3, which is less than the limit that we had set for the loop. So the controller again exits the inner for loop and starts executing the outer loop.
  2. In the outer for loop, the value of i will be iterated to 2 and the loop will not be executed because it is not less than 2, which is the limit we set for it.

This is how the values of a multidimensional array can be obtained using two for loops, in which the outer loop works with rows and the inner loop works with columns.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.137.161.222