3
votes

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;
}
3
First of all, don't ever use any scanfderivative (except sscanf but only if you are 100% SURE the input is correctely formatted) gidnetwork.com/b-59.htmlhl037_
@hl037_: Nonsense! There is no problem if you know about its pitfalls and behaviour.too honest for this site
In this case, I'm sure the input is correctly formatted. @hl037_Sara
I would suggest using printf() statements at points where you think the problem could be and check whether the output is as expected. That would be sufficient now. Later you can learn using a debugger like GNU debugger(gdb).Kishore
At compile time row1 col1 row2 col2 have not been initialized so they have indeterminate values. These indeterminate values are what you are using to initialize your matrices, because as they are declared they are compile-time created. Probably that is not a problem on that special case because after compiling they end up being zero, but that is not guaranteed.perencia

3 Answers

3
votes
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
      }
    }
}
2
votes

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.

1
votes

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.