Home > Back-end >  Multiplying two matrixes in C
Multiplying two matrixes in C

Time:10-06

I'm trying to multiply two matrixes in C but the result gives me:

\23 23\
\23 23\

instead of

\23 20\
\55 48\

1x1 of the matrix keeps repeating and it seems like the matResult[i][j]=sum isn't taking in the sum. The number of rows and columns are defined and there are no scans. I also used a function to calculate the multiplication of the matrix so that I could later use it in the main.

#include <stdio.h>
#define M 2  // m is the number of rows
#define N 2  // n is the number of columns

int i;
int j;
int k;
int sum;

/*
Function to calculate the multiplication of 2 matrixes
*/
int multiMat(int mat1[M][N], int mat2[M][N], int matResult[M][N]) {
  for (int i = 0; i < M; i  ) {
    for (int j = 0; j < N; j  ) {
      for (int k = 0; k < N; k  ) {
        sum = sum   (mat1[i][k] * mat2[k][j]);
      }
      matResult[i][j] = sum;
      sum = 0;
    }
  }
  return matResult[i][j];
}
int main(void) {
  int mat1[M][N] = {{1, 2}, {3, 4}};
  int mat2[M][N] = {{9, 8}, {7, 6}};

  int matResult[M][N];
  printf("\n\n  RESULT ");
  printf("\n  ===============\n");
  for (int i = 0; i < M; i  ) {
    for (int j = 0; j < N; j  ) {
      printf("     %d\t", multiMat(mat1, mat2, matResult));
    }
    printf("\n");
  }
  return 0;
}

CodePudding user response:

Here's my version of your code cleaned up. I've removed M as your matrices are square and your code won't easily adapt to multiplying two non-square matrices (though, as I noted in a comment, you can multiply an M×N matrix by an N×P matrix, and the result is an M×P matrix).

#include <stdio.h>

#define N 2  // n is the size of the square matrices

static void multiMat(int mat1[N][N], int mat2[N][N], int matResult[N][N])
{
    for (int i = 0; i < N; i  )
    {
        for (int j = 0; j < N; j  )
        {
            int sum = 0;
            for (int k = 0; k < N; k  )
            {
                sum = sum   (mat1[i][k] * mat2[k][j]);
            }
            matResult[i][j] = sum;
        }
    }
}

static void printMat(const char *tag, int mat[N][N])
{
    printf("%s (%dx%d):\n", tag, N, N);
    for (int i = 0; i < N; i  )
    {
        for (int j = 0; j < N; j  )
        {
            printf(" =", mat[i][j]);
        }
        printf("\n");
    }
}

int main(void)
{
    int mat1[N][N] = {{1, 2}, {3, 4}};
    int mat2[N][N] = {{9, 8}, {7, 6}};
    int mat3[N][N];

    multiMat(mat1, mat2, mat3);

    printMat("Mat1", mat1);
    printMat("Mat2", mat2);
    printMat("Mat3", mat3);

    return 0;
}

When run, it produces the output:

Mat1 (2x2):
   1   2
   3   4
Mat2 (2x2):
   9   8
   7   6
Mat3 (2x2):
  23  20
  55  48

The result (renamed Mat3 in this code) is what you wanted. It's often a good idea to print the inputs as well as the output — it ensures that the computer is thinking about things the same way you are. It also encourages writing functions like printMat(). It is possible to provide the dimensions of the matrices to the functions using VLA (variable-length array) notation. For example, a more general matrix multiply function might be:

void MatrixMuliply(size_t M, size_t N, size_t P, int mat1[M][N], int mat2[N][P], int mat3[M][P])
{
    for (int i = 0; i < M; i  )
    {
        for (int j = 0; j < P; j  )
        {
            int sum = 0;
            for (int k = 0; k < N; k  )
                sum  = mat1[i][k] * mat2[k][j];
            mat3[i][j] = sum;
        }
    }
}
  • Related