assignment-s-20.c (7629B)
1 /* Write a menu-driven program to perform the following matrix operations using 2-D 2 arrays and functions: 3 a. Sum 4 b. Difference 5 c. Product 6 d. Transpose 7 */ 8 9 #include <stdio.h> 10 #include <stdlib.h> 11 12 #define COL_MAX 10 13 #define COL_MIN 0 14 #define ROW_MAX 10 15 #define ROW_MIN 0 16 #define TRUE 1 17 #define FALSE 0 18 19 void input(int ***, int *, int *); 20 void display(int **, int, int); 21 void clear(int **, int); 22 void sum(int **, int **, int, int); 23 void difference(int **, int **, int, int); 24 void product(int **, int **, int, int, int); 25 void transpose(int **, int, int); 26 27 int main() 28 { 29 int **a = NULL, **b = NULL; 30 int rows_a, rows_b, cols_a, cols_b; 31 int choice; 32 33 while (TRUE) 34 { 35 printf("\n === MENU ===\n" 36 "1. Sum\n" 37 "2. Difference\n" 38 "3. Product\n" 39 "4. Transpose\n" 40 "0. Exit\n"); 41 printf("\nEnter your choice: "); 42 scanf("%d", &choice); 43 44 switch (choice) 45 { 46 case 1: 47 input(&a, &rows_a, &cols_a); 48 if (a == NULL) 49 break; 50 51 input(&b, &rows_b, &cols_b); 52 if (b == NULL) 53 { 54 clear(a, rows_a); 55 a = NULL; 56 break; 57 } 58 59 printf("\nMatrix A = \n"); 60 display(a, rows_a, cols_a); 61 printf("\nMatrix B = \n"); 62 display(b, rows_b, cols_b); 63 64 if (rows_a != rows_b || cols_a != cols_b) 65 { 66 printf("\nOrder of both matrix should be same to calculate sum."); 67 printf("\nInput : Matrix A = [%d x %d]" 68 " Matrix B = [%d x %d]", 69 rows_a, cols_a, rows_b, cols_b); 70 71 clear(a, rows_a); 72 clear(b, rows_b); 73 a = NULL; 74 b = NULL; 75 break; 76 } 77 printf("\nResult = \n"); 78 sum(a, b, rows_a, cols_a); 79 80 clear(a, rows_a); 81 clear(b, rows_b); 82 a = NULL; 83 b = NULL; 84 85 break; 86 87 case 2: 88 input(&a, &rows_a, &cols_a); 89 if (a == NULL) 90 break; 91 92 input(&b, &rows_b, &cols_b); 93 if (b == NULL) 94 { 95 clear(a, rows_a); 96 a = NULL; 97 break; 98 } 99 100 printf("\nMatrix A = \n"); 101 display(a, rows_a, cols_a); 102 printf("\nMatrix B = \n"); 103 display(b, rows_b, cols_b); 104 105 if (rows_a != rows_b || cols_a != cols_b) 106 { 107 printf("\nOrder of both matrix should be same to calculate difference."); 108 printf("\nInput : Matrix A = [%d x %d]" 109 " Matrix B = [%d x %d]", 110 rows_a, cols_a, rows_b, cols_b); 111 112 clear(a, rows_a); 113 clear(b, rows_b); 114 a = NULL; 115 b = NULL; 116 break; 117 } 118 printf("\nResult = \n"); 119 difference(a, b, rows_a, cols_a); 120 121 clear(a, rows_a); 122 clear(b, rows_b); 123 a = NULL; 124 b = NULL; 125 126 break; 127 128 case 3: 129 input(&a, &rows_a, &cols_a); 130 if (a == NULL) 131 break; 132 133 input(&b, &rows_b, &cols_b); 134 if (b == NULL) 135 { 136 clear(a, rows_a); 137 a = NULL; 138 break; 139 } 140 141 printf("\nMatrix A = \n"); 142 display(a, rows_a, cols_a); 143 printf("\nMatrix B = \n"); 144 display(b, rows_b, cols_b); 145 146 if (cols_a != rows_b) 147 { 148 printf("\nColumn count of matrix A should be same as Row count of matrix B."); 149 printf("\nInput : Matrix A = [%d x %d]" 150 " Matrix B = [%d x %d]", 151 rows_a, cols_a, rows_b, cols_b); 152 153 clear(a, rows_a); 154 clear(b, rows_b); 155 a = NULL; 156 b = NULL; 157 break; 158 } 159 printf("\nResult = \n"); 160 product(a, b, rows_a, cols_b, cols_a); 161 162 clear(a, rows_a); 163 clear(b, rows_b); 164 a = NULL; 165 b = NULL; 166 167 break; 168 169 case 4: 170 input(&a, &rows_a, &cols_a); 171 if (a == NULL) 172 break; 173 174 printf("\nMatrix = \n"); 175 display(a, rows_a, cols_a); 176 177 printf("\nResult = \n"); 178 transpose(a, rows_a, cols_a); 179 180 clear(a, rows_a); 181 a = NULL; 182 183 break; 184 185 case 0: 186 printf("\n\nExiting program...\n"); 187 return 0; 188 189 default: 190 printf("\nInvalid choice. Please pick a choice from the menu.\n"); 191 } 192 } 193 } 194 195 void input(int ***matrix, int *rows, int *cols) 196 { 197 int i, j; 198 printf("\nEnter row and column count (Max : 10 x 10): "); 199 scanf("%d %d", rows, cols); 200 201 if (*rows > ROW_MAX || *cols > COL_MAX || *rows <= ROW_MIN || *cols <= COL_MIN) 202 { 203 printf("\nInvalid dimensions. Limit is 1 to 10.\n"); 204 *matrix = NULL; 205 return; 206 } 207 208 *matrix = (int **)malloc(*rows * sizeof(int *)); 209 210 for (i = 0; i < *rows; i++) 211 { 212 (*matrix)[i] = (int *)malloc(*cols * sizeof(int)); 213 for (j = 0; j < *cols; j++) 214 { 215 printf("Enter element [%d][%d]: ", i + 1, j + 1); 216 scanf("%d", &((*matrix)[i][j])); 217 } 218 } 219 } 220 221 void display(int **matrix, int rows, int cols) 222 { 223 int i, j; 224 for (i = 0; i < rows; i++) 225 { 226 for (j = 0; j < cols; j++) 227 { 228 printf("%d ", matrix[i][j]); 229 } 230 printf("\n"); 231 } 232 } 233 234 void clear(int **matrix, int rows) 235 { 236 int i; 237 for (i = 0; i < rows; i++) 238 { 239 free(matrix[i]); 240 } 241 free(matrix); 242 } 243 244 void sum(int **a, int **b, int rows, int cols) 245 { 246 int i, j; 247 248 int **r = (int **)malloc(rows * sizeof(int *)); 249 for (i = 0; i < rows; i++) 250 { 251 r[i] = (int *)malloc(cols * sizeof(int)); 252 for (j = 0; j < cols; j++) 253 { 254 r[i][j] = a[i][j] + b[i][j]; 255 } 256 } 257 display(r, rows, cols); 258 clear(r, rows); 259 } 260 261 void difference(int **a, int **b, int rows, int cols) 262 { 263 int i, j; 264 265 int **r = (int **)malloc(rows * sizeof(int *)); 266 for (i = 0; i < rows; i++) 267 { 268 r[i] = (int *)malloc(cols * sizeof(int)); 269 for (j = 0; j < cols; j++) 270 { 271 r[i][j] = a[i][j] - b[i][j]; 272 } 273 } 274 display(r, rows, cols); 275 clear(r, rows); 276 } 277 278 void product(int **a, int **b, int rows_a, int cols_b, int common) 279 { 280 int i, x, y, z, temp; 281 282 int **r = (int **)malloc(rows_a * sizeof(int *)); 283 for (i = 0; i < rows_a; i++) 284 { 285 r[i] = (int *)malloc(cols_b * sizeof(int)); 286 } 287 288 for (x = 0; x < rows_a; x++) 289 { 290 for (y = 0; y < cols_b; y++) 291 { 292 temp = 0; 293 for (z = 0; z < common; z++) 294 { 295 temp += a[x][z] * b[z][y]; 296 } 297 r[x][y] = temp; 298 } 299 } 300 display(r, rows_a, cols_b); 301 clear(r, rows_a); 302 } 303 304 void transpose(int **matrix, int rows, int cols) 305 { 306 int i, j; 307 308 int **r = (int **)malloc(cols * sizeof(int *)); 309 for (i = 0; i < cols; i++) 310 { 311 r[i] = (int *)malloc(rows * sizeof(int)); 312 for (j = 0; j < rows; j++) 313 { 314 r[i][j] = matrix[j][i]; 315 } 316 } 317 display(r, cols, rows); 318 clear(r, cols); 319 }