0

Can anyone tell me why it doesn't compile correctly? Why does the number 2 turn into 0 and the swap didn't go out as fine? enter image description here

enter image description here enter image description here

1 Answer 1

0

To be clear, this program does compile. Otherwise check50 would not proceed to the third test.

The main problem is that the program is "swapping" for every value of k.

Let's join the init loop for a 4x4 board, already in progress.

  • k=2: The last row is [ 3 ] [ 2 ] [ ? ] [ ? ]. evens? yes => swap. Now the last row is [ 3 ] [ ? ] [ 2 ] [ ? ]

  • k=1: The last row is [ 3 ] [ ? ] [ 1 ] [ ? ]. evens? yes => swap. Now the last row is [ 3 ] [ 1 ] [ ? ] [ ? ]

  • k=0: The last row is [ 3 ] [ 1 ] [ ? ] [ 0 ]. evens? yes => swap. Now the last row is [ 3 ] [ ? ] [ 1 ] [ 0 ]

The example uses '?' for "unitialized" cells in the array. When this program runs, those cells have the value of 0. The concept of "unitialized" memory will become clearer as you progress through the course.

3
  • I kind of get what you're saying but then I had declared that if d%2==0 then swap the 2 (board[d-1][d-3])and 1 (board[d-1][d-2]). I don't think it has anything to do with k (the value from 15 to 0 ( for 4*4 board)). Can you further explain me on this? Commented Dec 31, 2016 at 6:24
  • Now, I solved the problem. The if statement (if d%2==0) should not be inside the for loop.
    – user27301
    Commented Dec 31, 2016 at 9:50
  • Well done. Now you can write an answer and accept it (after 2 days). That way your findings will help others who might have a similar problem. Commented Dec 31, 2016 at 12:45

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .