CodeClerks

Matrix multiplication in C Program



Write the reason you're deleting this FAQ

Matrix multiplication in C Program

Hello guys
I am a student of Computer Technology. I am studying C/C++ and very soon will start Java. Its really hard to study three language at a time. But there is no way to avoid this ! Because I have to study C/C++ and Java at a time to cut a good figure at exam. Beside its really fun to learn programming ! I always enjoy programming. Matrix multiplication in C Program
I have just come here at CodeClerks forum and thought that I can learn new programming logic here and also can share some of my programs. I hope you guys will also share your programming knowledge with me. ;)

Recently I have just wrote a program for Multiplying two Matrix. I believe that we can solve a problem with several algorithms. So I thought I should share my code here and hope you will also share your code with me. Then we will be able to learn different algorithms Matrix multiplication in C Program

#include <stdio.h>

int main()

{

  int m, n, p, q, c, d, k, sum = 0;

  int first_matrix[10][10], second_matrix[10][10], multiply[10][10];

  printf("Enter the number of rows and columns of first matrix\n");

  scanf("%d%d", &m, &n);

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

  for (c = 0; c < m; c++)

    for (d = 0; d < n; d++)

      scanf("%d", &first_matrix[c][d]);

  printf("Enter the number of rows and columns of second matrix\n");

  scanf("%d%d", &p, &q);

  if (n != p)

    printf("Matrices with entered orders can't be multiplied with each other.\n");

  else

  {

    printf("Enter the elements of second matrix\n");

    for (c = 0; c < p; c++)

      for (d = 0; d < q; d++)

        scanf("%d", &second_matrix[c][d]);

    for (c = 0; c < m; c++) {

      for (d = 0; d < q; d++) {

        for (k = 0; k < p; k++) {

          sum = sum + first_matrix[c][k]*second_matrix[k][d];

        }

        multiply[c][d] = sum;

        sum = 0;

      }

    }

    printf("Product of entered matrices:-\n");

    for (c = 0; c < m; c++) {

      for (d = 0; d < q; d++)

        printf("%d\t", multiply[c][d]);

      printf("\n");

    }

  }

  return 0;

}
 



Now, tell me about your algorithm of matrix multiply. Can we do this with one dimensional/linear array ??

Comments

Please login or sign up to leave a comment

Join