0

I'm trying to write a code for Matrix Multiplication. I don't know what I am doing wrong in this code as the compiler is not giving an error, but the program is still not working. I've attached the photos of output, the code is running perfectly for a 2x2 matrix but other than that it's not working correctly.

#include <stdio.h>
#include <stdlib.h>
int main()
{
int a[20][20], b[20][20], mul[20][20];
int m1, n1, m2, n2;
int i1, j1, i2, j2, i3, j3, k;
int sum = 0;
printf("Enter the number of Rows and Column for Matrix A: ");
scanf("%d %d", &m1, &n1);
printf("Enter the number of Rows and Column for Matrix B: ");
scanf("%d %d", &m2, &n2);

if (n1 == m2)
{
    printf("Insert elements in matrix A-\n");
    
    for (i1 = 1; i1 <= n1; i1++)
    {
        for (int j1 = 1; j1 <= m1; j1++)
        {
            printf("Enter A[%d][%d]: ", i1, j1);
            scanf("%d", &a[i1][j1]);
        }
    }
    
    printf("\n");
    
    printf("Insert elements in matrix B-\n");
    
    for (i2 = 1; i2 <= n2; i2++)
    {
        for (j2 = 1; j2 <= m2; j2++)
        {
            printf("Enter B[%d][%d]: ", i2, j2);
            scanf("%d", &b[i2][j2]);
        }
    }

    printf("\n");

    printf("Matrix A-\n");
    
    for (int i = 1; i <= n1; i++)
    {
        for (int j = 1; j <= m1; j++)
        {
            printf("%d ", a[i][j]);
        }
        printf("\n");
    }

    printf("Matrix B-\n");
    
    for (int i = 1; i <= n2; i++)
    {
        for (int j = 1; j <= m2; j++)
        {
            printf("%d ", b[i][j]);
        }
        printf("\n");
    }

    for (i3 = 1; i3 <= m1; i3++)
    {
        for (j3 = 1; j3 <= n2; j3++)
        {
            for (k = 1; k <= m2; k++)
            {
                sum = sum + a[i3][k] * b[k][j3];
            }
            mul[i3][j3] = sum;
            sum = 0;
        }
    }

    printf("\nThe Multiplication Matrix of the two entered Matrices is:\n");
    
    for (int i = 1; i <= m1; i++)
    {
        for (int j = 1; j <= n2; j++)
        {
            printf("%d ", mul[i][j]);
        }
        printf("\n");
    }
}
else
{
    printf("The given information is not valid.\nFor the multiplication of two matrices (AxB), the no. of Columns of A should be equal to the no. of Rows of B.\nCheck the given Data.\n");
    printf("Your given Data:-\nRow & Column of A = %d, %d\nRow & Column of B = %d, %d\n", m1, n1, m2, n2);
}
return 0;
}

For 2x2 Matrices

For 2X3 & 3x2 Matrices

For 1x3 & 3x4 Matrices

  • 1
    Do your really intend the loops (and the array indexing) to start from `1`? – Weather Vane Nov 11 '21 at 18:40
  • in C, arrays are indexed from [0, numElements-1]. I don't think that's necessarily the problem here because it looks like you've reserved enough initial space, but you might as well get in the habit of looping `for (int i=0; i – yano Nov 11 '21 at 18:40
  • Not sure why, but if I initialise the arrays to all `0` then for the third example `1 3`, `3 4` the output is `1 1 1 0` instead of absurd values. – Weather Vane Nov 11 '21 at 18:48
  • ... when I initialise the two source arrays to `0` but not the destination array `mul[][]` I get the same result. This suggests that your array indexing is off, and you aren't multiplying the array elements that you wanted. – Weather Vane Nov 11 '21 at 18:51
  • 1
    You have subscripts mixed up. For `A`, you take two dimensions, `m1` and `n1`, in that order. Then, in the loop reading A, you use `a[i1][j1]`, where `i1` iterates through `n1` and `j1` iterates through `m1`. So the first subscript corresponds to `n1` and the second to `m1`. In the multiplication loop, you have `sum + a[i3][k] * b[k][j3]`, where `i3` iterates through `m1`. So this uses `m1` in a different dimension than originally. – Eric Postpischil Nov 11 '21 at 18:56
  • ...and `k` iterates to `m2` which is incorrect for `a[i3][k]`. – Weather Vane Nov 11 '21 at 18:58
  • Aside: it is better to set `sum = 0;` just *once*, right before you begin to accumulate values. Instead of initialising it at the start of execution, and again *after* using it. – Weather Vane Nov 11 '21 at 19:02
  • @WeatherVane You were right the multiplication between the elements was not what I wanted because when I was initialising the for loop, the column loop is first where the loop for row should run first. That was the mistake. – Darshika Jadhav Nov 11 '21 at 19:34

0 Answers0