Coders Packet

Representation of Sparse Matrix (Using C++)

By Sangeetha Prabhu

In numerical analysis and scientific computing, a sparse matrix is a two-dimensional matrix in which most of the elements are zero.

SPARSE MATRIX

A sparse matrix is a two-dimensional array in which most of the elements are zero. The number of zero elements should be greater than half of the total number of elements.

A sparse matrix is ​​the core of a linear algebra system. Needless to say, anything important in a sufficiently complex computer system requires many linear algebra operations. You can't really render very large high-dimensional arrays in memory (when most of them are zero) and manipulate them. It is used in linear algebra rules, Computer Graphics, machine learning, and recommendation systems.
Recommendation System: Whenever Google or Amazon recommends something, relax and multiply many scattered vectors (matrices) to get the result.
Machine Learning Similarly, almost all learning methods rely heavily on sparse vector and matrix operations.
The information retrieval index is nothing more than a sparse matrix made intelligently.
has dozens of other use cases (social media, maps, and graph-based apps) where you need to store the relationships between n objects, all of which use sparse matrices.

REPRESENTATION OF A SPARSE MATRIX

 

#include <bits/stdc++.h>
using namespace std ;

int main ()
{
  const int size =20; int l=1;
  cout<<"SPARSE MATRIX"<<endl;
  int A[size][size],B[size][3], R , C,Z,nz;
  cout<<"Enter the no. Rows in  the matrix   :";
  cin>> R;
  cout<<"Enter the no. columns in  the matrix    :";
  cin>> C;
  for(int i=0 ;i<R;i++)
  for(int j=0 ;j<C;j++)
  {
    cout<<"Enter Element A["<<i+1<<"]["<<j+1<<"]   :";
    cin>>A[i][j];
    if(A[i][j]==0) Z++;
    else nz++;
  }
  if(Z>((R*C)/2))
  {    B[0][0]= R;
       B[0][1]= C;
       B[0][2]= nz;
    for(int i=0;i<R;i++)
    for(int j=0;j<C;j++)
    if(A[i][j]!=0)
      {
      	B[l][0]= i;
      	B[l][1]= j;
      	B[l][2]= A[i][j];
      l++;
      }
  }
     cout<<"The Sparse matrix is given by    :\n";
     for(int i=0;i<l;i++)
     {
      for(int j=0;j<3;j++)
       cout<<B[i][j]<<"   ";
       cout<<endl;
     }
  
  return 0;
}

OUTPUT

 

 

Download project

Reviews Report

Submitted by Sangeetha Prabhu (BuzzingBee)

Download packets of source code on Coders Packet