Hello All,

I wrote the following code to access a file, assign pointers to the contents of the file, display the file as a matrix, read the last line as a separate matrix, then I am supposed to perform matrix multiplication on the two aforementioned matricies.

I have the first two parts down, but when I try to compute the matrix multiplication y = Ax I get the Segmentation fault (core dump) error.

Here is the contents of matrix5.dat:

7 7 
10.  9.  8.  7.  6.  5.  4.  
 9. 11. -6.  8.  3. -2.  5. 
 8. -6. 12. 10. -8. -6.  4.
 7.  8. 10. 14. 13. -6.  5.
 6.  3. -8. 13. 16. 14. -9. 
 5. -2. -6. -6. 17. 15. 14. 
 4.  5.  4.  5. -9. 19. 10.
 1.  2. -1.  4. -3.  2.  1.
#include <stdio.h>
#include <stdlib.h>
#include <math.h>

int main()
{
        FILE *infile;
        char line[150];
        int row, col, rows, cols, i;
        double **a, *b, *c;
        double dot_p;
        infile = fopen("matrix5.dat", "r");
        if(infile == NULL){
        printf("Error opening matrix5.dat!");
        exit(0);
        }
        fscanf(infile, "%d %d", &rows, &cols);
        printf("\n_________________________________\n\n");
        printf("There are %d rows and %d columns.\n", rows, cols);
        printf("___________________________________\n\n");
        a = (double **)calloc((size_t)cols, sizeof(double *));
        b = (double *)calloc((size_t)cols, sizeof(double));
        c = (double *)calloc((size_t)cols, sizeof(double));
        for(row=0;row<rows;row++){
          a[row] = (double *)calloc((size_t)cols, sizeof(double));
          if(a[row] == NULL){
                printf("Error creating row %d!", row);
                exit(1);
          }
        }
        printf("a[][]:\n");
        for(row=0;row<rows; row++){
          for(col=0;col<cols;col++){
                fscanf(infile, "%lf", a+col);}
          printf("a[%2d] ", row);
          for(col=0;col<cols;col++) printf("%9.3f", a[col]);
        printf("\n");
        }
        printf("\n");
        printf("b[] ");
        do{
        do{
        for(col=0;col<cols;col++){
          fscanf(infile, "%lf", b+col);
          printf("%9.3f", b[col]);
        }
        printf("\n");
        }while(row == rows+2);
        printf("\n");
        for(row=0;row<rows;row++){
          for(i=0,dot_p=0.0;i<cols;i++){
                dot_p += a[row][i] * b[i];
                }
                c[row] = dot_p;
        }
        printf("c[]");
        for(row=0;row<rows;row++){
        printf("%9.3f", c[row]);
        }
        printf("\n");
        for(row=0;row<rows;row++){
        free(a[row]);
        }
        free(a[col]);
        free(b);
        free(c);
        fclose(infile);
        exit(0);

}

Recommended Answers

All 4 Replies

I tried compiling this but it has errors. You actually got this to compile?

yes, it worked just fine minus the core dump error

lines 41 and 42 are duplicates. delete one of them.

Your program has a number of errors in it -- here is a corrected copy. I changed the indentation of some loops to make it easier for me to see what it's doing -- you can just ignore my style if you wish to keep your own.

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#pragma warning(disable: 4996)

int main()
{
        FILE *infile;
        char line[150];
        int row, col, rows, cols, i;
        double **a, *b, *c;
        double dot_p;
        infile = fopen("matrix5.dat", "r");
        if(infile == NULL){
        printf("Error opening matrix5.dat!");
        exit(0);
        }
        fscanf(infile, "%d %d", &rows, &cols);
        printf("\n_________________________________\n\n");
        printf("There are %d rows and %d columns.\n", rows, cols);
        printf("___________________________________\n\n");
        a = (double **)calloc((size_t)rows, sizeof(double *)); // <<<<<<< HERE
        b = (double *)calloc((size_t)cols, sizeof(double));
        c = (double *)calloc((size_t)cols, sizeof(double));
        for(row=0;row<rows;row++)
        {
          a[row] = (double *)calloc((size_t)cols, sizeof(double));
          if(a[row] == NULL)
          {
                printf("Error creating row %d!", row);
                exit(1);
          }
        }
        printf("a[][]:\n");
        for(row=0;row<rows; row++)
        {
          for(col=0;col<cols;col++)
          {
                fscanf(infile, "%lf", &a[row][col]);  // <<<<<<< HERE
          }
          printf("a[%2d] ", row);
          for(col=0;col<cols;col++) 
              printf("%9.3f", a[row][col]);
          printf("\n");
        }
        printf("\n");
        printf("b[] ");
        //do{
        do{
        for(col=0;col<cols;col++){
          fscanf(infile, "%lf", &b[col]);
          printf("%9.3f", b[col]);
        }
        printf("\n");
        }while(row == rows+2);
        printf("\n");
        for(row=0;row<rows;row++)
        {
          for(i=0,dot_p=0.0;i<cols;i++)
          {
              dot_p += a[row][i] * b[i];
          }
          c[row] = dot_p;
        }
        printf("c[]");
        for(row=0;row<rows;row++)
        {
            printf("%9.3f", c[row]);// <<<<<<< HERE
        }
        printf("\n");
        for(row=0;row<rows;row++)
        {
            free(a[row]); // <<<<<<< HERE
        }
        free(a); // <<<<<<< HERE
        free(b);
        free(c);
        fclose(infile);
        exit(0);

}

Ancient Dragon: Thank you sooo much!!! I have spent about 8 hours on this code and was going nuts. It's good to see that at least I was on the right track. Again, thank you very much for your help!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.