swap elements in a column

Hi , i need help here .. i have to write a function that swaps elements in one columns for a matrix , for example :
1 3 4
3 5 9

if i choosed column #3 it will be :
1 3 9
3 5 4

here is my function and it didn't work :( :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
void col_swap ( int a[][columns] , int &row , int &col)
{
	int n,j,i,hold;

	for(i = 0; i< row ; i++)
		{
		for (j=0 ; j<col ; j++)
			if ( j==n) //n is the column that has been choosed by the user
				{
				hold = a[0][j];
				a[0][j] = a[0][j+1];
				a[0][j+1] = hold;

				cout << a[i][j] << " " << endl;
				}

		}

}


> //n is the column that has been choosed by the user

Really? Try printing out the value of n.
it's long function so i just posted the part that i'm facing a problem with :)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35

void col_swap ( int a[][columns] , int &row , int &col)
{
	int n,j,i,hold;
	cout << "Enter column number (0 to " << col-1 << "):";
	cin >> n;
	cout << "Before : " <<endl;

		for (i=0; i<row ; i++)
		{
				for (j=0 ; j<col ; j++)
					if ( j==n)
						cout << a[i][j] << " ";

		cout << endl;
		}
		cout << "After : " <<endl;

for(i = 0; i< row ; i++)
{
		for (j=0 ; j<col ; j++)
					if ( j==n)
					{
						hold = a[0][j];
						a[0][j] = a[0][j+1];
						a[0][j+1] = hold;

						cout << a[i][j] << " " << endl;
					}

		}

}

closed account (3qX21hU5)
First may I suggest some changes in your style of coding to make the code more easy to read.

1) Not so much indentation. You are indenting way to far in and it makes it hard to read. I would suggest anywhere from 2 spaces through 6 spaces for each indentation level.

2) Declare variables right before you are about to use them. When you declare all the variables for the given scope at the top users will keep having to scroll up to find out what each on is and what value they have. Also declare your loop indexes inside the for loops it makes it much cleaner. So instead of this int n,j,i,hold; I would suggest something like this.

1
2
3
4
int n;
cout << "Enter column number (0 to " << col-1 << "):";
cin >> n;
cout << "Before : " <<endl;


hold inside the if statement
1
2
3
4
5
6
7
8
if ( j==n)
{
    int hold = a[0][j];
    a[0][j] = a[0][j+1];
    a[0][j+1] = hold;

    cout << a[i][j] << " " << endl;
}


for (int i=0; i<row ; i++) for (int j=0 ; j<col ; j++)

The only reason you would want the index variables outside of the for loop would be if you needed to use the index outside of the loops scope for some reason, which you don't do here.

Them are just a few tips for you regarding formatting.\

For your current loop you don't need the row loop since you are not indexing the row only the column.

As for your problem... What exactly is it doing? Other then saying that it doesn't work we could use some more information on the error. Because I don't really see anything wrong with it, though I only did a quick search through it.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <algorithm>

constexpr std::size_t columns = 5 ;

// swap col_a and col_b
void col_swap( int a[][columns], std::size_t rows, std::size_t col_a , std::size_t col_b )
{
    if( col_a<columns && col_b<columns )
    {
        for( std::size_t r = 0 ; r < rows ; ++r  ) // for each row
        {
            // swap int at col_a with the int at col_b
            std::swap( a[r][col_a], a[r][col_b] ) ;
            /* or
            const int hold = a[r][col_a] ;
            a[r][col_a] = a[r][col_b] ;
            a[r][col_b] = hold ;
            */
        }
    }
}


http://liveworkspace.org/code/fVTYs$0
Thank you guys ,

but i can't say colA and colB , i want even if the rows more than 2 , because the swapping between all the elements in one column for example :

2 3 4 5
6 7 8 9
1 1 1 1

now :2 3 1 5
6 7 8 9
1 1 4 1
Topic archived. No new replies allowed.