The puzzle again...Swapping elements of 2D array

This is a discussion on The puzzle again...Swapping elements of 2D array within the C Programming forums, part of the General Programming Boards category; Ok, my puzzle program is almost complete. I just can't figure out how to swap the number the user chooses ...

how do you know that a and b have been set? It's possible that they're both 0, if the nested for loops didn't find the number that the user wanted to move (from my limited understanding of your program). In that case you'd be looking at puzzle[-1][0], which is not a very good idea.

Or what if the user just picked a number on the top row -- then you'd have the same problem. Moral of the issue: always check to make sure a is at least 1 before you go using a-1 as an index.

Plus your swap function just swaps the values of a and b, which isn't what you want. You want something more like this:

Code:

swap(&puzzle[a][b],&puzzle[X][Y]);

I'm not sure what X and Y are here, but I suspect they depend on which puzzle element is zero.

"Simplicity does not precede complexity, but follows it." -- Alan Perlis
"Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
"The only real mistake is the one from which we learn nothing." -- John Powell

"Simplicity does not precede complexity, but follows it." -- Alan Perlis
"Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
"The only real mistake is the one from which we learn nothing." -- John Powell

The for loops will find the number the user wants to move. It's understood in the assignment that they enter a number that's actually in the puzzle. Just for kicks though I ran the program and entered '27' for the number to move and it just returned 0 (which in main prints out "enter a valid number to move to the open spot.")

Well, yes. If the use enters a number that isn't found, the for loops won't set a and b, which will then retain their original values of 0 and 0. In other words, it would be like the user typed the first number (the one at puzzle[0][0]).

Personally, I'd initialize a and b to something like -1, so that after the for loop you can check if the loops actually found the number or not -- and if not, return accordingly. Or you could set a "number_found" flag. Or you could just not worry about it.

"Simplicity does not precede complexity, but follows it." -- Alan Perlis
"Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
"The only real mistake is the one from which we learn nothing." -- John Powell

The for loops will find the number the user wants to move. It's understood in the assignment that they enter a number that's actually in the puzzle. Just for kicks though I ran the program and entered '27' for the number to move and it just returned 0 (which in main prints out "enter a valid number to move to the open spot.")

Then you got lucky that neither of the two valid checks in that case came up good. The problem is that (0,0) is a perfectly valid thing for (a, b) to be afterwards -- you should start them off at say (-1, -1) and if they haven't changed after the for-loop, jump early.

"Simplicity does not precede complexity, but follows it." -- Alan Perlis
"Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
"The only real mistake is the one from which we learn nothing." -- John Powell

Yea I know gets would be better but for this assignment it's not necessary. I'm still confused though. I know I need to swap the elements of the array, but like I said earlier we can't change the function prototypes. So swap is destined to swap ints.

Yea I know gets would be better but for this assignment it's not necessary. I'm still confused though. I know I need to swap the elements of the array, but like I said earlier we can't change the function prototypes. So swap is destined to swap ints.

Good! That's what you want!

Edit: Of course at some point you need to figure out which way the piece can move. (Deciding that the move is valid is not enough--you must know what the move is actually going to be.)