Wednesday, March 20, 2013

Column Swap Of A Matrix

In column swap operation the columns of the matrix is swaped.............
i.e., the columns are interchanged................


Program:


#include<iostream>
#include<iomanip>
#include<windows.h>


using namespace std;




int main()
{
                            system("cls");
                            cout<<setw(54)<<"*****------------------------*****\n";
                            cout<<setw(54)<<"*****SWAP COLUMNS OF A MATRIX*****\n";
                            cout<<setw(54)<<"*****------------------------*****\n";
                            cout<<"\n\n";
                            int a[100][100],i,j,m,n;
                            cout<<"\tEnter number of rows : ";
                            cin>>m;
                            cout<<"\tEnter number of columns : ";
                            cin>>n;
                            cout<<"\n\n\tEnter the matrix : \n";
                            for(i=0;i<m;++i)
                            {
                                 for(j=0;j<n;++j)
                                 {
                                     cout<<"\tEnter ["<<i<<"]["<<j<<"] : ";
                                      cin>>a[i][j];
                                 }
                            }
                            cout<<"\n\n\t\t\tProcessing......";
                            Sleep(2200);
                            system("cls");
                            cout<<"\n\n\n\t\t\tThe matrix is : \n\n";
                            for(i=0;i<m;++i)
                            {
                                cout<<"\t\t\t";
                                for(j=0;j<n;++j)
                                {
                                     cout<<a[i][j]<<" ";
                                }
                                cout<<endl;
                            }
                            int p=n-1;
                            for(j=0;j<n/2;++j)
                            {
                                for(i=0;i<m;++i)
                                {
                                    int temp=a[i][j];
                                    a[i][j]=a[i][p];
                                    a[i][p]=temp;
                                }
                                --p;
                            }
                            cout<<"\n\n\t\t\tThe swaped matrix is : \n\n";
                            for(i=0;i<m;++i)
                            {
                                cout<<"\t\t\t";
                                for(j=0;j<n;++j)
                                {
                                    cout<<a[i][j]<<" ";
                                }
                                cout<<endl;
                            }

                            return 0;
}



Output:





0 comments:

Post a Comment