C program to allocate memory dynamically for 2-d array, C/C++ Programming

Aim: To implement a program to allocate memory dynamically for 2 dimensional array (accept and print matrix) using pointers.

Code:                      

#include

#include

class matrix

{

            int **p,r,c,i,j;

            public:

                        matrix(int,int);

                        ~matrix();

                        void getdata();

                        void display();

};

matrix::matrix(int x,int y)

{

            r=x;

            c=y;

            p=new int *[r];

            for(i=0;i

                        p[i]=new int [c];

}

matrix::~matrix()

{

            for(i=0;i

                        delete p[i];

            delete p;

            cout<<"\nMemory deallocated successfully\n";

}

void matrix::getdata()

{

            cout<<"\nEnter matrix values:\n";

            for(i=0;i

            {

                        cout<<"Enter values for row:"<

                        for(j=0;j

                                    cin>>p[i][j];

                        }

}

void matrix::display()

{

            cout<<"\nThe matrix is:\n";

            for(i=0;i

            {;

                        for(j=0;j

                                    cout<

                        cout<

            }

}

void main()

{

            int rows,cols;

            clrscr();

            cout<<"Enter number of rows and columns for a matrix\n";

            cout<<"Enter number of rows\n";

            cin>>rows;

            cout<<"Enter number of columns\n";

            cin>>cols;

            matrix A(rows,cols);

            A.getdata();

            A.display();

            getch();

}

Output:

Enter number of rows and columns for a matrix

Enter number of rows                                                           

3                                                                               

Enter number of columns                                                        

3                                                                              

Enter the values of the matrix                                                 

Enter values for row 1                                                         

1 2 3                                                                          

Enter values for row 2                                                         

4 5 6                                                                           

Enter values for row 3                                                         

7 8 9                                                                          

The matrix is                                                                   

1       2       3                                                              

4       5       6                                                              

7       8       9                                                               

Memory deallocated successfully

Posted Date: 9/29/2012 4:12:36 AM | Location : United States







Related Discussions:- C program to allocate memory dynamically for 2-d array, Assignment Help, Ask Question on C program to allocate memory dynamically for 2-d array, Get Answer, Expert's Help, C program to allocate memory dynamically for 2-d array Discussions

Write discussion on C program to allocate memory dynamically for 2-d array
Your posts are moderated
Related Questions
Byteland county is very famous for luminous jewels. Luminous jewels are used in making beautiful necklaces. A necklace consists of various luminous jewels of particular colour. Nec

Demonstration using Moss Open Source Machine Translation Project Description: Moses is an open source Statistical Machine Translation System. I need someone to provide me a d


Program is to define a class as teacher: Program is to define a class as teacher and collect information about them by using classes and object class teacher   {   pr

how c program starts executing?

what is oops

Program is to define a class as library: Program is to define a class as library and perform all the function of library management by using classes and object class librar


Syntax rules for writing constructor functions Its name must be similar as that of the class to which it belongs. It is declared with no return type (not even void). Ho

A: A reference has to always refer to some object and, so, must always be initialized; pointers do not have such limits. A pointer may be reassigned to point to distinct objects wh