Nacho
Nacho

Reputation: 21

What's wrong with this code for multiplying two matrices in C?

The aim of the matrixMultiplier function is to multiply A * B, and then show the resulting matrix C, for any two square matrices A and B. A and B sizes are limited by 10*10.

When the user is asked the size of the matrix they wish to introduce, although A and B are 10*10, the function only works with the submatrices in A and B up to the dimension which the user has specified, say 3*3.

I've tested this out multiplying the 3*3 identity matrix by itself and it's not giving me the correct answer.

#include <stdio.h>

void matrixMultiplier(int A[][10], int B[][10], int C[][10], int n) {
    int i, j, k;
    for (i = 0; i < n; i++) {
        for (k = 0; k < n; k++) {
            C[i][k] = 0;               /* Initialize output matrix to zero */
        }
    }

    for (i = 0; i < n; i++) {
        for (k = 0; k < n; k++) {
            for (j = 0; j < n; j++) {
                C[i][k] += A[i][j] * B[j][k];
            }
        }
    }
    printf("\n");

    for (i = 0; i < n; i++) {
        for (k = 0; k < n; k++) {
            printf("%4d", C[i][k]);
        }
        printf("\n");
    }
}
     
int main(void) {
    int A[][10] = {{0}};
    int B[][10] = {{0}};
    int C[][10] = {{0}}; /* Initialize output matrix to zero */
    int i, j;
    int n;

    printf("Enter square matrix dimension: ");
    scanf("%d", &n);
    for (i = 0; i < n; i++) {
        for (j = 0; j < n; j++) {
            printf("Assign a value: ");
            scanf("%d", &A[i][j]);
        }
        printf("New row.\n");
    }
    for (i = 0; i < n; i++) {
        for (j = 0; j < n; j++) {
            printf("%4d", A[i][j]);
        }
        printf("\n");
    }
    for (i = 0; i < n; i++) {
        for (j = 0; j < n; j++) {
            printf("Assign a value: ");
            scanf("%d", &B[i][j]);
        }
        printf("New row.\n");
    }
    for (i = 0; i < n; i++) {
        for (j = 0; j < n; j++) {
            printf("%4d", B[i][j]);
        }
        printf("\n");
    }
    matrixMultiplier(A, B, C, n);
    return 0;
}

Upvotes: 1

Views: 209

Answers (2)

Shubham
Shubham

Reputation: 1153

Take a look at this code:

#include <stdio.h>
#define MAX_SIZE 10

void matrixMultiplier(int A[][MAX_SIZE], int B[][MAX_SIZE], int C[][MAX_SIZE], int n)
{
 int i, j, k;

 for (i = 0; i < n; i++)
   for (k = 0; k < n; k++)
     for (j = 0; j < n; j++)
       C[i][k] += A[i][j] * B[j][k];

 printf("\nProduct Matrix C:\n\n");
    for(i = 0; i < n; ++i)
    {
        for(j = 0; j < n; ++j)
            printf("%4d", C[i][j]);
        putchar('\n');
    }
}

int main(void)
{
    //int A[][10] = {{0}}; By initializing like this you are creating this int A[1][10] = {{0}};
    int A[MAX_SIZE][MAX_SIZE] = {0};
    int B[MAX_SIZE][MAX_SIZE] = {0};
    int C[MAX_SIZE][MAX_SIZE] = {0};
    int i, j, n;

    printf("Enter square matrix dimension: ");
    scanf("%d", &n);

    printf("Input %d values row-wise, for matrix A: ", n*n);
    for(i = 0; i < n; ++i)
        for(j = 0; j < n; ++j)
            scanf("%d", &A[i][j]);

    printf("Input %d values row-wise, for matrix B: ", n*n);
    for(i = 0; i < n; ++i)
        for(j = 0; j < n; ++j)
            scanf("%d", &B[i][j]);

    printf("\nGiven Matrix A:\n\n");
    for(i = 0; i < n; ++i)
    {
        for(j = 0; j < n; ++j)
            printf("%4d", A[i][j]);
        putchar('\n');
    }

    printf("\nGiven Matrix B:\n\n");
    for(i = 0; i < n; ++i)
    {
        for(j = 0; j < n; ++j)
            printf("%4d", A[i][j]);
        putchar('\n');
    }

   matrixMultiplier(A, B, C, n);
   return 0;
}

If you need more explanation about 2D arrays refer to this link

Upvotes: -1

Mykola Semenov
Mykola Semenov

Reputation: 802

You need to use statically allocated arrays like this:

 int A[10][10] = {{0}};
 int B[10][10] = {{0}};
 int C[10][10] = {{0}};  /* Initialize output matrix to zero */

As other commentators noticed, in C int A[][10] = {{0}}; is basically an equivalent of int A[1][10] = {{0}};, so only first rows are correctly set.

Upvotes: 2

Related Questions