"The best way to cheer yourself up is to try to cheer somebody else up." Mark Twain

Wednesday, March 24, 2010

Define a function SWAPCOL() in C++ to swap (interchange) the first column elements with the last column elements, for a two dimensional integer array passed as the argument of the function.

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
{ 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

C makes it easy to shoot yourself in the foot; C++ makes it harder, but when you do it blows your whole leg off.
Now Playing: Ballade Pour Adeline

About Me

My photo
I m an IT lecturer of a college. I love social-work. I want to do something beneficial for society before dying , that can promote our society, to some extent.