Hello,
I am working on a swap function with 2 dimensional arrays. I have it working if I write it as an If statement but cannot get it to work as a function.
It should basically initialize the array with the square root of whatever you give d and then print the array to the screen so a 4*4 array should print out as
15 14 13 12
11 10 9 8
7 6 5 4
3 2 1 0
The swap function should swap the 2 and the 1 so it should print out as
15 14 13 12
11 10 9 8
7 6 5 4
3 1 2 0
I tried to use pointers because I believe that would be best but I removed them because I could not figure out how to pass a two dimensional array that I needed to perform math on as a pointer to the function.
Attempting to use it as a function call.
Code:
void
init(void)
{
int row,col,num=d*d,remain=d%2;
// Initalize the board with a two dimensional array.
for(row=0;row<d;row++) {
for(col=0;col<d;col++) {
num-=1; /* Start subtracting 1 from d*d Example 16-1=15 */
board[row][col] = num;
}
}
if(remain==0) {
swap(board[d-1][d-2],board[d-1][d-3]);
}
}
/*
* Prints the board in its current state.
*/
void
draw(void)
{
// Draw the board based upon the argument provided running the game.
int row,col;
for(row=0;row<d;row++) {
printf("\n");
for(col=0;col<d;col++) {
printf("%3d",board[row][col]);
}
}
printf("\n");
}
/* I know this function doesn't do anything. I cannot figure out how to use pointers so that it changes the values of the address of the array element being passed to it. */
void
swap(int a, int b) {
int temp;
temp = a;
a = b;
b = temp;
temp = 0;
draw();
}
Any help would be great.
Thanks,
Wayne