I am learning to use sequential memory for matrices. I have made following program but it is giving incorrect result. Can someone tell me where I have gone wrong . I am pasting both programs where I am using sequential memory and one in which I have not used and is correct.I am getting incorrect results in sequential one. 

```
#include <stdio.h>
#include <stdlib.h>
 int main() 
 {
    int i, m, n, p, q, c, d, k, sum = 0;
    int *first, *second, *multiply;
    printf("Enter the number of rows and columns of first matrix\n");
    scanf("%d%d", &m, &n);
    printf("Value entered %d%d \n",m,n);
    first = malloc(m*n*sizeof(int*));
    printf("Enter the number of rows and columns of second matrix \n");
    scanf("%d%d", &p,&q); 
    printf("value entered %d%d \n",p,q); 
    second = malloc(p*q*sizeof(int));
    multiply = malloc(m*n*sizeof(int));
    printf("Enter the elements of first matrix\n");
    for( c = 0 ; c < m ; c++ )
       for ( d = 0 ; d < n ; d++ )
         scanf("%d", &first[c*m+d]);
      if ( n != p )
     printf("Matrices with entered orders can't be multiplied with each other.\n");
     else {
      printf("Enter the elements of second matrix\n");
     for ( c = 0 ; c < p ; c++ ){
        for ( d = 0 ; d < q ; d++ )
        scanf("%d", &second[c*p+d]);
   }

 for ( c = 0 ; c < m ; c++ ) {
  for ( d = 0 ; d < q ; d++ ) {
    for ( k = 0 ; k < p ; k++ ) {
      sum = sum + first[c*m+k]*second[k*p+d];
    }
    multiply[c*m+d] = sum;
    sum = 0;
    }

  }

     printf("Product of entered matrices:-\n");
     for ( c = 0 ; c < m ; c++ ) {
       for ( d = 0 ; d < q ; d++ )
    printf("%d\t", multiply[c*m+d]);
  printf("\n");
   }

    free(second);

   free(multiply);
} 

  free(first);
   return 0;
}

```