Home > Software engineering >  Matrix multiplication by loops
Matrix multiplication by loops

Time:11-21

This is the template and initialization of variables.

#include <stdio.h>
int main()
{
    int k=0,m=0,l=0,s=0; 
    int a[k][m],a1[m][l],c[k][l];

Inputting the matrix dimensions and entering the elements individually

//Inputting the matrices...

printf("Enter the dimension of first matrix (row and column)>> \n");
scanf("%d%d",&k,&m);
printf("Enter the dimension of second matrix (row and column)>> \n");
scanf("%d%d",&k,&m);
printf("First matrix elements are inputted from below >> \n");
for (int i = 0; i < k ; i  )
{
    for (int j = 0; j < m; j  )
    {
        printf("a[%d][%d]>> ",i,j);
        scanf("%d",&a[i][j]);
    }
    printf("\n");
}

Inputting the second matrix

printf("Second matrix elements are inputted from below >> \n");
for (int i = 0; i < m ; i  )
{
    for (int j = 0; j < l; j  )
    {
        printf("a[%d][%d]>> ",i,j);
        scanf("%d",&a[i][j]);
    }
    printf("\n");
}

Here's the logic for matrix multiplication

//Logic for the matrix multiplication

for (int i = 0; i < k; i  )
{
    for (int j = 0; j < l; j  )
    {
        s=0;
        for (int jj = 0; jj < m; jj  )
        {
            s =a[i][jj]*a1[jj][j];
        }
        c[i][j]=s;
    }
}

Printing the multiplied matrix

//for outputting the matrix
                                                
for (int i = 0; i < k ; i  )
{
    for (int j = 0; j < l; j  )
    {
        printf("a[%d][%d] = %d",k,l,c[k][l]);
        
    }
    printf("\n");
}
return 0;

Respected experienced coders please help me out in this one. I am stuck for a while now.

I inputted two matrices of order [k][m] and [m][l]. I then multiplied them by this logic.

And then I simply printed the final matrix c[k][l] by some nested loops.

Please kindly help me.

The output of the following code in an online compiler Enter the dimension of first matrix (row and column)>>

2 2

Enter the dimension of second matrix (row and column)>>

2 2

First matrix elements are inputted from below >>

a[0][0]>> 2

a0>> 2

a[1][0]>> 2

a1>> 2

Second matrix elements are inputted from below >>

onlinegdb compiler

CodePudding user response:

Hey, I have modified your code. Hope this will help you.

#include <stdio.h>
int main()
{
    int k, m, l, x;

    //Entering matrix dimensions
    printf("Enter the dimension of first matrix (row and column)>> \n");
    scanf("%d%d", &k, &m);
    printf("Enter the dimension of second matrix (row and column)>> \n");
    scanf("%d%d", &m, &l);
    printf("First matrix elements are inputted from below >> \n");

    int a[k][m];
    //Entering matrix elements
    for (int i = 0; i < k; i  )
    { 
       for (int j = 0; j < m; j  )
       {
            printf("a[%d][%d]>> ", i, j);
            scanf("%d", &a[i][j]);
        }
        printf("\n");
    }
    int a1[m][l];
    printf("Second matrix elements are inputted from below >> \n");
    for (int i = 0; i < m; i  )
    {
        for (int j = 0; j < l; j  )
        {
            printf("a1[%d][%d]>> ", i, j);
            scanf("%d", &a1[i][j]);
        }
        printf("\n");
    }

    //Matrix multiplication logic
    int c[k][l];
    for (int i = 0; i < k; i  )
    {
        for (int j = 0; j < m; j  )
        {
            c[i][j] = 0;
            for (int x = 0; x < m; x  )
            {
                c[i][j]  = a[i][x] * a1[x][j];
            }
        }
    }
    //Displaying product matrix
    for (int i = 0; i < k; i  )
    {
        for (int j = 0; j < l; j  )
        {
            printf("\t%d", c[i][j]);
        }
        printf("\n");
    }
    return 0;
}

Tips

  • Declare array for each matrix after the declaration of its row and column variables.
  • c[i][j] = a[i][x] * a1[x][j]; is one major part of the logic of matrix multiplication which you missed out.
  • You entered the matrix dimension variables the same for both matrices.

CodePudding user response:

There are multiple problems:

  • The matrix arrays must be defined after the values of their dimensions have been read. As posted, the behavior is undefined because the dimensions are 0.

  • the dimensions of the second matrix are input with scanf("%d%d", &k, &m); into k and m instead of m and l. The first dimension should actually be either assumed to be the same as the second dimension of the first matrix or checked.

  • the input loop for the second matrix stores the values into a instead of a1: a typical cut and paste bug.

  • The printing statement printf("a[%d][%d] = %d",k,l,c[k][l]); is incorrect: the index variables are i and j, so the statement should be:

      printf("a[%d][%d] = %d\n", i, j, c[i][j]);
    

Here is a modified version:

#include <stdio.h>

int main() {
    int k, m, m1, l; 

    //Inputting the matrices...
    printf("Enter the dimensions of first matrix (rows and columns)>> \n");
    if (scanf("%d%d", &k, &m) != 2)
        return 1;
    printf("Enter the dimensions of second matrix (rows and columns)>> \n");
    if (scanf("%d%d", &m1, &l) != 2)
        return 1;

    if (k <= 0 || m <= 0 || m != m1 || l <= 0) {
        printf("incompatible dimensions\n";
        return 1;
    }
    int a[k][m], a1[m][l], c[k][l];

    printf("First matrix elements are inputted from below >> \n");
    for (int i = 0; i < k; i  ) {
        for (int j = 0; j < m; j  ) {
            printf("a[%d][%d]>> ", i, j);
            if (scanf("%d", &a[i][j]) != 1)
                return 1;
        }
        printf("\n");
    }

    printf("Second matrix elements are inputted from below >> \n");
    for (int i = 0; i < m; i  ) {
        for (int j = 0; j < l; j  ) {
            printf("a1[%d][%d]>> ", i, j);
            if (scanf("%d", &a1[i][j]) != 1)
                return 1;
        }
        printf("\n");
    }

    //Logic for the matrix multiplication
    for (int i = 0; i < k; i  ) {
        for (int j = 0; j < l; j  ) {
            int s = 0;
            for (int jj = 0; jj < m; jj  ) {
                s  = a[i][jj] * a1[jj][j];
            }
            c[i][j] = s;
        }
    }

    //for outputting the matrix
    for (int i = 0; i < k; i  ) {
        for (int j = 0; j < l; j  ) {
            printf(" %d", c[i][j]);
        }
        printf("\n");
    }
    return 0;
}
  • Related