CSCI 103 - Spring 2024 Introduction to Programming

Q7

first iteration: i = 0

  1. 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]
  2. At the end of the loop, *change = 5 (in this iteration, the value of r too), decrement by 1 so that *change = 4
  3. Swap change and same so change = &c ( c = 0 at this point ), same = &r ( r = 4 at this point)
  4. Since i % 2 == 0, delta does not change (=1)
  5. *change += 1 (so c also updates to = 1)

second iteration: i = 1 (starting with r = 4, c = 1)

  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]
  2. At the end of loop, *change = 5 (so is c), decrement so that *change = 4
  3. Swap change and same so change = &r (r = 4), same = &c (c = 4)
  4. i % 2 == 1, so delta = -1
  5. *change += -1 (so r = 3)

third iteration: i = 2 (starting with r = 3, c = 4)

  1. Inner loops runs (5 - 2) = 3 times, coloring img coordinates (3, 4), (2, 4) (1, 4) [row changes, col stays same]
  2. End of loop: *change = 0 ( = r), decrement by delta = -1, so *change = 1
  3. Swap change and same: change = &c (c = 4), same = &r (r = 1)
  4. i % 2 == 0 so delta stays the same (= -1)
  5. *change += -1 (so c = 3)

fourth iteration: i = 3 (starting with r =1 , c = 3)

  1. Inner loop runs (5 - 3) = 2 times, coloring img coordinates (1, 3), (1, 2) [row same, col changes]
  2. End of loop: *change = 1 ( = c), decrement by delta = -1 so *change = 2
  3. swap change same: change = &r (r = 1), same = &c (c = 2)
  4. i % 2 == 1 so delta = 1
  5. *change += 1 (so r = 2)

fifth iteration: i = 4 (starting with r = 2, c = 2)

  1. Inner loop runs (5 - 4) = 1 times, coloring img coordinate (2, 2) [row changes, col same]
  2. End of loop, *change = 3 ( = r), decrement by delta = 1 so *change = 2
  3. swap change and same
  4. i % 2 != 1 so delta stays the same
  5. *change += 1
  6. Since i = 5, which is not < MAX, loop ends.
  7. 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)