Saturday 11 April 2020

Program : display matrix using class in c++

Write a Program to design a class to represent a matrix. The class should have the functionality to insert and retrieve the elements of the matrix.



#include<iostream.h>
class matrix

int **p; 
int d1,d2;
public: 
matrix(int x, int y); 
void get_element(int i, int j, int value) 

p[i][j]=value; 

int & put_element(int i, int j) 

return p[i][j]; 
}
};
matrix ::matrix(int x, int y)

d1 = x;  d2 = y; 
p = new int *[d1]; 
for(int i = 0; i < d1; i++) 
p[i] = new int[d2];
}
int main()

int m, n;
cout<<"Enter size of matrix"; 
cin>>m>>n; 
matrix A(m,n); 
cout<<"Enter Matrix Element row by row:"; 
int i,j,value;
for(i=0;i<m;i++) 
for(j=0;j<n;j++) 
{   
cin>>value;   
A.get_element(i,j,value); 

cout<<"\n";
for(i=0;i<m;i++)

for(j=0;j<n;j++) 
cout<<A.put_element(i,j);
cout<<"\n";

return 0;
}

No comments:

Post a Comment