3. Write a CPP program to find transpose of 2D matrix and allocate memory dynamically to the matrix using dynamic memory allocation. Initialize and display contents of the matrix and deallocate memory.


 // TRANSPOSE OF A MATRIX using DYNAMIC MEMORY ALLOCATION


#include<iostream.h>
class trans
{
            int **a,**b;
            int m,n;
  public:
            void initialize();
            void transpose();
            void display();
            void deallocate();
};
 
void trans::initialize()
{
            int i,j;
            cout<<"No. of Rows and Columns: ";
            cin>>m>>n;
            a=new int *[m];   // create input matrix
            for(i=0; i<m; i++)
                        a[i]=new int[n];
            cout<<"Enter elements:\n";
            for(i=0; i<m; i++)
            for(j=0; j<n; j++)  cin>>a[i][j];
            b=new int *[n];  // create transpose matrix
            for(i=0; i<n; i++)
                        b[i]=new int[m];
}

void trans::transpose() 
{
            int i,j; 
            for(i=0; i<m; i++)
            for(j=0; j<n; j++)    // transpose is in 'b'
                        b[j][i]=a[i][j];
}

void trans::display() 
{
            int i,j;
            cout<<"Transpose:\n";
            for(i=0; i<n; i++)
            {
                        for(j=0; j<m; j++)
                                    cout<<b[i][j]<<" ";
                        cout<<"\n";
            }
}

void trans::deallocate() 
{
            int i;
            for(i=0; i<m; i++) delete a[i];
            delete a;
            for(i=0; i<n; i++) delete b[i];
            delete b;
}

int main() 
{
            trans t;
            t.initialize();
            t.transpose();
            t.display();
            t.deallocate();
}
Posted by Unknown On 01:45 No comments

0 comments:

Post a Comment

  • RSS
  • Delicious
  • Digg
  • Facebook
  • Twitter
  • Linkedin
  • Youtube

Blog Archive

Contact Us


Name

E-mail *

Message *