Unable to understand pattern problem using loops

I was printing a pattern like this and found this code online.
AAAAA*
AAAA***
AAA*****
AA*******
A*********

When i calculated how the loop is working I found this that for first iteration the values of i,j and k are 1,5,1 respectively that means five "A" then *;for second loop I calculated the values 2,4,2 respectively.The second loop will run till j>=i which means 4,3,2 as i has been 2 now and j is 4 so there should be 3 "A" not four.Can you help me understand where my thought process is going wrong or how i am calculating their value wrong.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include<iostream>
using std::endl;
using std::cout;

int main()
{
    for(int i=1;i<=5;i++)
    {

    for(int j=5;j>=i;j--)
      {
     cout<<"A";
      }

    for(int k=1;k<2*i;k++)
      {
      cout<<"*";
      }
     cout<<endl;
     }
}


Also is it useful to practice problems of this kind to get hang of loops or should i make generic programmes.
Last edited on
I replaced the cout statements within the nested for loops with the current values of i and j, and i and k. I don't know if this will help visualize what's happening in the loop processing.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
i 1 j 5
i 1 j 4
i 1 j 3
i 1 j 2
i 1 j 1
i 1 k 1

i 2 j 5
i 2 j 4
i 2 j 3
i 2 j 2
i 2 k 1
i 2 k 2
i 2 k 3

i 3 j 5
i 3 j 4
i 3 j 3
i 3 k 1
i 3 k 2
i 3 k 3
i 3 k 4
i 3 k 5

i 4 j 5
i 4 j 4
i 4 k 1
i 4 k 2
i 4 k 3
i 4 k 4
i 4 k 5
i 4 k 6
i 4 k 7

i 5 j 5
i 5 k 1
i 5 k 2
i 5 k 3
i 5 k 4
i 5 k 5
i 5 k 6
i 5 k 7
i 5 k 8
i 5 k 9
@wildblue sorry for the noob question but can u explain me why for the second loop the value for j start from 5 shouldn't it start from 4 as j--. Why isn't it value updated to 4.Also why does k start from 0 always.it would be really helpful if you can explain me or point me somewhere about how the variable updation is happening.
Last edited on
For each time through the outside loop (i), the inside loops run fully from their initial value, that's 5 for j and 1 for k, through whatever number they end with in that particular run (which will depend on the current value of i).

1
2
3
4
5
6
7
8
9
i = 1 // start outside loop
  first nested loop   j=5 and decrements, 4, 3, 2, 1, 0; at 0 loop ends, condition j>=i no longer true 
  second nested loop  k=1 and increments, 2;             at 2 loop ends, condition k<2*i (2) no longer true
  new line
i = 2  // start outside loop again. Internal loops start back at their beginning state.
  first nested loop   j=5 and decrements, 4, 3, 2, 1;    at 1 loop ends, condition j>=i no longer true
  second nested loop  k=1 and increments, 2, 3, 4;       at 4 loop ends, condition k<2*i (4) no longer true
  new line
etc.
Last edited on
why for the second loop the value for j start from 5 shouldn't it start from 4 as j--.

I think you don't fully understand how the for statement works. This:
1
2
3
for(int j=5;j>=i;j--) {
    doStuff;
}

is equivalent to:
1
2
3
4
5
6
7
{
    int j=5;
    while (j >= i) {
        doStuff;
        j--;
    }
}

Notice that the increment part (j--) happens at the end of the loop, not at the beginning.
Thanks for the help finally its clear to me where i was going wrong.
Topic archived. No new replies allowed.