Home > Mobile >  How does this for loop work? I understand it fills out the array, but I can't wrap my head arou
How does this for loop work? I understand it fills out the array, but I can't wrap my head arou

Time:07-17

We are going through arrays at the moment, and this was an example of a for loop filling out a 2d array. What I don't understand is HOW the for loop fills out the array in the way that it does. The question asked for arr[4] 2, arr [0] [0], arr2 2, and arr [3] [0]. I didn't get any right. I have an okay understanding of arrays as containers of elements, but I can't wrap my head around how this array is "filled out" with this for loop. I revisited the for chapters but it made me no wiser and I am stuck in the mud. C is my first programming language aswell.

 double arr[5][3];

  int k = 0;
  for ( int i = 0; i < 5; i   )
  {
  for ( int j = 0; j < 3; j   )
    {
    arr[i][j] = k;
    k  ;
    }
  }

CodePudding user response:

Two-dimensional arrays are called matrices.

double arr[5][3] = matrix a(5rows*3columns) = 
                           { a[0][0] a[0][1] a[0][2]
                             a[1][0] a[1][1] a[1][2]
                             a[2][0] a[2][1] a[2][2] 
                             a[3][0] a[3][1] a[3][2]
                             a[4][0] a[4][1] a[4][2]
                            }

where [0][0],[0][1],[1][0] etc.......[i][j] as the positions of the matrix

**1st outer loop(i)**
**1st inner loop(j)**


 1. i=0
       j=0 #this is a inner loop
         arr[0][0] = k = 0 #the a[0][0] of the matrix is filled
         k   --> k is incremented by 1 #this is for the next loop
       j   --> j is incremented by 1

2d array looks like

{ 0       a[0][1] a[0][2]
  a[1][0] a[1][1] a[1][2]
  a[2][0] a[2][1] a[2][2] }

**1st outer loop(i)**
**2nd inner loop(j)**

 2. i=0
       j=1 #this is a inner loop
         arr[0][1] = k = 1 #the a[0][1] of the matrix is filled
         k   --> k is incremented by 1 #this is for the next loop
       j   --> j is incremented by 1

2d array looks like

{ 0       1       a[0][2]
  a[1][0] a[1][1] a[1][2]
  a[2][0] a[2][1] a[2][2] }

**1st outer loop(i)**
**3rd inner loop(j)**
     i=0
       j=2 #this is a inner loop
         arr[0][2] = k = 2 #the a[0][2] of the matrix is filled
         k   --> k is incremented by 1 #this is for the next loop
       j   --> j is incremented by 1

2d array looks like

{ 0       1       2
  a[1][0] a[1][1] a[1][2]
  a[2][0] a[2][1] a[2][2] }

as soon as j is incremented by 1 it's value becomes 3 now and hence violates the j < 3 conditions in the for loop. So it goes back to the outer loop.

**2nd outer loop(i)**
**1st inner loop(j)**
        i=1 # i   --> i gets incremented by 1 
         j=0 #this is a inner loop,The counters of j are initialized to 0.
         arr[1][0] = k = 3 #the a[1][0] of the matrix is filled
         k   --> k is incremented by 1 #this is for the next loop
       j   --> j is incremented by 1

2d array looks like

{ 0       1       2
  3       a[1][1] a[1][2]
  a[2][0] a[2][1] a[2][2] }

**2nd outer loop(i)**
**2nd inner loop(j)**
     i=1 #  
       j=1 #this is a inner loop
         arr[1][1] = k = 4 #the a[1][1] of the matrix is filled
         k   --> k is incremented by 1 #this is for the next loop
       j   --> j is incremented by 1

{ 0       1       2
  3       4       a[1][2]
  a[2][0] a[2][1] a[2][2] }

**2nd outer loop(i)**
**3rd inner loop(j)**

    i=1 # 
       j=2 #this is a inner loop
         arr[1][2] = k = 5 #the a[1][2] of the matrix is filled
         k   --> k is incremented by 1 #this is for the next loop
       j   --> j is incremented by 1

{ 0       1       2
  3       4       5
  a[2][0] a[2][1] a[2][2] }

as soon as j is incremented by 1 it's value becomes 3 now and hence violates the j < 3 conditions in the for loop. So it goes back to the outer loop.

**3rd outer loop(i)**
**1st inner loop(j)**

--------

the cycle repeats itself until it hits the i < 5 condition which then finally exits from the loop. So the final loop would be

**5th outer loop(i)**
**3rd inner loop(j)**

{ 0       1       2
  3       4       5
  6       7       8
  9.      10.     11
  12.     13.     14 }

Hope it made the concepts a bit easy to understand.

CodePudding user response:

I can understand the confusion. The for-loop created with "i" go through rows and "j" goes through columns. So when we execute the code, the first for-loop runs with i=0. We enter inside the loop and now we find the for-loop for columns /"j" for-loop.

Our j value initially is 0. so with i=0, j=0 we enter the columns loop.

now we have arr[i][j] , that is arr[0][0]

In the next line we saved k=0, in arr[0][0].

Now next line that is k increments the value to k=1

we exit the columns loop and go back to the statement

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

J will increment j=1 and we check the condition it's true so we again enter the loop.

Please remember our i is still 0 i.e. i=0 since we haven't exited the "i" loop our value will not go through the increment process.

so now we have i=0, j=1.

Next value will be saved k=1 in arr[0][1].

This will go on for j=0,1,2 when j=3 will reach our condition will become false. In that case, we won't enter the "j" loop again. Since we don't have any other statement to perform after the "j" loop.

We will exit from the "i" loop as well and go to the increment part. Now, i=1, we check condition i<5, and it's true. So now we enter the "i" loop again.

Now once we enter, we again see the "j" loop or columns loop

again we will initialize the j=0 and go thru the loop.

Now our arr[i][j]= arr[1][0].

Now again we will go through j=0,1,2. so value filled will be in arr[1][0], arr[1][1], arr[1][2]

the same thing will happen we exit and increment i=2 check condition enter again now we again have three values of j so value filled will be in arr[2][0], arr[2][1], arr[2][2]

I hope this helped you. Will encourage you to execute the for loop on paper and draw the 2d array and fill it while executing it on paper.

  • Related