Example : If the two dimensional array contains
2 1 4 9
1 3 7 7
5 8 6 3
7 2 1 2
After swapping of the content of 1st column and the last column, it should be :
9 1 4 2
7 3 7 1
3 8 6 5
2 2 1 7 (CBSE 2009)
#include <iostream.h>
#include <conio.h>
void swapcol(int a[][], int r, int c)
// r -> row, c ->col
// r -> row, c ->col
{ int t;
for(int i = 0; i< r ; i++)
{
t = a[i][0];
a[i][0] = a[i][c-1];
a[i][c-1] = t;
}
}
void main()
{
int arr[10][10];
int m, n, i, j;
cout<<"\n Enter rows & columns :";
int m, n;
cin >> m >> n;
/* Input Array Elements */
for ( i = 0; i< m ; i++)
for( j = 0; j < n ; j++)
{
cout<< "\n Enter element for the array[ " << i+1 << " ] "<< " [ " << j+i << "]" ;
cin >> arr[i][j];
}
/* Swap function call */
swapcol(arr, m, n);
/* Display Array in Matrix form */
for( i = 0; i < m ; << i ++)
{ cout<< endl;
for ( j = 0; j < n ; j ++)
cout << arr[i][j] << " ";
}
getch();
}
No comments:
Post a Comment