8. C PROGRAMMING LAB | Check Now

C PROGRAMMING LAB –08] Develop a program to introduce 2D array manipulation and implement matrix multiplication and ensure the rules of multiplication are checked.

Algorithm

• Step-1: Start
• Step-2: [Read the order of both matrices M, N, P, Q]
• Step-3: Check for i = 0 to M and j = 0 to N for the matrix A, read A[i][j]
• Step-4: Check for i = 0 to P and j = 0 to Q for the matrix B, read B[i][j]
• Step-5: If in case (N=P) then only multiplication is possible then goto step 6 otherwise goto step 7
• Step-6: Initially set matrix C[i][j] as 0
• For k=0 to n
• C[i][j] = C[i][j] + A[i][k]*B[i][k] than goto step 8
• Step-7: Multiplication is not possible
• Step-8: Prints the multiplication of the two matrix
• Step-9: Stop
See also  4. VTU WEB TECHNOLOGY LAB | READ NOW

Program -8 source code

```#include<stdio.h>

int main()
{
int a[20][20],b[20][20],c[20][20];
int m,n,p,q,i,j,k;

printf("Enter rows and columns of matrix A\n");
scanf("%d%d",&m,&n);
printf("Enter rows and columns of matrix B\n");
scanf("%d%d",&p,&q);

if(n!=p)
{
printf("Matrix multiplication not possible\n");
return 0;
}

printf("Enter elements of matrix A\n");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
scanf("%d",&a[i][j]);
}
}

printf("Enter elements of matrix B\n");
for(i=0;i<p;i++)
{
for(j=0;j<q;j++)
{
scanf("%d",&b[i][j]);
}
}

for(i=0;i<p;i++)
{
for(j=0;j<q;j++)
{
c[i][j]=0;
for(k=0;k<n;k++)
{
c[i][j]=c[i][j]+a[i][k]*b[k][j];
}
}
}

printf("Product of two matrices is\n");
for(i=0;i<m;i++)
{
for(j=0;j<q;j++)
{
printf("%d\n",c[i][j]);
}
}

}```

C PROGRAMMING -Output

Enter rows and columns of matrix A

2 2

Enter rows and columns of matrix B

2 2

Enter the elements of matrix A

1 2

3 4

Enter the elements of matrix B

4 3

2 1

The product of two matrices is

8

5

20

13

See also  2. C PROGRAMMING LAB | Check Now

C PROGRAMMING -Viva Questions

1] How to initialize two-dimensional arrays?

2] How to pass a two-dimensional array as a function parameter?

3] How the memory is allocated for a two-dimensional array

4] Write the program to add and subtract two matrix

5] Program to find the transpose of a matrix

6] Program to find determinants of a matrix

7] Program to find the diagonal elements of a matrix

Join For Job Alerts