# pset3 - swap in function Init

I've been working on Game of Fifteen in Pset3 and I'm having a hard time understanding why I can't seem to find a way to swap the last 2 elements in the Init() function if the dimensions of the board are an even number.

I managed to create a successful swap function for Bubble sort in helpers.c but somehow in this context I can't get the swap to work... I tried creating a swap() function outside of Init with no success and I tried swapping inside the for loops, outside the for loops and inside the while loops, and outside the loops all together. During my various attempts I saw that trying to swap within the for loops was probably not the best idea as it seemed to unnecessarily complicate my code (without any positive results...).

Here is my last attempt for the Init() and Draw() functions, it seems to initialize and print properly apart from the swapping issue...

``````void init(void)
{
// Initialize the size of the array to d2-1 and the variables used for     row and column.
int arr_len = (d*d)-1;
int i, j;

while (arr_len >= 0) {

for (i = 0; i < d; i++) {
for (j = 0; j < d; j++) {

// If size of array is even number, no swaping is necessary.

board[i][j] = arr_len;
arr_len--;
}
}
}

//If arr_len is an odd number, swap the last 2 numbers.

if (d % 2 == 0) {

int swap = board[d-1][d-3];
board[d-1][d-3] = board[d-1][d-2];
board[d-1][d-2] = swap;
}
}

/**
* Prints the board in its current state.
*/

void draw(void)
{

int i, j;
int arr_len = (d*d) - 1;

printf("\n\n");

for (i = 0; i < d; i++) {
for (j = 0; j < d; j++) {

//Using different spacings in printf call so that "|" and numbers align properly.

if (arr_len > 9) {
printf("| %i |", arr_len);
arr_len--;
}
else if (arr_len > 0 && arr_len < 10) {
printf("| %i  |", arr_len);
arr_len--;
}
else
printf("| *  |");
}

printf("\n");
}
}
``````

I also tried other methods that dont use swapping like so:

``````void init(void)
{

// Initialize a variable representing the number of tiles in the board.

int arr_len = (d*d) - 1;
int i;
int j;

while (arr_len >= 0) {

for (i = 0; i < d; i++) {
for (j = 0; j < d; j++) {

if (d % 2 == 1 || arr_len !=2) {

board[i][j] = arr_len;
arr_len--;
}

else if (d % 2 == 0 && arr_len == 2) {
board[i][j] = arr_len - 1;
arr_len--;
}

else {
board[i][j] = arr_len + 1;
arr_len--;
}
}
}
}
}
``````

But that is also not giving me the expected results... Any insights would be very much appreciated...

The swapping looks ok (at least in the first init function). The thing that is "not giving the expected results" is the draw function. The draw function should print the "current state" of the board array. This draw function is drawing the same board every time, based on the calculated value `arr_len`. And it doesn't swap (not should it!).