assignment_17.cpp (5225B)
1 /* WAP to convert the Sparse Matrix into non-zero form and vice-versa. */ 2 3 #include <iostream> 4 using namespace std; 5 6 class SparseMatrix { 7 private: 8 int rows; 9 int cols; 10 int **standardMatrix; 11 int **triplet; 12 int nonZeroCount; 13 14 void allocateStandard(int r, int c) { 15 rows = r; 16 cols = c; 17 standardMatrix = new int*[rows]; 18 for (int i = 0; i < rows; i++) { 19 standardMatrix[i] = new int[cols]; 20 for (int j = 0; j < cols; j++) standardMatrix[i][j] = 0; 21 } 22 } 23 24 void freeStandard() { 25 if (standardMatrix != nullptr) { 26 for (int i = 0; i < rows; i++) delete[] standardMatrix[i]; 27 delete[] standardMatrix; 28 standardMatrix = nullptr; 29 } 30 } 31 32 void freeTriplet() { 33 if (triplet != nullptr) { 34 for (int i = 0; i <= nonZeroCount; i++) delete[] triplet[i]; 35 delete[] triplet; 36 triplet = nullptr; 37 } 38 } 39 40 public: 41 SparseMatrix() { 42 rows = 0; cols = 0; nonZeroCount = 0; 43 standardMatrix = nullptr; 44 triplet = nullptr; 45 } 46 47 void inputStandardMatrix() { 48 freeStandard(); 49 freeTriplet(); 50 cout << "Enter number of rows: "; cin >> rows; 51 cout << "Enter number of columns: "; cin >> cols; 52 if (rows <= 0 || cols <= 0) { cout << "Invalid matrix dimensions!\n"; rows = cols = 0; return; } 53 allocateStandard(rows, cols); 54 cout << "Enter matrix elements (" << rows << " x " << cols << "):\n"; 55 for (int i = 0; i < rows; i++) 56 for (int j = 0; j < cols; j++) cin >> standardMatrix[i][j]; 57 } 58 59 void convertToTriplet() { 60 if (standardMatrix == nullptr) { cout << "No standard matrix available!\n"; return; } 61 freeTriplet(); 62 nonZeroCount = 0; 63 for (int i = 0; i < rows; i++) 64 for (int j = 0; j < cols; j++) 65 if (standardMatrix[i][j] != 0) nonZeroCount++; 66 67 triplet = new int*[nonZeroCount + 1]; 68 for (int i = 0; i <= nonZeroCount; i++) triplet[i] = new int[3]; 69 70 triplet[0][0] = rows; 71 triplet[0][1] = cols; 72 triplet[0][2] = nonZeroCount; 73 74 int k = 1; 75 for (int i = 0; i < rows; i++) 76 for (int j = 0; j < cols; j++) 77 if (standardMatrix[i][j] != 0) { 78 triplet[k][0] = i; 79 triplet[k][1] = j; 80 triplet[k][2] = standardMatrix[i][j]; 81 k++; 82 } 83 } 84 85 void inputTripletForm() { 86 freeStandard(); 87 freeTriplet(); 88 int r, c, count; 89 cout << "Enter total rows: "; cin >> r; 90 cout << "Enter total columns: "; cin >> c; 91 cout << "Enter number of non-zero elements: "; cin >> count; 92 if (r <= 0 || c <= 0 || count < 0 || count > r * c) { cout << "Invalid input parameters!\n"; return; } 93 94 rows = r; cols = c; nonZeroCount = count; 95 triplet = new int*[nonZeroCount + 1]; 96 for (int i = 0; i <= nonZeroCount; i++) triplet[i] = new int[3]; 97 98 triplet[0][0] = rows; 99 triplet[0][1] = cols; 100 triplet[0][2] = nonZeroCount; 101 102 cout << "Enter (row, col, value) for each non-zero element:\n"; 103 for (int i = 1; i <= nonZeroCount; i++) cin >> triplet[i][0] >> triplet[i][1] >> triplet[i][2]; 104 } 105 106 void convertToStandard() { 107 if (triplet == nullptr) { cout << "No 3-tuple representation found!\n"; return; } 108 freeStandard(); 109 rows = triplet[0][0]; 110 cols = triplet[0][1]; 111 nonZeroCount = triplet[0][2]; 112 allocateStandard(rows, cols); 113 114 for (int i = 1; i <= nonZeroCount; i++) { 115 int r = triplet[i][0]; 116 int c = triplet[i][1]; 117 int val = triplet[i][2]; 118 if (r >= 0 && r < rows && c >= 0 && c < cols) standardMatrix[r][c] = val; 119 } 120 } 121 122 void displayStandard() { 123 if (standardMatrix == nullptr) { cout << "Standard matrix is empty!\n"; return; } 124 for (int i = 0; i < rows; i++) { 125 for (int j = 0; j < cols; j++) cout << standardMatrix[i][j] << " "; 126 cout << endl; 127 } 128 } 129 130 void displayTriplet() { 131 if (triplet == nullptr) { cout << "3-Tuple representation is empty!\n"; return; } 132 for (int i = 0; i <= nonZeroCount; i++) 133 cout << triplet[i][0] << " " << triplet[i][1] << " " << triplet[i][2] << endl; 134 } 135 136 ~SparseMatrix() { 137 freeStandard(); 138 freeTriplet(); 139 } 140 }; 141 142 int main() { 143 SparseMatrix sm; 144 int choice; 145 146 while (true) { 147 cout << "1.InputMatrix 2.ToTriplet 3.DisplayTriplet \n4.InputTriplet 5.ToStandard 6.DisplayMatrix -1.Exit\n"; 148 cout << "Enter your choice: "; 149 cin >> choice; 150 151 switch (choice) { 152 case 1: sm.inputStandardMatrix(); break; 153 case 2: sm.convertToTriplet(); break; 154 case 3: sm.displayTriplet(); break; 155 case 4: sm.inputTripletForm(); break; 156 case 5: sm.convertToStandard(); break; 157 case 6: sm.displayStandard(); break; 158 case -1: cout << "Exiting...\n"; return 0; 159 default: cout << "Wrong choice.\n"; 160 } 161 } 162 }