# How to swap columns in sudoku



## GSquadron (Aug 25, 2011)

Hi guys with another thread about  sudoku
I can swap the rows with this one, but cannot swap columns 
Is there a way to swap the columns?
The array is this one:

```
int row[9][9] = {{1,2,3,4,5,6,7,8,9},
        {4,5,6,7,8,9,1,2,3},
        {7,8,9,1,2,3,4,5,6},
        {2,3,4,5,6,7,8,9,1},
        {5,6,7,8,9,1,2,3,4},
        {8,9,1,2,3,4,5,6,7},
        {3,4,5,6,7,8,9,1,2},
        {6,7,8,9,1,2,3,4,5},
        {9,1,2,3,4,5,6,7,8}
    };
```


----------



## hellrazor (Aug 25, 2011)

First you'd want to make all the tiles pointers, that way if you want to swap columns you can go down each row and switch the value of the pointer in column A with the value of the pointer in column B.


----------



## GSquadron (Aug 25, 2011)

Or in other words without pointers u are saying like this:
swap(row[9][1],row[9][2]);
............
............
Just that row[9][1] will become a pointer?
row[9][2] too?

I think with pointers, the coding is even more long.
If there is really no other way, like doing it with only a command.


----------



## hellrazor (Aug 25, 2011)

Oh, I'm sorry, I meant reference instead of value. So they could all be pointers pointing to wherever, and then you switch where each on points..... yeah, I guess you could do what you were thinking..... but mine's cheating more than anything, which is cooler.

So yeah, do what you suggested and I'll go back to not having used anything C-related in a very long while.


----------



## Kreij (Aug 25, 2011)

Sure you can just use a for loop. Let's say you want to swap col1 with 2, 3 with 4, etc.
You should really use a more decriptive name for your array, so I'll use SudokuArray instead of calling the 2D array "row" 

```
int temp; // temporary variable to store column value while swapping
for (int row = 0; row < 9; row++) // iterate through each of the rows
{
    for (int col = 0; col < 9; col = col + 2)  // iterate through every other column
    {
        temp = SudokuArray[row][col];
        SudokuArray[row][col] = SudokuArray[row][col + 1];
        SudokuArraay[row][col + 1] = temp;
    }
}
```


----------



## GSquadron (Aug 25, 2011)

What type should temp be?
Because u cannot swap an integer with a multiD array integer


----------



## ctrain (Aug 25, 2011)

Aleksander Dishnica said:


> What type should temp be?
> Because u cannot swap an integer with a multiD array integer



... int

why couldn't you?


----------



## GSquadron (Aug 25, 2011)

int temp = int array[9][9]?
It couldnt do that
Anyway i finished the sudoku and will make an rpg text game which i will give online for free normally(1 million dollars maybe  )
I dont see anyone on the net (forums) giving free rpg text games though ?
Also, i am learning 3D games now, so not even time to spend.
I need to do the strategy game as best as possible.


----------



## ctrain (Aug 25, 2011)

it couldn't do that because it's not valid.

int temp = int array[9][9];

not the same as something like.

int array[9][9];
int temp = array[0][1];

http://codepad.org/CgKwqULJ


----------



## GSquadron (Aug 25, 2011)

I did that in another way, more practical.
Normally this was the first sudoku i make in life so it was a punch in the stomach for sure.
I found out that is better doing things yourself rather than waiting for others to explain them
to you. And this is the harsh truth.


----------



## Kreij (Aug 26, 2011)

Aleksander Dishnica said:


> I found out that is better doing things yourself rather than waiting for others to explain them
> to you. And this is the harsh truth.



Welcome to coding.


----------

