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

When should you employ multiple inheritance?, Q: When should you employ mul...

Q: When should you employ multiple inheritance? A:There  are  three  acceptable  answers:-  "Rarely," "Never," and  "while  the  problem  domain cannot be modeled accurately any

Working a homework programming assignment in C++, I am working on a program...

I am working on a program that allows a player to play a game against the computer. In this game the player can only chose to draw 1, 2, or 3 toothpicks. How do I make restrictions

Compiler Design - Limit the Method Instructions, Raj is a newbie to the pro...

Raj 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 with ''}''. Ã

Program to store family members details, Program to store family members de...

Program to store family members details in C++: #include #include #include struct record {   char name[30];   int age;   char state[40]; }x; //cal

Calculate the discharge by c program, #include stdio.h #include math.h ...

#include stdio.h #include math.h float discharge(float,float); void main()   {   char prompt;      float time,out,cr;   cr = 100e-6;   for (time = 0;time   {   /* call th

Write a program to change the matrix program, Change the matrix program (pr...

Change the matrix program (program 3) slightly. Overload == operator to compare two matrices to be added or subtracted. i.e., whether the column of first and the row of second

Basic input -output library of subroutines, GetChar - get a character fro...

GetChar - get a character from the keyboard, echo to the display and return it in reg al. - Note: if a linefeed (LF) is received (Enter key), a carriage return (CR) will als

Assignment, write a function rise to the power to find the exponent of a nu...

write a function rise to the power to find the exponent of a number whether function is called

Introduction of c, Discuss the saleint featuresand application of c

Discuss the saleint featuresand application of c

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