Sara
Sara

Reputation: 121

Matrix multiplication in C - error values in result matrix

So I coded traditional matrix multiplication in C (I'm a beginner to C), but for some reason my result isn't correct, although I don't see any glaring errors. My input file looks like:

3 2

2 2

2 2

2 2

2 3

1 1 1

1 1 1

The 3 2 and 2 3 in the first and fifth lines represent the number of rows and columns in the subsequent matrices. The result should be a 3x3 matrix, all 4s. However, this code returns

4197299 4 4

4 4 4

-1912599044 32621 572

I'm inclined to believe this might be due to the way I declared the matrices. Instead of using malloc, I scanned the row and column values from the input file and directly instantiated the required matrices. I'm very new to C, so the concept of dynamic memory allocation isn't 100% clear yet. I could be totally off the mark, but I'm not sure. What confuses me especially is that about half of the matrix returned correct values. Why is this the case? Below is my code.

#include<stdio.h>
#include<stdlib.h>


int main(int argc, char** argv){
  int i, j, k, row1, col1, row2, col2, temp;
  if(argc != 2){
    printf("error\n");
    exit(1);
  }

  FILE *file = fopen(argv[1], "r");
  if(file == NULL){
    printf("error\n");
    exit(1);
  }
  //MATRIX 1
  fscanf(file, " %d\t%d", &row1, &col1);
  int matrix1[row1][col1];

  for(i = 0; i<row1; i++){
    for(j=0; j<col1; j++){
      fscanf(file, "%d", &temp);
      matrix1[i][j] = temp;
    }
  }
  //MATRIX TWO
  fscanf(file, " %d\t%d", &row2, &col2);
  int matrix2[row2][col2];
  int product[row1][col2]; //DECLARING PRODUCT MATRIX
  for(i = 0; i<row2; i++){
    for(j=0; j<col2; j++){
      fscanf(file, "%d", &temp);
      matrix2[i][j] = temp;
    }
  }  
  
  for(i = 0; i<row1; i++){
    for(j = 0; j<col2; j++){
      for(k = 0; k<col1; k++){
    product[i][j] += matrix1[i][k]*matrix2[k][j]; //MULTIPLICATION STEP
      }
    }
  }

  for(i = 0; i<row1; i++){
    for(j = 0; j<col2; j++){
      printf("%d\t", product[i][j]); //PRINTING THE PRODUCT
    }
    printf("\n");
  }
  
  return 0;
}

Upvotes: 3

Views: 440

Answers (3)

Davislor
Davislor

Reputation: 15134

Yeah, that way of declaring dynamic arrays doesn't necessarily work, in any version before or after C99, although what’s causing the problem is that you don’t initialize product. Try calloc( row1*col2, sizeof(int) ); This allocates all the elements and initializes them to 0.

Upvotes: 1

Kishore
Kishore

Reputation: 839

for(i = 0; i<row1; i++){
    for(j = 0; j<col2; j++){
      product[i][j] = 0; // should be zero before summing
      for(k = 0; k<col1; k++){
          product[i][j] += matrix1[i][k]*matrix2[k][j]; //MULTIPLICATION STEP
      }
    }
}

Upvotes: 3

Some programmer dude
Some programmer dude

Reputation: 409166

You don't initialize product, which means that its contents is indeterminate, then using it in calculations (with e.g. +=) results in undefined behavior.

You need to initialize the products matrix to all zeroes first.

Upvotes: 2

Related Questions