Saturday, 28 January 2017

Matrix multiplication program in C using arrays ~ mkniit ~ gniitsolution

Matrix multiplication program in C is used to perform a multiplication operation. This C program of matrix multiplication is totally depends on an arrays concept. This is based on three dimensional matrix formats. Now the matrix will be multiplied by only the mathematical form. Multiply the matrix in a form of (2×3). Before going to enter this topic, We’ve already published an article about Matrix addition source code in C.


Matrix multiplication code:

Q: Write a c program for Matrix multiplication using arrays concept.

01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
//Header files
#include<stdio.h>
 main()
 {
  //Program variables
  int d[10][10],c[10][10],b[10][10],m,n,i,j,ip,q,p,k;
  clrscr(); //Function to clear previous output
  printf("input row n column of matrix A: "); //Display fnction
  scanf("%d %d", &m,&n);//Getting input function
  printf("Input row and column of B matrix: ");
  scanf("%d %d",&k,&q);
  if(n==k) //Conditional statement
   {
    printf("matrices can be multiplied\n");
    printf("resultant matrix is %d X %d\n",m,q);
    printf("input A-matrix\n");
    read_mat(d,m,n);
    printf("input B-matrix\n");
    read_mat(b,k,q);
    for(i=0;i<m;++i) //Looping statement(nested for loop)
     for(j=0;j<q;++j)
     {
          c[i][j]=0;
          for(ip=0;ip<n;++ip)
          c[i][j]=c[i][j] + d[i][ip] * b[ip][j];
      }
      printf(" Resultant of A and B matrices :\n");
      write_mat(c,m,q);
      }
      else
       {
    printf(" Col of matrix a must be equal to row of matrix B\n");
    printf("matrices cannot be multiplied :\n");
    }
      }
      read_mat(d,m,n)
       int d[10][10],m,n;
     {
      int i,j;
       for(i=0; i<m; i++)
       for(j=0; j<n; j++)
        scanf("%d",&d[i][j]);
      }
      write_mat(d,m,n)
      int d[10][10],m,n;
       {
         int i,j;
         for(i=0; i<m; i++)
          {
        for(j=0; j<n; j++)
         printf("%5d", d[i][j]);
         printf("\n");
     }
 getch();
}

1 comment:

  1. awesome blog and awesome post, c language lover should visit here: kidsfront.com

    ReplyDelete