Matrix Multiplication in C

I am trying to solve the matrix multiplication problem with C. The matrix sizes given in the (2x2) task. I wrote this code, but it does not print the result as I expect. I think I'm missing a point in the C rules.

What is my error in this code?

#include <stdio.h>
int main() {
    int matA[2][2]={0,1,2,3};
    int matB[2][2]={0,1,2,3};
    int matC[2][2];
    int i, j, k;
    for (i = 0; i < 2; i++) {
        for(j = 0; j < 2; j++) {
            for(k = 0; k < 2; k++) {
                matC[i][j] += matA[i][k] * matB[k][j];
            }
            printf("%d\n",matC[i][j]);
        } 
    }
}

Print result:

2 
3 
4195350
11
+5
source share
8 answers

The problem is that in the line

matC[i][j] += matA[i][k] * matB[k][j];

you add things to matC, but when you create it you do not initialize it, so it has garbage.

You can do something like:

int matC[2][2] = {0}which will initialize the entire matrix with 0

+5
source

Here is the matrix multiplication code I'm using:

for(i=0;i<M;i++){
    for(j=0;j<K;j++){
        matC[i][j]=0;
        for(k=0;k<N;k++){
            matC[i][j]+=matA[i][k]*matB[k][j];
        }
    }
}

the big thing is setting the response matrix to zero (as the others said without code).

+8
source

matC . . .

+4

:

#include<stdio.h>
void main()
{
    int r1, c1, r2, c2;

    printf("Enter number of rows and columns for matrix A : ");
    scanf("%d %d",&r1,&c1);

    printf("Enter number of rows and columns for matrix B : ");
    scanf("%d %d",&r2,&c2);

    int a[r1][c1], b[r2][c2], ab[r1][c2], ba[r2][c1],i,j,k,temp;

    if(c1==r2 && r1==c2)
    {
        printf("\nEnter element in matrix A : ");
        for(i=0;i<r1;i++)
        {
            for(j=0;j<c1;j++)
            {
                printf("\n Enter element : ");
                scanf("%d",&a[i][j]);
            }
        }
        printf("\nEnter element in B : ");
        for(i=0;i<r2;i++)
        {
            for(j=0;j<c2;j++)
            {
                printf("\nEnter element : ");
                scanf("%d",&b[i][j]);
            }
        }
        for(i=0;i<r1;i++)
        {
            for(j=0;j<c2;j++)
            {
                temp=0;
                for(k=0;k<r2;k++)
                {
                    temp+=a[i][k]*b[j][k];
                }
                ab[i][j]=temp;
            }
        }
        for(i=0;i<r2;i++)
        {
            for(j=0;j<c1;j++)
            {
                temp=0;
                for(k=0;k<r1;k++)
                {
                    temp+=b[i][k]*a[k][j];
                }
                ba[i][j]=temp;
            }
        }
        printf("\nMatrix A : ");
        for(i=0;i<r1;i++)
        {
            printf("\n\t");
            for(j=0;j<c1;j++)
            {
                printf("%d",a[i][j]);
            }
            printf("\n");
        }
        printf("\nMatrix B : ");
        for(i=0;i<r2;i++)
        {
            printf("\n\t");
            for(j=0;j<c2;j++)
            {
                printf("%d",b[i][j]);
            }
        }
        printf("\nMatrix multiplication of A*B : ");
        for(i=0;i<r1;i++)
        {
            printf("\n\t");
            for(j=0;j<c2;j++)
            {
                printf("\t%d",ab[i][j]);
            }
            printf("\n");
        }
        printf("\nMatrix multiplication of B*A : ");
        for(i=0;i<r2;i++)
        {
            printf("\n\t");
            for(j=0;j<c1;j++)
            {
                printf("\t%d",ba[i][j]);
            }
            printf("\n");
        }
    }
    else
        printf("\nMatrix Multiplication is not possible...!!!");
}
+2

C .

+1

matC .

0
0

You may want to dynamically allocate memory for the resulting matrix. If so, use calloc()to highlight and clean items. printMatrix()called to print the result, but not defined here.

/* matrix1: [rows1 x cols1];  matrix2: [rows2 x cols2]; product is
matrix3: [rows1 x cols2] if (cols1 == rows2) is true.  calloc to 
allocate / clear memory for matrix3.  Algorithm is O(n^3) */

float ** matrix3;
if (cols1 == rows2) {  // product matrix can be calculated
    // calloc product matrix3
    matrix3 = (float **)calloc(rows1, sizeof(float *));
    for (int i = 0; i < rows1; i++)
        matrix3[i] = (float *)calloc(cols2, sizeof(float));

    int i, j, k;
    float tmp;
    for (i = 0; i < rows1; i++) {
        for (j = 0; j < cols2; j++) {
            tmp = 0.0;
            for (k = 0; k < rows2; k++)
                tmp += matrix1[i][k] * matrix2[k][j];
            matrix3[i][j] = tmp;
        }
    }
    printMatrix(matrix3, rows1, cols2, 3);
    free(matrix3);
} else {   // cols1 != rows2
    puts("dimensional mismatch; can't multiply matrices");
}
0
source

All Articles