An algorithm for triplex representation of sparse matrix.

####An algorithm for triplex representation for sparse matrix and to show whether it is sparse matrix or not.
Flowchart given below:







Now we will implement this into C program.
 Source Code:
#include <iostream>
using namespace std;
int main(int argc, char const *argv[]) {
  int i,j,k=0,a[100][100],m,n,count=0;
  std::cout << "Enter the number of rows and columns: " ;
  std::cin >> m>>n;
  std::cout << "Enter the array elements: " << '\n';
  for (size_t i = 0; i <m; i++) {
    for (size_t j = 0; j<n;j++) {
        std::cin >> a[i][j];
        if (a[i][j]==0) {
          count=count+1;
        }
    }
  }
if (count>(m*n)/2) {
  std::cout << "This is a sparse matrix." << '\n';
}else{
  std::cout << "Not a sparse matrix." << '\n';
}
  for(i=0;i<n;i++){
    for(j=0;j<m;j++){
      std::cout << a[i][j] << " ";
    }
    std::cout << '\n';
  }


  return 0;
}


Output:
Enter the number of rows and columns: 3 3
Enter the array elements:
0
0
0
1
2
0
3
0
4
This is a sparse matrix.
0 0 0
1 2 0
3 0 4

Press any key to continue . . .





Explanation:
Here we first took a two dimensional array.Then we took the number of rows and columns.Then we took the element of that two dimensional array.The we examine that whether it is a sparse matrix or not.
 Conditions of sparse matrix:
**It must be a two dimensional array.
**The multiplication of the rows and columns must be greater than the the total number of zero values.
** If n is the number of rows and m is the number of columns and count is the number of zero values then ,to be sparse matrix ,it must fulfil the condition (( m*n>count)).If the condition is fulfilled then it will be called sparse matrix.And finally we printed the matrix in triplex representation.















No comments

Powered by Blogger.