A Learning Platform

SUCCESS

Success = Discipline + Consistency + Sacrifice.
Discipline - in what you do.
Consistency - in what you do.
Sacrifice - for what you do.

                   -- Manjunatha C. P. --

C Program: Multiplication of Matrix using Function

Matrix Multiplication in C:

We can perform several operations on matrix like addition, subtraction, multiplication, division etc… Matrix are 2 dimensional arrays in C.

To Multiply 2 Matrices: Number of Columns of first matrix = Number of Rows of second matrix.

Output Matrix order: ( Number of Rows of first matrix, Number of Columns of second matrix ).

Example:

a(r1, c1) * b(r2, c2) = multiply(r1, c2).

a(2, 3) * b(3, 4) = multiply(2, 4).

a(3, 3) * b(3, 3) = multiply(3, 3).

Where:

a & b are input matrices.

multiply is output matrix.

Description:

i, j and k : Loop control variables.

r1 & c1 : Rows and columns of first matrix.

r2 & c2 : Rows and columns of second matrix.

a & b : Input matrices.

mul : Multiplied matrix.

read() : Function to read matrix ‘a’ and ‘b’.

display() : Function to display the matrices.

multiply() : Function to multiply 2 matrices.

Input : Number of rows & columns of matrices, elements of 2 matrices.

Output : Multiplication of 2 input matrices.


Algorithm: Multiplication of matrix using Functions.

STEP 1: START

STEP 2: READ number of rows and columns of first matrix

              READ r1, c1

STEP 3: READ number of rows and columns of second matrix

              READ r2, c2

STEP 4: CHECK Number of columns of first matrix ≠ Number of rows of second matrix.

              If c1 ≠ r2

              PRINT “Multiplication not possible”

              EXIT program

STEP 5: CALL read() function to read matrix ‘a’ and ‘b’

              READ the elements of first matrix

              LOOP from i = 0 to r1

               LOOP from j = 0 to c1

                 READ a(i)(j) 

              READ the elements of second matrix

              LOOP from i = 0 to r2

               LOOP from j = 0 to c2

                READ b(i)(j) 

STEP 6: CALL the multiply() function

              LOOP from i = 0 to r1

               LOOP from j = 0 to c2

                LOOP from k = 0 to c1

                  mul(i)(j) =mul(i)(j) + a(i)(k) * b(k)(j);                       

STEP 7: CALL  the display() function to print multiplication matrix

              LOOP from i = 0 to r1

               LOOP from j = 0 to c2

                PRINT mul(i)(j) 

STEP 8: END


Program: Multiplication of matrix using Functions in C


#include<stdio.h>

#include<conio.h>

void read(int a[][10], int b[][10], int r1, int c1, int r2, int c2);

void multiply(int a[][10], int b[][10], int mul[][10], int r1, int c1, int r2, int c2);

void display(int mul[][10], int r1, int c2);

int main()

{

  int a[10][10], b[10][10], mul[10][10], r1, c1, r2, c2;

  printf("Enter rows and columns of first matrix: ");

  scanf("%d %d", &r1, &c1);

  printf("Enter rows and columns of second matrix: ");

  scanf("%d %d", &r2, &c2);

    if(c1 != r2)

    {

        printf("Multiplication not possible");

        getch();

        return 0;

    }

  read(a, b, r1, c1, r2, c2);

    multiply(a, b, mul, r1, c1, r2, c2);

    display(mul, r1, c2);

    getch();

  return 0;

}

void read(int a[][10], int b[][10], int r1, int c1, int r2, int c2)

{

  int i, j;

  printf("Enter elements of first matrix:\n");

  for(i = 0; i < r1; ++i)

    for(j = 0; j < c1; ++j)

      scanf("%d", &a[i][j]);

  printf("\nEnter elements of second matrix:\n");

  for(i = 0; i < r2; ++i)

    for(j = 0; j < c2; ++j)

        scanf("%d", &b[i][j]);

}

void multiply(int a[][10], int b[][10], int mul[][10], int r1, int c1, int r2, int c2)

{

  int i, j, k;

  for(i = 0; i < r1; ++i)

    for(j = 0; j < c2; ++j)

    {

            mul[i][j]=0;

      for(k=0; k<c1; ++k)

        mul[i][j] =mul[i][j] + a[i][k] * b[k][j];

    }

}

void display(int mul[][10], int r1, int c2)

{

  int i, j;

  printf("Output Matrix:\n");

  for(i = 0; i < r1; ++i)

  {

    for(j = 0; j < c2; ++j)

      printf("%d\t", mul[i][j]);

    printf("\n");

  }

}


Output 1:


Enter rows and columns of first matrix: 2 3

Enter rows and columns of second matrix: 3 4

Enter elements of first matrix:

1 2 3

9 5 7

Enter elements of second matrix:

9 5 6 6

1 5 2 4

5 6 3 7

Output Matrix:

26 33 19 35

121 112 85 123

Output 2:

Enter rows and columns of first matrix: 2 3

Enter rows and columns of second matrix: 2 2

Multiplication not possible


NOTE : Square matrices of same order can always be multiplied and the order of output matrix will also be a square matrix.

QUIZ:

1] Two matrices can be multiplied only if.

  • Rows of 2 matrix are equal.
  • Columns of 2 matrix are equal.
  • Number of columns of first matrix = Number of rows of second matrix.
  • Number of rows of first matrix = Number of columns of second matrix.