Friday, 7 December 2012

Transpose of a matrix cpp program

CPP program to implement sparse matrix 

#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
class matrix
{ private:
          int i,j;
  public:
          void getmat(int m[][10],int r,int c) ;
          void dispmat(int m[][10],int r,int c) ;
          void transpose(int m[][10],int r,int c );
};

void matrix::getmat(int m[][10],int r,int c)
{        for(i=0;i<r;i++)
                   for(j=0;j<c;j++)
                   {        cout<<" Enter Element ("<<i+1<<"," <<j+1<<" ) : ";
                             cin>>m[i][j];
                   }

}

void matrix::dispmat(int m[][10],int r,int c)
{        for(i=0;i<r;i++)
          {        for(j=0;j<c;j++)
                   cout<<m[i][j]<<" ";
                   cout<<endl;
          }
}

void matrix::transpose(int m[][10],int r,int c)
{        for(i=0;i<c;i++)
          {        for(j=0;j<r;j++)
                   cout<<m[j][i]<<" ";
                   cout<<endl;
          }
}

void main()
{        matrix m;
          int c,col,rows,mat[10][10];
          while(1)
          {        clrscr();
                   cout<<" To Do the Following Functions On Matrix.\n";
                   cout<<" ----------------------------------------\n";
                   cout<<" 1.Input a Matrix.\n";
                   cout<<" 2.Display Matrix.\n";
                   cout<<" 3.Transpose of Matrix.\n";
                   cout<<" 4.Exit from Program.\n";
                   do
                   {        cout<<" Select Your Choice(1:4) : ";
                              cin>>c;
                   }
                   while((c<1)||(c>4));
                   clrscr();
          switch(c)
          { case 1 :
cout<<" Enter the Matrix\n ";
                   cout<<"\n How Many Rows ? : ";
                   cin>>rows;
                   cout<<"\n How Many Columns ? : ";
                   cin>>col;
                   m.getmat(mat,rows,col);
                   break;
          case 2 :
cout<<"\n The Matrix You Entered is \n";
                   m.dispmat(mat,rows,col);
                   break;
          case 3 :
cout<<"\n The Original Matrix is "<<endl;
                   m.dispmat(mat,rows,col);
                   cout<<" Transpose of the given Matrix is "<<endl;
                   m.transpose(mat,rows,col);
                   break;
          case 4 :
 exit(1);break;
          }
           cout<<endl<<" Press any Key to Continue.....";
          getch();
     }
}

No comments:

Post a Comment