top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

Matrix multiplication using C/C++?

+3 votes
714 views

Say we have two matrix of m*n and n*t.
Any sample code in C/C++ along with the algorithm would be helpful.

posted Mar 4, 2015 by Kapil Kapoor

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

1 Answer

+3 votes

Here the program for that (Tested). It will support to a max of 5x5 matric.
Before doing the multiplication you have to check i.e if your matric is of MxN and PxQ then if and only if N == P then you can perform the multiplication. And the resulted matric output will be of form MxQ.
And if you want to learn how the multiplication happen just go through this site (click on the below link).
Matrix Multiplication

#include <stdio.h>

void get_matrix_input(int (*matrix)[5], int row, int col)
{
    int i, j;
    printf("Enter the elemet of the matrxi(%d, %d)\n", row, col);

    for (i = 0; i < row; i++)
        for (j = 0; j < col; j++)
            scanf("%d", &matrix[i][j]);
}

int main()
{
    int row1, row2, col1, col2;
    int mat_a[5][5], mat_b[5][5], res[5][5];
    int i, j, k, sum = 0;

    printf("Enter the number row and col for matric A : ");
    scanf("%d%d", &row1, &col1);

    get_matrix_input(mat_a, row1, col1);

    printf("Enter the number row and col for matric B : ");
    scanf("%d%d", &row2, &col2);

    if (col1 != row2) {
        printf("Matrices with entered orders can't be multiplied with each other.\n");
    } else {
        get_matrix_input(mat_b, row2, col2);

        for (i = 0; i < row1; i++) {
            for (j = 0; j < col2; j++) {
                for (k = 0; k < row2; k++) {
                    sum += mat_a[i][k] * mat_b[k][j];
                }
                res[i][j] = sum;
                sum = 0;
            }
        }
    }

    printf("Product of entered matrices :\n");
    for (i = 0; i < row1; i++) {
        for (j = 0; j < col2; j++)
            printf("%d\t", res[i][j]);

        printf("\n");
    }
    return 0;
}

Sample input and output:

Enter the number row and col for matric A : 2 3
Enter the elemet of the matrxi(2, 3)
1 2 3
4 5 6
Enter the number row and col for matric B : 3 2
Enter the elemet of the matrxi(3, 2)
7 8
9 10
11 12
Product of entered matrices :
58  64  
139 154
answer Mar 4, 2015 by Arshad Khan
Thanks, it helped, actually I need almost all matrix operation may be will post the queries in next few days :)
Your are welcome :)
Similar Questions
0 votes
for(i=0;i<n;i++)
  for(j=0;j<n;j++)
     for(k=0;k<n;k++)
        C[i][j]+=A[i][k]*B[k][j];

In this algorithm, there are 6 combinations of loops : the one given above is ijk. The others are ikj,jki,jik,kij and kji. Which one executes the fastest and why?

+2 votes

Does anyone know how to rotate a 2d matrix circularly for n times in suppose C language...? It would be a lot of help if you could explain with code.

Hint : Each time each row vector needs to be rotated one element to the right relative to the preceding row vector.

0 votes

suppose

A(n,m) = 
1 2 3         
4 5 6                            
7 8 9

and 

B(p, q) = 
1 1
1 1

What is best method to find min of square of difference of sub-matrices of A and B e.g.

sub-matrices of A =

1 2    |     2 3   |    4 5    |   5 6
3 4    |     5 6   |    7 8    |   8 9

Difference of first sub-matrix of A with B =

(1-1)  (2-1)    = |     0 1
(3-1)  (4-1)      |     2 3

sum of square of elements = 0*0 + 1*1 + 2*2 + 3*3 = 14

similar steps for other sub-matrices of A

Please suggest looking for an alternate method or algorithm which has time complexity less than O(n*m*p*q)

...