Matrix Multiplication

Matrix is 2D array in computer science. Take user input for row and colum for matrix A and matrix B. Column count of matrix A should match to row count of matrix B else we should return. Allocate two 2D dynamic array A and B, first allocate pointes for row elemnets and then allocate columns elements and assign each column pointers to row list. Now populate elements for matrix A with user input. Then do the same for matrix B. Now take element[0,0] from A and B and multiply row elements of A to column elements of B and sum up. This sum is the resultant element for position [0,0]. Repeat the same for rest of all positions individually. Thus the resultant matrix is formed.

Source Code

#include <stdio.h>
#include <malloc.h>
int main(int argc, char* argv[])
{
  int rows_a = 0, rows_b = 0;
  int cols_a = 0, cols_b = 0;
  int **matrix_a;
  int **matrix_b;
  int r, c, k, sum;
10    printf ("== Matrix Multiplication Demo ==\n");
11    printf ("Matrix A has rows : ");
12    scanf ("%d", &rows_a);
13    printf ("Matrix A has columns : ");
14    scanf ("%d", &cols_a);
15    printf ("Matrix B has rows : ");
16    scanf ("%d", &rows_b);
17    printf ("Matrix B has columns : ");
18    scanf ("%d", &cols_b);
19 
20    if (cols_a != rows_b) {
21      printf ("Cannot multiply these two matrixes!");
22      return -1;
23    }
24 
25    matrix_a = (int **) malloc (sizeof(int **)*rows_a);
26    for (= 0; r < rows_a; r++) {
27      matrix_a[r] = (int *) malloc(sizeof(int)*cols_a);
28    }
29    matrix_b = (int **) malloc (sizeof(int **)*rows_b);
30    for (= 0; r < rows_b; r++) {
31      matrix_b[r] = (int *) malloc(sizeof(int)*cols_b);
32    }
33 
34    printf ("\nMatrix A\n");
35    for (= 0; r < rows_a; r++) {
36      for (= 0; c < cols_a; c++) {
37        printf ("Element [%d][%d] : ", r, c);
38        scanf ("%d", &matrix_a[r][c]);
39      }
40    }
41    printf ("\nMatrix B\n");
42    for (= 0; r < rows_b; r++) {
43      for (= 0; c < cols_b; c++) {
44        printf ("Element [%d][%d] : ", r, c);
45        scanf ("%d", &matrix_b[r][c]);
46      }
47    }
48    printf ("\nMatrix A\n");
49    for (= 0; r < rows_a; r++) {
50      printf ("| ");
51      for (= 0; c < cols_a; c++) {
52        printf ("%.2d ", matrix_a[r][c]);
53      }
54      printf (" |");
55      printf ("\n");
56    }
57    printf ("\nMatrix B\n");
58    for (= 0; r < rows_b; r++) {
59      printf ("| ");
60      for (= 0; c < cols_b; c++) {
61        printf ("%.2d ", matrix_b[r][c]);
62      }
63      printf (" |");
64      printf ("\n");
65    }
66    printf ("\nMatrix A x B\n");
67    for (= 0; r < rows_a; r++) {
68      printf ("| ");
69      for (= 0; c < cols_b; c++) {
70        sum = 0;
71        for(= 0; k < cols_a; k++) {
72          sum = sum + matrix_a[r][k] * matrix_b[k][c];
73        }
74        printf ("%.2d ", sum);
75      }
76      printf (" |");
77      printf ("\n");
78    }
79    return 0;
80  }

Output

== Matrix Multiplication Demo ==
Matrix A has rows : 2
Matrix A has columns : 2
Matrix B has rows : 2
Matrix B has columns : 3

Matrix A
Element [0][0] : 1
Element [0][1] : 2
Element [1][0] : 3
Element [1][1] : 4

Matrix B
Element [0][0] : 5
Element [0][1] : 6
Element [0][2] : 7
Element [1][0] : 8
Element [1][1] : 9
Element [1][2] : 10

Matrix A
| 01 02  |
| 03 04  |

Matrix B
| 05 06 07  |
| 08 09 10  |

Matrix A x B
| 21 24 27  |
| 47 54 61  |

You have viewed 1 page out of 248. Your C learning is 0.00% complete. Login to check your learning progress.

 Vote 0

Similar topics related to this section

array, multidimensional arrays, 2D and 3D dynamic array, add Matrix, multiply Matrix, adjacency Matrix, Circular buffer,

# C Programming Language (Prentice Hall Software)
# Let Us C Paperback - 2006 by Yashavant Kanetkar
# Understanding and Using C Pointers Core techniques for memory management
# Data Structures Using C and C++ Paperback - 1998
# Data Structures In C Paperback - August 11, 2008 by Noel Kalicharan