C program 2D matrix multiplication using malloc

This is a discussion on C program 2D matrix multiplication using malloc within the C Programming forums, part of the General Programming Boards category; Hear is a program that I created that already has Matrix A and B filled in. I then created a ...

  1. #1
    Registered User
    Join Date
    Apr 2009
    Posts
    3

    C program 2D matrix multiplication using malloc

    Hear is a program that I created that already has Matrix A and B filled in. I then created a driver program to create Matrix C and fill in with Matrix A*B. My problem is that I'm getting a Segmentation fault. I know this has to deal with memory that doesnt belong to me, but I have looked around on this site and others and my code looks to be fine in my eyes. LOL must not be very good then. Please help..very confused on where my segmentation fault is happening at. I believe its with a for loop that I have created.

    Code:
    #include <stdlib.h>
    #include <stdio.h>
    #include <math.h>
    int **MatrixMultiply(int **A, int **B, int n);
    void printMatrix(int **array, int rows, int columns);
    int main(void)
    {
    int i=0;
    int j=0;
    
    //Stores Matrix A #s
    int **A;
    A=(int**) malloc(5*sizeof(int*));
    for (i=0;i<5;i++)
    {
    A[i]=(int*) malloc(5*sizeof(int));
    A[0][0]=3,A[0][1]=1,A[0][2]=5,A[0][3]=42,A[0][4]=35;
    A[1][0]=8,A[1][1]=34,A[1][2]=64,A[1][3]=21,A[1][4]=68;
    A[2][0]=61,A[2][1]=41,A[2][2]=55,A[2][3]=32,A[2][4]=87;
    A[3][0]=17,A[3][1]=14,A[3][2]=53,A[3][3]=52,A[3][4]=12;
    A[4][0]=35,A[4][1]=54,A[4][2]=15,A[4][3]=12,A[4][4]=12;
    }
    
    
    //Stores Matrix B #s
    int **B;
    B =(int**) malloc(5*sizeof(int*));
    for(j=0; j<5;j++)
    {
    B[i]=(int*) malloc(5*sizeof(int));
    B[0][0]=16,B[0][1]=10,B[0][2]=5,B[0][3]=74,B[0][4]=35;
    B[1][0]=71,B[1][1]=8,B[1][2]=68,B[1][3]=38,B[1][4]=98;
    B[2][0]=13,B[2][1]=31,B[2][2]=51,B[2][3]=42,B[2][4]=74;
    B[3][0]=22,B[3][1]=61,B[3][2]=35,B[3][3]=67,B[3][4]=43;
    B[4][0]=54,B[4][1]=47,B[4][2]=12,B[4][3]=11,B[4][4]=33;
    }
    
    
    
    //prints matrix with for loop
    for(i=0;i<5;i++)
    {
     printf("%d",**MatrixMultiply(A,B,5));
    }
    printf("\n");
    
    //prints matrix with print function
    //printf("%d",printMatrix(C,5,5));
    return (0);
    }
    
    int **MatrixMultiply(int **A, int **B, int n)
    {
    
    int i;
    int j;
    int k;
    int sum;
    int **C;
    C=(int**) malloc(5*sizeof(int*));
    
    for (i=0;i<5;i++)
    {
    C[i]=(int*) malloc(5*sizeof(int));
    }
    
    for (i=0; i<n; i++)
    {
     for (j=0; j<n; j++)
     {
      sum = 0;
      for(k=0;k<n;k++)
      {
      sum += A[i][k] * B[k][j];
      }
      C[i][j]=sum;
    
     }
    }
    
    return(C);
    }
    
    void printMatrix(int **array, int rows, int columns)
    {
      int i, j;
      for(i=0; i<rows; i++)
      {
        printf("[ ");
        for(j=0; j<columns; j++)
        {
            printf("%5d ", array[i][j]);
        }
        printf("]\n");
      }
    }

  2. #2
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,058
    Before initializing the dynamic array, malloc() storage for all the int items, as in.
    Code:
    for (i=0;i<5;i++)
        A[i]=(int*) malloc(5*sizeof(int));
    A[0][0]=3,A[0][1]=1,A[0][2]=5,A[0][3]=42,A[0][4]=35;
    A[1][0]=8,A[1][1]=34,A[1][2]=64,A[1][3]=21,A[1][4]=68;
    A[2][0]=61,A[2][1]=41,A[2][2]=55,A[2][3]=32,A[2][4]=87;
    A[3][0]=17,A[3][1]=14,A[3][2]=53,A[3][3]=52,A[3][4]=12;
    A[4][0]=35,A[4][1]=54,A[4][2]=15,A[4][3]=12,A[4][4]=12;

  3. #3
    Registered User
    Join Date
    Apr 2009
    Posts
    3
    I'm a little confused on what you are trying to explain to me. The way I took what you where trying to say is that my code for that segment should look like this:
    Code:
    for(j=0; j<5;j++)
    {
    B[i]=(int*) malloc(5*sizeof(int));
    }
    B[0][0]=16,B[0][1]=10,B[0][2]=5,B[0][3]=74,B[0][4]=35;
    B[1][0]=71,B[1][1]=8,B[1][2]=68,B[1][3]=38,B[1][4]=98;
    B[2][0]=13,B[2][1]=31,B[2][2]=51,B[2][3]=42,B[2][4]=74;
    B[3][0]=22,B[3][1]=61,B[3][2]=35,B[3][3]=67,B[3][4]=43;
    B[4][0]=54,B[4][1]=47,B[4][2]=12,B[4][3]=11,B[4][4]=33;
    When I did this to my program it still gave me a segmentation fault. So again I'm kinda confused as to what you are trying to explain to me.

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    This:
    Code:
    A[0][0]=3,A[0][1]=1,A[0][2]=5,A[0][3]=42,A[0][4]=35;
    can also be written as:
    A[0][0]=(3,A[0][1]=(1,A[0][2]=(5,A[0][3]=(42,A[0][4]=35))));
    [/code] (subject to right number of end parenthesis).

    Another way to write the exact same functionality it would be:
    Code:
    A[0][0]=A[0][1]=A[0][2]=A[0][3]=A[0][4]=35;
    since the comma operator evaluates to the last argument. So you are setting ALL elements to the last assigned value.

    My guess is that you didn't REALLY mean that when you wrote the code.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,058
    Quote Originally Posted by college_kid View Post
    I'm a little confused on what you are trying to explain to me. The way I took what you where trying to say is that my code for that segment should look like this:
    Code:
    for(j=0; j<5;j++)
    {
    B[i]=(int*) malloc(5*sizeof(int));
    }
    B[0][0]=16,B[0][1]=10,B[0][2]=5,B[0][3]=74,B[0][4]=35;
    B[1][0]=71,B[1][1]=8,B[1][2]=68,B[1][3]=38,B[1][4]=98;
    B[2][0]=13,B[2][1]=31,B[2][2]=51,B[2][3]=42,B[2][4]=74;
    B[3][0]=22,B[3][1]=61,B[3][2]=35,B[3][3]=67,B[3][4]=43;
    B[4][0]=54,B[4][1]=47,B[4][2]=12,B[4][3]=11,B[4][4]=33;
    When I did this to my program it still gave me a segmentation fault. So again I'm kinda confused as to what you are trying to explain to me.
    Can you spot the difference??

  6. #6
    Registered User
    Join Date
    Apr 2009
    Posts
    3
    Thank you, That makes much more sense. Didn't realize you couldn't declare like that. It worked. Now just have to take care of a couple bugs...yippy.

    Thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A Full Program to analyze.
    By sergioms in forum C Programming
    Replies: 2
    Last Post: 12-30-2008, 09:42 AM
  2. Matrix Multiplication ( Accessing data from a file ) HELP
    By six_degreez in forum C Programming
    Replies: 2
    Last Post: 07-24-2008, 06:21 PM
  3. Matrix Multiplication with simple syntax! plz help.
    By codebox in forum C Programming
    Replies: 6
    Last Post: 11-05-2004, 09:03 AM
  4. Very handy matrix functions - part 1
    By VirtualAce in forum Game Programming
    Replies: 8
    Last Post: 05-20-2004, 11:38 AM
  5. Matrix and vector operations on computers
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 11
    Last Post: 05-11-2004, 07:36 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21