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
In which condition a template a better solution than a base class? A: While you are designing a generic class to contain or manage objects of other types, while the format & beh

A: No. There is nothing you can do in C++ which you cannot do in C. In spite of everything

Introduction. In this assignment you are required to simulate a maze traversal using so called recursive backtracking (the algorithm is given below). The grid of #s and 0s in the f

Define the Category of Function? A function, depending on whether arguments or parameters are present or not and whether a value returned or not, may belong to one of the foll

Write a function that has a single int parameter n and returns an int: 0 if n is negative, otherwise 1. You are not allowed to simply check the integer value, and should use the fu

Ravi is a newbie to the programming and while learning the programming language he came to know the following rules: · Each program must start with ''''''''{'''''''' and end wi

a program to find the area under the curve y = f(x) between x = a and x = b, integrate y = f(x) between the limits of a and b. The area under a curve between tw

#questiowrite a program to calculate e^x

Differentiate between a template class and class template in C++? Ans) Template class: A generic definition or a parameterized class not instantiated until the client gives the

What's a six-letter word that has an e as its first, third, and fifth letter? Can you find an anagram of pine grave. Or how about a word that starts and ends with ant (other than a