-1

So as stated in the title I have a program to multiplies to matrices read from a file, but when I run it it simply crashes. I am required to have two functions to perform the multiplication and one to print the result while using pointers with no return values. Any help is appreciated.

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

void mat_mult(int ** arr1, int rows1, int cols1, int ** arr2, 
    int rows2, int rcols2, int ** arr3);
void mat_out(int ** arr, int rows, int cols);

int main(void){
int **mat1, **mat2, **res, rows1, cols1, rows2, cols2, i, j;
    FILE* f;
    f = fopen("Matrices.txt", "r");
    fscanf(f, "%d", &rows1);
    fscanf(f, "%d", &cols1);
    mat1 = (int**) malloc(rows1*sizeof(int*));
    for(i = 0;i < rows1;i++){
        mat1[i] = (int*)malloc(cols1*sizeof(int));
    }
    for(i = 0;i < rows1;i++){
        for(j = 0;j < cols1;j++){
            fscanf(f, "%d", &mat1[i][j]);
        }
    }
    fscanf(f, "%d", &rows2);
    fscanf(f, "%d", &cols2);
    mat2 = (int**) malloc(rows2*sizeof(int*));
    for(i = 0;i < rows2;i++){
        mat2[i] = (int*)malloc(cols2*sizeof(int));
    }
    for(i = 0;i < rows2;i++){
        for(j = 0;j < cols2;j++){
            fscanf(f, "%d", &mat2[i][j]);
        }
    }
    res = (int**)calloc(rows1,sizeof(int*));
    for(i = 0;i < rows1;i++){
        res[i] = (int*)calloc(cols2,sizeof(int));
    }
    /*mat_mult(mat1, rows1, cols1, mat2, rows2, cols2, res);
    mat_out(res, rows1, cols2);*/

}

void mat_mult(int ** mat1, int rows1, int cols1, int ** mat2, 
    int rows2, int cols2, int ** res){
    int i, j, k;
    for(i = 0;i < rows1;i++){
        for(j = 0;j < cols2;j++){
            res[i][j] = 0;
            for(k = 0;k < cols1;k++){
                res[i][j] += mat1[i][k] * mat2[k][j];
            }
        }
    }
}
void mat_out(int ** res, int rows, int cols){
int i, j;
    for(i = 0;i < rows;i++){
        for(j = 0;j < cols;j++){
            printf("%d ",res[i][j]);
        }
    printf("\n");
    }
}
  • Can you identify where the crash happens? – ysap Apr 11 '17 at 21:50
  • One of the first things to try when your program doesn't work, and especially when it crashes mysteriously, is to run it in a debugger. At minimum, you ought to be able to find where the crash is occurring. – John Bollinger Apr 11 '17 at 21:50
  • 1
    However, your code looks OK to me and runs just fine (after uncommenting the calls to `mat_mult()` and `mat_out()`). The only problem valgrind reports is failure to free the allocated memory, and that won't, in itself, cause a crash. – John Bollinger Apr 11 '17 at 21:58
  • 1
    Are you sure you have `Matrices.txt` defined correctly? – ysap Apr 11 '17 at 21:59
  • If indeed you have a crash, then the most plausible reason is bad input. In particular, if the second dimension of the first matrix is (read as) greater than the first dimension of the second matrix, then you'll get an out-of-bounds array access producing UB. You really ought to validate that those are the same. (Where I mean "matrix" in an abstract, mathematical sense.) – John Bollinger Apr 11 '17 at 22:05
  • There is no matrix (aka 2D array) in your code. A pointer is not an array! – too honest for this site Apr 11 '17 at 22:06

1 Answers1

0

I didn't see any errors, so I compiled and executed your program and it worked as expected, maybe your problem was the data that you added to your file Matrices.txt.

My file Matrices.txt is:

2
2
3
2
1
4
2
2
3
2
1
4

This file will produce 2 matrix with cells equal to:

3 2
1 4

And the multiplication result is:

enter image description here

Which is perfectly fine.

Patricio Sard
  • 2,092
  • 3
  • 22
  • 52