title: Matrix Multiplication in C with input from a file
body: <p>I've written a program for matrix multiplication but I want to modify it. Firstly, I want to change matrices to first[a][b] instead of 10 and read the dimensions of matrix from file .Would I need to allocate memory dynamically depending on dimensions of matrix using malloc or I can take some maximum, but that would lead to wastage of lot of memory Would I need to store dimensions of matrix in an array from a file. Suggest what changes do I need ? I am not opening file , just redirecting stdin to file. I am unable to get the input via file ??</p>
  
  <p>Modified code is as </p>
  
  <pre><code>#include &lt;stdio.h&gt;
  
  int main() 
  {
   int 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", &amp;m, &amp;n);
    first = malloc(m*sizeof(int*));
     for (int i =0;i &lt;m; i++)
  first[i] =malloc(n*sizeof(int));
      second = malloc(p*sizeof(int*));
       for(int i=0;i&lt;p;i++)
  second[i] = malloc(q*sizeof(int));
  multiply = malloc(m*sizeof(int));
  for (int i=0;i&lt;q;i++)
  multiply[i] = malloc(q*sizeof(int));
  printf("Enter the elements of first matrix\n");
      for (  c = 0 ; c &lt; m ; c++ )
      for ( d = 0 ; d &lt; n ; d++ )
        scanf("%d", &amp;first[c][d]);
      printf("Enter the number of rows and columns of second matrix\n");
    scanf("%d%d", &amp;p, &amp;q);
    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 &lt; p ; c++ )
    for ( d = 0 ; d &lt; q ; d++ )
      scanf("%d", &amp;second[c][d]);
   for ( c = 0 ; c &lt; m ; c++ ) {
    for ( d = 0 ; d &lt; q ; d++ ) {
      for ( k = 0 ; k &lt; p ; k++ ) {
        sum = sum + first[c][k]*second[k][d];
      }
      multiply[c][d] = sum;
      sum = 0;
    }
    }
   printf("Product of entered matrices:-\n");
   for ( c = 0 ; c &lt; m ; c++ ) {
    for ( d = 0 ; d &lt; q ; d++ )
      printf("%d\t", multiply[c][d]);
    printf("\n");
   }
  for (int i = 0; i &lt; p; i++)
    free(second[i]);
  free(second);
  for (int i = 0; i &lt; q; i++)
    free(multiply[i]);
    free(multiply);
  }
  for (int i = 0; i &lt; m; i++)
  free(first[i]);
   free(first);
  return 0;
   }
  </code></pre>