Q7
first iteration: i = 0
- inner loop runs (5-0) times, coloring image coordinates (0,0) (1, 0) (2, 0) (3, 0) (4, 0) [here, row changes, col stays the same]
- At the end of the loop,
*change = 5
(in this iteration, the value of r too), decrement by 1 so that*change = 4
- Swap change and same so
change = &c
( c = 0 at this point ),same = &r
( r = 4 at this point) - Since
i % 2 == 0
, delta does not change (=1) *change += 1
(so c also updates to = 1)
second iteration: i = 1 (starting with r = 4, c = 1)
- Inner loops runs (5 - 1) = 4 times, coloring images coordinates (4, 1) (4, 2) (4, 3) (4, 4) [here, row stays the same, col changes]
- At the end of loop,
*change = 5
(so is c), decrement so that*change = 4
- Swap change and same so
change = &r
(r = 4),same = &c
(c = 4) i % 2 == 1
, so delta = -1*change += -1
(so r = 3)
third iteration: i = 2 (starting with r = 3, c = 4)
- Inner loops runs (5 - 2) = 3 times, coloring img coordinates (3, 4), (2, 4) (1, 4) [row changes, col stays same]
- End of loop: *change = 0 ( = r), decrement by delta = -1, so *change = 1
- Swap change and same: change = &c (c = 4), same = &r (r = 1)
- i % 2 == 0 so delta stays the same (= -1)
- *change += -1 (so c = 3)
fourth iteration: i = 3 (starting with r =1 , c = 3)
- Inner loop runs (5 - 3) = 2 times, coloring img coordinates (1, 3), (1, 2) [row same, col changes]
- End of loop:
*change = 1
( = c), decrement by delta = -1 so*change = 2
- swap change same:
change = &r
(r = 1),same = &c
(c = 2) i % 2 == 1
so delta = 1*change += 1
(so r = 2)
fifth iteration: i = 4 (starting with r = 2, c = 2)
- Inner loop runs (5 - 4) = 1 times, coloring img coordinate (2, 2) [row changes, col same]
- End of loop,
*change = 3
( = r), decrement by delta = 1 so*change = 2
- swap change and same
i % 2 != 1
so delta stays the same*change += 1
- Since i = 5, which is not < MAX, loop ends.
- At the end of the day, you get kind of a spiral shape, with the following coordinates colored to black: (2, 2) (1, 3), (1, 2) (3, 4), (2, 4) (1, 4) (4, 1) (4, 2) (4, 3) (4, 4)