15. WAP to convert the Sparse Matrix into non-zero form and vice-versa.
PROGRAME CODE
#include<stdio.h>
#include<iostream>
using namespace std;
class sparse_matrix{
int
A[50][50],B[50][3],row,col,k;
public:
void
datains(int row,int col){
this->row=row;
this->col=col;
cout<<"ENTER
THE DATA ->"<<endl;
for(int
i=0;i<row;i++){
for(int
j=0;j<col;j++)
cin>>A[i][j];
}
display();
k=check();
if(k){
cout<<"THIS
IS A PURSE MATRIX ...."<<endl;
cout<<"THE
TOTAL ELEMENTS ARE -> "<<(row*col)<<" THE TOTAL
NON-ZERO ELEMENTS ARE ->"<<(row*col)-k<<endl;
}
else
cout<<"THIS
IS NOT A PURSE MATRIX ...."<<endl;
}
int
check(){
int
count=0;
for(int
i=0;i<row;i++){
for(int
j=0;j<col;j++){
if(A[i][j]==0)
count++;
}
}
if(count>=(row*col)/2)
return
count;
else
return
0;
}
void
purse(){
int
p=0;
for(int
i=0;i<row;i++){
for(int j=0;j<col;j++){
if(A[i][j]!=0){
B[p][0]=i;
B[p][1]=j;
B[p][2]=A[i][j];
p++;
}
}
}
}
void
display(){
cout<<"THE
MATRIX IS->"<<endl;
for(int
i=0;i<row;i++){
for(int
j=0;j<col;j++){
cout<<A[i][j]<<" ";
}
cout<<endl;
}
}
void
display1(){
cout<<"THE
NEW MATRIX IS->"<<endl;
for(int
i=0;i<((row*col)-k);i++){
for(int
j=0;j<col;j++){
cout<<B[i][j]<<" ";
}
cout<<endl;
}
}
};
int main(){
int
row=0,col=0;
sparse_matrix
ob;
cout<<"ENTER
THE ROW VALUE -> "<<endl;
cin>>row;
cout<<"ENTER
THE COL VALUE -> "<<endl;
cin>>col;
ob.datains(row,col);
ob.purse();
ob.display1();
return
0;
}
No comments:
Post a Comment