2-d array

I am given to swap 2-d array i.e. swap row with column. i think my concept is correct but it is not printing on screen.

if 2-d array is given :
2 1 4 9
1 3 7 7
5 8 6 3
7 2 1 2

then it should print on screen (after swaping) :
9 1 4 2
7 3 7 1
3 8 6 5
2 2 1 7

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
#include<iostream.h>
void swapcol(int [50][50],int,int);
int main()
{int a[50][50],r,c,m,n;
 cout<<"enter no of row \n";
  cin>>m;
  cout<<"enter no of column \n";
  cin>>n;
  cout<<"enter element \n";
  for(r=0;r<m;r++)
  { for(c=0;c<n;c++)
       {cin>>a[r][c];
        }
   }
  swapcol(a,m,n);
  cout<<"after swaping \n";
   for(r=0;r<m;r++)
  { for(c=0;c<n;c++)
       {cout<<a[r][c];
	cout<<"done"<<endl;
       }
   }
return 0;
}
void swapcol(int a[50][50],int m,int n)
{int r,c,c1,b;
  for(r=0;r<m;r++)
  {  for(;c=0,c1=n-1;)
      { b=a[r][c];
         a[r][c1]=a[r][c];
      a[r][c1]=b;
      }
}
}



please help me!!
Last edited on
Your problem is in swapcol() line 30;

replace that line with: a[r][c] = a[r][c1];
1
2
3
4
5
6
7
8
9
void swapcol( int a[50][50], int m, int n )
{
   for ( int r = 0; r < m; r++ )
   {  
      int t = a[r][0];
      a[r][0] = a[r][n-1];
      a[r][n-1] = t;
   }
}
Topic archived. No new replies allowed.