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

Define difference among new & malloc?, Both malloc & new functions are util...

Both malloc & new functions are utilized for dynamic memory allocations & the basic difference is: malloc need a special "typecasting" while it allocates memory for eg. if the poin

#title., A student apears in exam of math, physics, and chemistry. Write a...

A student apears in exam of math, physics, and chemistry. Write a program to find the total marks aa student has aqueired find the average.

C help, Need help with C network program

Need help with C network program

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

Online tutor, Given an char variable last that has been initialized to a lo...

Given an char variable last that has been initialized to a lowercase letter, write a loop that displays all possible combinations of two letters in the range ''a'' through last. Th

Minimum Shelf , At a shop of marbles, packs of marbles are prepared. Packet...

At a shop of marbles, packs of marbles are prepared. Packets are named A, B, C, D, E …….. All packets are kept in a VERTICAL SHELF in random order. Any numbers

Write a c program as text layout, Your task in this project is to write a C...

Your task in this project is to write a C program named layout.c which does text layout. This is a staged project, complete the stages in order. The maximum mark if you nish each

Program with inbuilt functions, write a atm program in c with inbuilt funct...

write a atm program in c with inbuilt functions for 1782?

M--commerce, Project Description: We are aiming to prepare an industry f...

Project Description: We are aiming to prepare an industry first in the m-commerce world. M-commerce payment processing programmers required Skills required are C Programmi

Display an array using standard input, Stage One Define the specification o...

Stage One Define the specification of the program            Add two nxm size matrices   Stage Two Divide the program up into separate modules           Input Matrix         Ad

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