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;
  printf ("== Matrix Multiplication Demo ==\n");
  printf ("Matrix A has rows : ");
  scanf ("%d", &rows_a);
  printf ("Matrix A has columns : ");
  scanf ("%d", &cols_a);
  printf ("Matrix B has rows : ");
  scanf ("%d", &rows_b);
  printf ("Matrix B has columns : ");
  scanf ("%d", &cols_b);

  if (cols_a != rows_b) {
    printf ("Cannot multiply these two matrixes!");
    return -1;
  }

  matrix_a = (int **) malloc (sizeof(int **)*rows_a);
  for (= 0; r < rows_a; r++) {
    matrix_a[r] = (int *) malloc(sizeof(int)*cols_a);
  }
  matrix_b = (int **) malloc (sizeof(int **)*rows_b);
  for (= 0; r < rows_b; r++) {
    matrix_b[r] = (int *) malloc(sizeof(int)*cols_b);
  }

  printf ("\nMatrix A\n");
  for (= 0; r < rows_a; r++) {
    for (= 0; c < cols_a; c++) {
      printf ("Element [%d][%d] : ", r, c);
      scanf ("%d", &matrix_a[r][c]);
    }
  }
  printf ("\nMatrix B\n");
  for (= 0; r < rows_b; r++) {
    for (= 0; c < cols_b; c++) {
      printf ("Element [%d][%d] : ", r, c);
      scanf ("%d", &matrix_b[r][c]);
    }
  }
  printf ("\nMatrix A\n");
  for (= 0; r < rows_a; r++) {
    printf ("| ");
    for (= 0; c < cols_a; c++) {
      printf ("%.2d ", matrix_a[r][c]);
    }
    printf (" |");
    printf ("\n");
  }
  printf ("\nMatrix B\n");
  for (= 0; r < rows_b; r++) {
    printf ("| ");
    for (= 0; c < cols_b; c++) {
      printf ("%.2d ", matrix_b[r][c]);
    }
    printf (" |");
    printf ("\n");
  }
  printf ("\nMatrix A x B\n");
  for (= 0; r < rows_a; r++) {
    printf ("| ");
    for (= 0; c < cols_b; c++) {
      sum = 0;
      for(= 0; k < cols_a; k++) {
        sum = sum + matrix_a[r][k] * matrix_b[k][c];
      }
      printf ("%.2d ", sum);
    }
    printf (" |");
    printf ("\n");
  }
  return 0;
}

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  |

About our authors: Team EQA

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

#