add tupple form

pull/818/head
Sandra 2022-08-28 23:12:51 +05:30
parent 005af5d471
commit b5aaf761e3
1 changed files with 66 additions and 10 deletions

View File

@ -1,40 +1,96 @@
/*Description :c++ solution to check if a given matrix is sparse matrix. /*Description :c++ solution to check if a given matrix is sparse matrix.
If the count of zeroes present in the mmatrix is more than half the elements of the matrix, If the count of zeroes present in the mmatrix is more than half the elements of the matrix,
it is flagged as a sparse matrix.*/ it is flagged as a sparse matrix.
Also the tuple form of the matrix(if the said matrix is sparse) is displayed.*/
#include<iostream> #include<iostream>
using namespace std; using namespace std;
int compactMatrix(int sparseMatrix[4][5])
{
int size = 0;
for (int i = 0; i < 4; i++)
for (int j = 0; j < 5; j++)
if (sparseMatrix[i][j] != 0)
size++;
/* number of columns in compactMatrix (size) must be
equal to number of non - zero elements in sparseMatrix */
int compactMatrix[size][3];
// Making of new matrix
int k = 0;
for (int i = 0; i < 4; i++)
for (int j = 0; j < 5; j++)
if (sparseMatrix[i][j] != 0)
{
compactMatrix[k][0] = i;
compactMatrix[k][1] = j;
compactMatrix[k][2] = sparseMatrix[i][j];
k++;
}
cout<<"The tuple form is:"<<endl;
for (int i=0; i<size; i++)
{
for (int j=0; j<3; j++)
cout <<" "<< compactMatrix[i][j];
cout <<"\n";
}
return 0;
}
int main () { int main () {
int a[10][10] = { {0, 0, 9} , {5, 0, 8} , {7, 0, 0} }; int sparseMatrix[4][5] =
{
{0 , 0 , 3 , 0 , 4 },
{0 , 0 , 5 , 7 , 0 },
{0 , 0 , 0 , 0 , 0 },
{0 , 2 , 6 , 0 , 0 }
};
int i, j, count = 0; int i, j, count = 0;
int row = 3, col = 3; int row = 4, col = 5;
for (i = 0; i < row; ++i) { for (i = 0; i < row; ++i) {
for (j = 0; j < col; ++j){ for (j = 0; j < col; ++j){
if (a[i][j] == 0) if (sparseMatrix[i][j] == 0)
count++; count++;
} }
} }
cout<<"The matrix is:"<<endl; cout<<"The matrix is:"<<endl;
for (i = 0; i < row; ++i) { for (i = 0; i < row; ++i) {
for (j = 0; j < col; ++j) { for (j = 0; j < col; ++j) {
cout<<a[i][j]<<" "; cout<<sparseMatrix[i][j]<<" ";
} }
cout<<endl; cout<<endl;
} }
cout<<"The number of zeros in the matrix are "<< count <<endl; cout<<"The number of zeros in the matrix are "<< count <<endl;
if (count > ((row * col)/ 2)) if (count > ((row * col)/ 2))
cout<<"This is a sparse matrix"<<endl; {
cout<<"This is a sparse matrix"<<endl;
compactMatrix(sparseMatrix);
}
else else
cout<<"This is not a sparse matrix"<<endl; cout<<"This is not a sparse matrix"<<endl;
return 0; return 0;
} }
/*output /*output
The matrix is: The matrix is:
0 0 9 0 0 3 0 4
5 0 8 0 0 5 7 0
7 0 0 0 0 0 0 0
The number of zeros in the matrix are 5 0 2 6 0 0
The number of zeros in the matrix are 14
This is a sparse matrix This is a sparse matrix
The tuple form is:
0 2 3
0 4 4
1 2 5
1 3 7
3 1 2
3 2 6
*/ */