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.


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.



#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<<"]   :";
    if(A[i][j]==0) Z++;
    else nz++;
  {    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++)
      	B[l][0]= i;
      	B[l][1]= j;
      	B[l][2]= A[i][j];
     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]<<"   ";
  return 0;




Download Complete Code


No comments yet