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

Assignment Help:

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


Related Discussions:- C program to allocate memory dynamically for 2-d array

Inside and outside type casting, depth description of the inside and outsid...

depth description of the inside and outside typecasting

What should one catch?, A: By keeping along with the C++ tradition of "ther...

A: By keeping along with the C++ tradition of "there's more than one method to do that" (translation: "give programmers options & tradeoffs so they can choose what's best for them

Write a c program to input three real numbers, Write a C program to input f...

Write a C program to input five numbers and print them out on a new line Write a C program to input three real numbers and print them out as follows:   The first variable is

Define a class?, Define a class? A: It is an expanded concept of a data ...

Define a class? A: It is an expanded concept of a data structure: rather than holding only data, it can hold data and functions both.

Should one design a classes from the outside, Should one design a classes f...

Should one design a classes from the outside (interfaces first) or inside (data first)? A: From the outside. A superior interface provides a simplified view which is express

Stack, write a simple c++ program to implement a stack: 1. push 2. pop

write a simple c++ program to implement a stack: 1. push 2. pop

Objects as function arguments, Objects as Function Arguments: In C prog...

Objects as Function Arguments: In C program there are several methods to define arguments, and in some case even a structure can be approved as an argument.  Similarly in C+

Algorithm, algorithm to prepare mark sheet of a student by inputing name,br...

algorithm to prepare mark sheet of a student by inputing name,branchcode,semester,register no,5 marks of students and total mark of student

Theory, recursive sub programs

recursive sub programs

Write Your Message!

Captcha
Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd