Write a C program that uses functions to perform the following:

  1. Addition of Two Matrices
  2. Multiplication of Two Matrices

Algorithm:

Step 1: Start
Step 2: Declare i, j, k, A[3][3], B[3][2], C[3][2] as integers
Step 3: Initialize i = 0, j = 0
Step 4: Till i < 3 execute step 5 else goto step 9
Step 5: Till j < 3 execute steps 6 to 7 else goto step 8
Step 6: Read A[i][j]
Step 7: Increment j by 1 goto step 5
Step 8: Increment i by 1 goto step 4
Step 9: Initialize i = 0, j = 0
Step 10: Till i < 3 execute step 11 else goto step15
Step 11: Till j < 2 execute steps 6 to 7 else goto step 14
Step 12: Read B[i][j]
Step 13: Increment j by 1 goto step 11
Step 14: Increment i by 1 goto step 10
Step 15: Initialize i = 0, j = 0, k = 0
Step 16: Till i < 3 execute step 17 else goto step 24
Step 17: Till j < 2 execute step 18  else goto step 23
Step 18: Initialize C[i][j] = 0
Step 19: Till k<3 execute steps 20 to 21 else goto step
Step 20: calculate C[i][j] = C[i][j] + A[i][k] * B[k][j]
Step 21: Increment k by 1 goto step 19
Step 22: Increment j by 1 goto step 17
Step 23: Increment i by 1 goto step 16
Step 24: Initialize i = 0, j = 0
Step 25: Till i < 3 execute step 26 else goto step 30
Step 26: Till j < 3 execute steps 27 to 28 else goto step 29
Step 27: Print C[i][j]
Step 28: Increment j by1 goto step 26
Step 29: Increment i by 1 goto step 25
Step 30: Stop

Addition of Two Matrices

Program:

#include <stdio.h>
#include <conio.h>
void main()
{
   int a[3][3], b[3][3], c[3][3], i, j;
   clrscr();
   printf("Enter the elements of 3*3 matrix a \n");
   for(i = 0; i < 3; i++)
   {
      for(j = 0; j < 3; j++)
      {
     scanf("%d", &a[i][j]);
      }
   }
   printf("Enter the elements of 3*3 matrix b \n");
   for(i = 0; i < 3; i++)
   {
      for(j = 0; j < 3; j++)
      {
    scanf("%d", &b[i][j]);
      }
   }
   for(i = 0; i < 3; i++)
   {
      for(j = 0; j < 3; j++)
      {
     c[i][j] = a[i][j] + b[i][j];
      }
   }
   printf("The resultant 3*3 matrix c is \n");
   for(i = 0; i < 3; i++)
   {
      for(j = 0; j < 3; j++)
      {
     printf("%d\t", c[i][j]);
      }
      printf("\n");
   }
   getch();
}

Input & Output:

Enter the elements of 3*3 matrix  a
1 2 3 4 5 6 7 8 9
Enter the elements of 3*3 matrix  b
1 2 3 4 5 6 7 8 9
The resultant 3*3 matrix  c is
2      4      6
8     10     12
14    16     18

Multiplication of Two Matrices

Program:

#include<stdio.h>
#include<conio.h>
void main()
{
   int a[3][3], b[3][3], c[3][3], i, j, k;
   clrscr();
   printf("Enter the elements of 3*3 matrix a \n");
   for(i = 0; i < 3; i++)
   {
      for(j = 0; j < 3; j++)
      {
     scanf("%d", &a[i][j]);
      }
   }
   printf("Enter the elements of 3*3 matrix b \n");
   for(i = 0; i < 3; i++)
   {
      for(j = 0; j < 3; j++)
      {
    scanf("%d", &b[i][j]);
      }
   }
   for(i = 0; i < 3; i++)
   {
      for(j = 0; j < 3; j++)
      {
     c[i][j] = 0
     for(k = 0; k < 3; k++)
     {
        c[i][j] = c[i][j] + (a[i][k] * b[k][j])
     }
      }
   }
   printf("The resultant 3*3 matrix c is \n");
   for(i = 0; i < 3; i++)
   {
      for(j = 0; j < 3; j++)
      {
     printf("%d\t", c[i][j]);
      }
      printf("\n");
   }
   getch();
}

Input & Output:

Enter the elements of 3*3 matrix  a
1 2 3 4 5 6 7 8 9
Enter the elements of 3*3 matrix  b
1 2 3 4 5 6 7 8 9
The resultant 3*3 matrix  c is
30    36      42
55    81      96
102   126     150