bsc

Comprehensive codebase and cou...
Log | Files | Refs | README | LICENSE

assignment-s-20.c (7820B)


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