0

Here , I wrote a code implementing addition operations on a Sparse Matrix using member function operator of a class . Now while the input functions work fine . The addtion function shows error with normal elements but works fine with diagonal elements . and the elements in the first row . I have pasted my code and my output screenshots below . Any suggestions whatsover whould be very helpful in solving the problem .

**My Code** 

#include <iostream>

using namespace std;

  class Element{

  public:
  int i ;
  int j ;
  int x;
   };

 class Sparse
{
  private:
int m ;
int n;
int num;
Element *ele;

  public:
   
 Sparse(int m,int n,int num)
{
    this->m = m;
    
    this->n = n;
    
   this->num = num;
    
 ele = new Element[this->num];
  }
   ~Sparse()
  {
    delete []ele;
   }
  Sparse operator+(Sparse &s);

 friend  istream & operator >>(istream &is,Sparse &s);

friend ostream & operator << (ostream &os,Sparse &s);
   };

  Sparse Sparse::operator+(Sparse &s)
     {
    int i ,j,k;

    if (m!=s.m || n!=s.n)
        return Sparse(0,0,0);
    Sparse *sum  = new Sparse(m,n,num+s.num);
    i=j=k=0;
    while (i<num && j<s.num)
   
   {
   if (ele[i].i<s.ele[j].i)
   
 sum->ele[k++]=ele[i++];

 else if (ele[i].i > s.ele[j].i)
  
 sum->ele[k++]=s.ele[j++];

    else
  {
 if (ele[i].j<s.ele[j].j)
    
      sum->ele[k++]=ele[i++];

 else if (ele[i].j > s.ele[j].j)
       
       sum->ele[k++]=s.ele[j++];

else
{
 sum->ele[k]=ele[i];
   
  sum->ele[k++].x=ele[i++].x+s.ele[j++].x;
  }
 
     }

   }

  for (;i<num;i++)

 {
    sum->ele[k++] = ele[i];
 }

   for (;j<s.num;j++)

 {
    sum->ele[k++] = s.ele[i];
 }

 sum->num = k;

 return *sum ;

 }


istream & operator >>(istream &is,Sparse &s)
{
    cout << "Enter non-zero Elements" << endl ;
    for (int i = 0;i<s.num ;i++)
    {
        cin >> s.ele[i].i >> s.ele[i].j >> s.ele[i].x;
    }
    return is;

}

ostream & operator << (ostream &os,Sparse &s)
{
    int k = 0;

    for (int i=0; i<s.m; i++)
    {
        for (int j=0; j<s.n; j++)
        {
            if (s.ele[k].i == i && s.ele[k].j == j)
                
             cout << s.ele[k++].x << " " ;
            
              else
                
              cout << "0 " ;
       
              }
        
           cout << endl ;
    }

    return os ;

   }



  int main()

 {

 Sparse s1(5,5,5);

 Sparse s2(5,5,5);

 cin >> s1;

  cin >> s2;

Sparse sum = s1+s2 ;

cout << "First Matrix " << endl << s1 ;

cout << "Second Matrix" << endl << s2 ;

cout << "Sum Matrix " << endl << sum ;

 return 0 ;

}

Output I : ( works correct )

  Enter non-zero Elements 
  0 0 5
  1 1 4
  2 2 3
  3 3 5 
  4 4 8
  Enter non-zero Elements 
  0 1 8
  1 1 4
  1 2 5 
  1 3 9
  2 1 6
  First Matrix
  5 0 0 0 0
  0 4 0 0 0
  0 0 3 0 0
  0 0 0 5 0
  0 0 0 0 8
  Second Matrix
  0 8 0 0 0
  0 4 5 9 0
  0 6 0 0 0 
  0 0 0 0 0 
  0 0 0 0 0
  Sum Matrix
  5 8 0 0 0
  0 8 5 9 0
  0 6 3 0 0
  0 0 0 5 0
  0 0 0 0 8

Output II :(shows error)

  enter non-zero elements
  0 2 3
  1 3 4
  2 4 6
  3 2 8
  4 1 9
  enter non-zero elements 
  0 2 6
  1 4 2
  2 1 6
  3 2 8
  4 1 4
  First Matrix
  0 0 3 0 0
  0 0 0 4 0
  0 0 0 0 0
  0 0 8 0 0
  0 9 0 0 0
  Second Matrix
  0 0 6 0 0
  0 0 0 0 2
  0 6 0 0 0
  0 0 8 0 0
  0 4 0 0 0
  Sum Matrix
  0 0 9 0 0
  0 0 0 4 0
  0 0 0 0 0
  0 0 0 0 0
  0 0 0 0 0
Quenti_20
  • 1
  • 3

0 Answers0