I am working on PSET3 Fifteen and am really unsure of what I am doing with the init function. I have a nested for loop – but I don't know what to do with the values. I then move on down to draw and I believe all I am doing in draw is not actually using anything from init. I have made a nested for loop in draw and this is my output when d = 3:

0 0 0

1 1 1

2 2 2

I am not sure if I need to declare global variables or what I need to do to, but it seems like whatever I am doing in init is getting lost and is not being drawn, and all the work is being done in draw. Any guidance is GREATLY appreciated!


In your init function you need to assign values to each position in the board array. In the draw function, you use the values from your board array to print to the screen. Note that the integer array board[MAX][MAX] has already been declared as a global variable in the distributed code.


init function

Nested for loops is a good way to start, because the board you want to be drawing later on has two dimensions: board[i][j] is the variables I have given mine. argv[1] is the number (don't forget atoi!) that gives the board its dimensions, both in height and width. So what you want to do first is to iterate the first for loop along the rows, while the nested for loop will iterate along the columns (You can also do it vice versa!) until the lower right hand corner of the board is reached.

Be careful though: The numbers shall be drawn in descending order, so you have to think about a way to include this in the initialisation function as well.

Also don't forget that tiles 0 and 1 need to be swapped when the number of tiles is uneven, i.e. when the board dimensions are even!

draw function

This is much easier than the init function, because after initialising the board, you have saved the board already in your board[i][j] two dimensional array! You simply have to draw, or print it. Again, nested for loops is a good way to do this: The outer for loop can again iterate over the rows, while the nested for loop iterates along the columns (I always do it this way round). In order to print use

printf("%2d ", board[i][j]); 

This should be part of your nested for loop. Note that I have included a space to make to board look prettier; you can also draw a new line after each iteration of a row, so that there are spaces left, right, top, and down.

I hope this helps, if not let us know.



You must log in to answer this question.

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