Thread: Hi, problem with pointers and matrix multiplication

  1. #1
    Registered User
    Join Date
    Nov 2010
    Posts
    1

    Hi, problem with pointers and matrix multiplication

    Here is my code:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    int main(void)
    {
            FILE *infile;
            int row, col, rows, cols;
            double **a, *b, *c, sum;
    /* Define infile */
            infile = fopen("matrix5.dat", "r");
    /* read rows and cols */
            fscanf(infile, "%d  %d", &rows, &cols);
            printf("Rows = %d  Cols = %d\n", rows, cols);
    
    /* Allocate memory to **a */
            a=( double **)calloc((size_t)cols, sizeof(double *));
    /* Allocate memory for a[row], row=0, 1, ..., rows-1 */
            for(row=0; row<rows; row++){
                    a[row] = (double *)calloc((size_t)cols, sizeof(double));
            }
    /* Allocate memory for b[rows] and c[rows] */
            b=(double *)calloc((size_t)cols, sizeof(double *));
            c=(double *)calloc((size_t)cols, sizeof(double *));
    /* read all rows and save the values in a[][] */
    
            for(row=0; row<rows; row++){
                    for(col=0; col<cols; col++)
                            fscanf(infile, "%lf", a[row]+col);
            }
    
    /* read b[] from the last line */
            for(col=0; col<cols; col++){
                    fscanf(infile, "%lf", b+col);
            }
    /* print a[][] */
            for(row=0; row<rows; row++){
                    for(col=0; col<cols; col++)
                            printf("%9.3f ", a[row][col]);
                    printf("\n");
            }
    
    /* print b[] */
            printf("\n");
            for(col=0; col<cols; col++){
                    printf("%9.3f ", b[col]);
            }
    
    /* compute c[] = a[][] * b[] */
            for(row=0; row<rows; row++){
                    for(col=0; col<cols; col++){
                            sum += a[row][col]*b[col];
                    }
                    c[row]=sum;
                    sum=0;
            }
    /* print c[] */
            printf("\n");
            for(row=0; row<rows; row++){
                    printf("%9.3f ", c[row]);
            }
            printf("\n");
    /* return memory */
    
    /* close infile */
    fclose(infile);
    }
    When it multiplies the matrix, b[5] seems to take on the value of the previous answer, which was 44, throwing the rest of the answers off.

    a[][] is a 7x7 matrix and b[] is a 1x7 matrix.
    I seriously do not know what is wrong, I've been at it for hours. I'm using vi editor, compile with gcc.
    Thanks!
    Last edited by Phadelity; 11-23-2010 at 10:11 PM.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    sum is used uninitialized.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Code:
    /* Allocate memory for b[rows] and c[rows] */
            b=(double *)calloc((size_t)cols, sizeof(double *));
            c=(double *)calloc((size_t)cols, sizeof(double *));
    And these are the wrong size.
    They should be sizeof(double)

    Also, calloc for doubles does not guarantee that the resultant memory has a value of 0.0
    Question 7.31

    Also, you should not be casting the return result of malloc in a C program.
    Question 7.7b


    You can fix both things easily with
    Code:
    b = malloc( cols * sizeof *b );
    c = malloc( cols * sizeof *c );
    Using the "dereferenced pointer" makes the compiler work out what size is really needed.
    And no, this is not dereferencing a variable at run time - this is all in the compiler at compile time.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Sorting Matrix
    By alex 2010 in forum C++ Programming
    Replies: 0
    Last Post: 06-24-2010, 09:40 AM
  2. C - access violation
    By uber in forum C Programming
    Replies: 2
    Last Post: 07-08-2009, 01:30 PM
  3. problem with structure pointers
    By spiit231 in forum C Programming
    Replies: 4
    Last Post: 03-16-2008, 01:15 PM
  4. matrix class
    By shuo in forum C++ Programming
    Replies: 2
    Last Post: 07-13-2007, 01:03 AM
  5. Gauss-Jordan Matrix Inversion in C++
    By Max_Power82 in forum C++ Programming
    Replies: 3
    Last Post: 12-03-2006, 08:31 PM