User:Chechu-20067/sandbox

////7. Write a program to add 2 sparse Matrices. Check all validation using namespace std;
 * 1) include
 * 2) define max 10

class Sparse {   int pc, da, pb, c[max][3], z, s;

public: void sparseAddition(int A[][3], int B[][3]) {       da = pb = pc = 1; while ((da <= A[0][2]) && (pb <= B[0][2])) {           if (A[da][0] == B[pb][0]) {               if (A[da][1] == B[pb][1]) {                   s = A[da][2] + B[pb][2]; if (s != 0) {                       c[pc][0] = A[da][0]; c[pc][1] = A[da][1]; c[pc][2] = s;                       pb++; da++; pc++; }                   else {                       da++; pb++; }               }                else {                   if (A[da][1] < B[pb][1]) // Use '<' for correct order {                       c[pc][0] = A[da][0]; c[pc][1] = A[da][1]; c[pc][2] = A[da][2]; da++; pc++; }                   else {                       c[pc][0] = B[pb][0]; c[pc][1] = B[pb][1]; c[pc][2] = B[pb][2]; pb++; pc++; }               }            }            else {               if (A[da][0] < B[pb][0]) // Use '<' for correct order {                   c[pc][0] = A[da][0]; c[pc][1] = A[da][1]; c[pc][2] = A[da][2]; da++; pc++; }               else {                   c[pc][0] = B[pb][0]; c[pc][1] = B[pb][1]; c[pc][2] = B[pb][2]; pb++; pc++; }           }        }        while (pb <= B[0][2]) {           c[pc][0] = B[pb][0]; c[pc][1] = B[pb][1]; c[pc][2] = B[pb][2]; pb++; pc++; }       while (da <= A[0][2]) {           c[pc][0] = A[da][0]; c[pc][1] = A[da][1]; c[pc][2] = A[da][2]; da++; pc++; }       z = pc - 1; c[0][0] = A[0][0]; c[0][1] = A[0][1]; c[0][2] = pc - 1; }   void displaysum {       for (int i = 0; i <= z; i++) {           for (int j = 0; j < 3; j++) cout << c[i][j] << "\t\t "; cout << endl; }   }    void display(int A[][3]) {       for (int i = 0; i <= A[0][2]; i++) {           for (int j = 0; j < 3; j++) cout << A[i][j] << "\t\t"; cout << endl; }   } };

int main {   Sparse s1, s2, s3; int A[max][3], B[max][3], j, s, i; top: cout << "enter the row size, column size and non zero elements size of matrix A: "; cin >> A[0][0] >> A[0][1] >> A[0][2]; if ((A[0][0] <= max) && (A[0][1] <= max) && (A[0][0] != 0) && (A[0][1] != 0) && (A[0][2] != 0)) {   top1: cout << "\n enter the row size, column size and non zero elements size of matrix B: "; cin >> B[0][0] >> B[0][1] >> B[0][2]; if ((B[0][0] <= max) && (B[0][1] <= max) && (B[0][0] != 0) && (B[0][1] != 0) && (B[0][2] != 0)) {           if((A[0][2]<(A[0][0]*A[0][1])/2)&&(B[0][2]<(B[0][0]*B[0][1])/2)) {             if ((A[0][0] == B[0][0]) && (A[0][1] == B[0][1])) {               cout << "\n Enter rows, columns and non-zero elements of matrix A: \n"; for (i = 1; i <= A[0][2]; i++) { cout<<"\n enter the row of "<> A[i][0]; do                   { if((A[i][0]<0)||(A[i][0]>(A[0][0]-1))) {                         cout<<"\n invalid entry..!!.enter a valid entry(between 0-"<>A[i][0]; }                   }while((A[i][0]<0)||(A[i][0]>(A[0][0]-1))); cout<<"\n enter the column of "<>A[i][1]; do                   { if((A[i][1]<0)||(A[i][1]>(A[0][1]-1))) {                         cout<<"\n invalid entry..!!.enter a valid entry(between 0-"<>A[i][1]; }                   }while((A[i][1]<0)||(A[i][1]>(A[0][1]-1))); cout<<"\n enter the "<>A[i][2]; do                  { if(A[i][2]==0) {                      cout<<"\n enter a number other than zero: "; cin>>A[i][2]; }                  }while(A[i][2]==0); }               cout<> B[i][0]; do                   { if((B[i][0]<0)||(B[i][0]>(B[0][0]-1))) {                         cout<<"\n invalid entry..!!.enter a valid entry(between 0-"<>B[i][0]; }                   }while((B[i][0]<0)||(B[i][0]>(B[0][0]-1))); cout<<"\n enter the column of "<>B[i][1]; do                   { if((B[i][1]<0)||(B[i][1]>(B[0][1]-1))) {                         cout<<"\n invalid entry..!!.enter a valid entry(between 0-"<>B[i][1]; }                   }while((B[i][1]<0)||(B[i][1]>(B[0][1]-1))); cout<<"\n enter the "<<1<<" non zero element: "; cin>>B[i][2]; do                  { if(B[i][2]==0) {                      cout<<"\n enter a number other than zero: "; cin>>B[i][2]; }                  }while(B[i][2]==0); }               cout << "\n matrix A: \n"; s1.display(A); cout << "\n matrix B:\n"; s2.display(B); s3.sparseAddition(A, B); cout << "\n sum of two sparse matrices is:\n"; s3.displaysum; }           else cout << "\n rows and columns of two matrices are not equal. Addition is not possible"; }         else cout<<"\n not a sparse matrix.sparse matrix addition is not possible"; }       else {           cout << "\n invalid entry...!!.( enter size of row and column between(1-10)and non zero element greater than 0 )\n"; goto top1; }   }    else {       cout << "\n invalid entry...!!.(enter size of row and column between(1-10)and non zero element greater than 0) \n"; goto top; }   return 0; }