title: How to perform different operations on matrix using dynamic memory allocation?
body: <p>There is segmentation fault in <code>case 5</code> of this program(transpose) this segmentation fault occurs only when the input row is greater than that of the input column. Hopefully this is due to the reason I have not allocated the memory accordingly.</p>
  
  <pre><code>b= (int**)malloc(r*sizeof(int*));
      for(i=0; i&lt;c; i++)
      {
          b[i] = (int*)malloc(sizeof(int));
      }
  </code></pre>
  
  <p>And if I am doing it accordingly like this:</p>
  
  <pre><code>b= (int**)malloc(c*sizeof(int*));
      for(i=0; i&lt;r; i++)
      {
          b[i] = (int*)malloc(sizeof(int));
      }
  </code></pre>
  
  <p>there still is seg. fault and this time it is not producing correct output for matrix of any order.</p>
  
  <p><strong><em>Here is my full code :</em></strong> </p>
  
  <pre><code>#include&lt;stdio.h&gt;
  #include&lt;stdlib.h&gt;
  int** inputmatrix(int **a,int r, int c)
  {
      int i, j;
      a = (int**)malloc(r*sizeof(int));
      for(i=0; i&lt;c; i++)
      {
          a[i] = (int*)malloc(sizeof(int));
      }
      printf("\n Input the Elements of the Matrix :");
      for(i=0; i&lt;r; i++)
      {
          for(j=0; j&lt;c; j++)
          {
              scanf("%d",&amp;a[i][j]);
          }
      }
      return a;
  }
  void showmatrix(int** a, int r, int c)
  {
      int i, j;
      for(i=0; i&lt;r; i++)
      {
          printf("\n");
          for(j=0; j&lt;c; j++)
          {
              printf("  %d",a[i][j]);
          }
      }
  }
  
  int** add(int **a, int **b, int r, int c)
  {
      int i,j;
  
      for(i=0; i&lt;r; i++)
      {
          for(j=0; j&lt;c; j++)
          {
              a[i][j] = a[i][j]+b[i][j];
          }
      }
      return a;
  }
  int** multiplication(int** a, int **b, int r1, int c1, int c2)
  {
      int **c,i,j,k;
      c = (int**)malloc(r1*sizeof(int*));
      for(i=0; i&lt;c2; i++)
      {
          c[i] = (int*)malloc(sizeof(int));
      }
      for(i=0; i&lt;r1; i++)
      {
          for(j=0; j&lt;c2; j++)
          {
              c[i][j] = 0;
              for(k=0; k&lt;c1; k++)
              {
                  c[i][j] = c[i][j] + a[i][k]*b[k][j];
              }
          }
      }
      return c;
  }
  
  int minval(int **a, int r, int c)
  {
      int i, min;
      min = a[r][0];
      for(i=0; i&lt;c; ++i)
      {
          if(a[r][i]&lt;min)
          {
              min = a[r][i];
          }
      }
      return min;
  }
  
  int maxval(int **a, int r, int c)
  {
      int i, max;
      max = a[0][c];
      for(i=0; i&lt;r; ++i)
      {
          if(a[i][c] &gt; max )
          {
              max = a[i][c];
          }
      }
      return max;
  }
  
  void saddlepoint(int **a, int r, int c)
  {
      int i, j, rpos, cpos, flag = 0,sp;
      for(i=0; i&lt;r; ++i)
      {
          for(j=0; j&lt;c; ++j)
          {
              if(a[i][j] == minval(a, i, c) &amp;&amp; a[i][j] == maxval(a, r, j))
              {
                  sp = a[i][j];
                  flag = 1;
                  rpos = i;
                  cpos = j;
              }
          }
      }
      if(flag == 1)
      {
          printf("\n The Saddle point of the Matrix is found at position (%d,%d) value is %d ", rpos, cpos,sp);
      }
      else
      {
          printf("\n There is no saddle point in the Matrix ");
      }
  }
  
  int magicsquare(int **a, int r, int c)
  {
      int i, j, row_sum, col_sum, d1, d2, flag = 0;
      if(r==c)
      {
          for(i =0 ;i&lt;r; i++)         // for digonals
           {
              d1 = d1 + a[i][i];
              d2 = d2 + a[i][r-i-1];
          }
          for(i=0; i&lt;r; i++)
          {
              row_sum = 0;
              for(j=0; j&lt;c; j++)
              {
                  row_sum = row_sum + a[i][j];
              }
              if(row_sum == d1)
                  flag = 1;
              else
                  break;
          }
          for(i=0; i&lt;r; i++)
          {
              col_sum = 0;
              for(j=0; j&lt;c; j++)
              {
                  col_sum = col_sum + a[j][i];
              }
              if(col_sum == d1)
                  flag =1;
              else
                  break;
          }
      }
      else
      {
          printf("\n This Matrix is not a Magic Square ");
      }
      return flag;
  }
  
  int** transpose(int **a, int r, int c)
  {
      int i, j, **b;
      b= (int**)malloc(c*sizeof(int*));
      for(i=0; i&lt;r; i++)
      {
          b[i] = (int*)malloc(sizeof(int));
      }
      for(i =0; i&lt;r; i++)
      {
          for(j=0; j&lt;c; j++)
          {
              b[j][i] = a[i][j];
          }
      }
      return b;
  }
  
  int main()
  {
      int **a, **b,r1,c1,r2,c2, i,j,ch,f;
      int **c;
      printf("\n enter your choice : \n1.Addition \n2.Multiplication \n3.Saddle Point \n4. Magic Square \n5.Transpose\n");
      scanf("%d",&amp;ch);
      printf("\n enter the oder of matrix A :");
      scanf("%d%d",&amp;r1,&amp;c1);
      a = inputmatrix(a,r1,c1);
      switch(ch)
      {
          case 1:
                  printf("\n enter the oder of matrix B :");
                  scanf("%d%d",&amp;r2,&amp;c2);
                  if(r1==r2 &amp;&amp; c1==c2)
                  {
                      b = inputmatrix(b,r2,c2);
                      a = add(a,b,r1,c1);
                      printf("\n the result of the addition of matrices is :");
                      for(i=0; i&lt;r1; i++)
                      {
                          printf("\n");
                          for(j=0;j&lt;c1; j++)
                          {
                              printf("%d\t",a[i][j]);
                          }
                      }
                  }
                  else
                  {
                      printf("\n these matrices can't be added ");
                  }
                  break;
          case 2 :
                  printf("\n Enter the Order of Matrix B :");
                  scanf("%d%d",&amp;r2,&amp;c2);
                  b = inputmatrix(b,r2,c2);
                  if(c1 == r2)
                  {
                      c = multiplication(a, b, r1, c1, r2);
                      for(i=0; i&lt;r1; i++)
                      {
                          printf("\n");
                          for(j=0; j&lt;c2; j++)
                          {
                              printf("%d\t",c[i][j]);
                          }
                      }
                  }
                  else
                  {
                      printf("\n Sorry, These Matrices Can't be Multiplied ");
                  }
                  break;
          case 3 :
                  printf("\n The Matrix you Entered is :");
                  for(i=0; i&lt;r1; i++)
                  {
                      printf("\n");
                      for(j=0; j&lt;c1; j++)
                      {
                          printf("  %d",a[i][j]);
                      }
                  }
                  saddlepoint(a,r1,c1);
                  break;
          case 4 :
                  printf("\n The Matrix you Entered is :");
                  for(i=0; i&lt;r1; i++)
                  {
                      printf("\n");
                      for(j=0; j&lt;c1; j++)
                      {
                          printf("  %d",a[i][j]);
                      }
                  }
                  int f =  magicsquare(a, r1, c1);
                  if(f==1)
                      printf("\n This Matrix is a Magic Square ");
                  else
                      printf("\n This Matrix is not a Magic Square ");
                  break;
          case 5 :
                  printf("\n The Matrix you enter is :");
                  showmatrix(a,r1,c1);
                  b = transpose(a,r1,c1);
                  printf("\n the transpose of the entered matrix is :");
                  for(i=0; i&lt;c1; i++)
                  {
                      printf("\n");
                      for(j=0; j&lt;r1; j++)
                      {
                          printf("  %d",b[i][j]);
                      }
                  }
                  break;
  
          default : printf("\n Sorry, This is a Wrong Choice ");
              }
      return 0;
  }
  </code></pre>
  
  <p><strong><em>A few Output cases are also below:</em></strong></p>
  
  <p><strong>case 1:</strong></p>
  
  <pre><code>enter your choice : 
  1.Addition 
  2.Multiplication 
  3.Saddle Point 
  4. Magic Square 
  5.Transpose
  5
  
  enter the oder of matrix A :3
  2
  
  Input the Elements of the Matrix :1
  2
  3
  4
  5
  Segmentation fault (core dumped)
  </code></pre>
  
  <p><strong>case 2:</strong></p>
  
  <pre><code>enter your choice:
  1.Addition 
  2.Multiplication 
  3.Saddle Point
  4. Magic Square 
  5.Transpose
  5
  
  enter the oder of matrix A :2
  3
  
  Input the Elements of the Matrix :1
  2
  3
  4
  5
  6
  
  The Matrix you enter is :
  1  2  3
  4  5  6
  the transpose of the entered matrix is :
  1  4
  2  5
  3  6
  </code></pre>
  
  <p>And there is some problem in the multiplication function also, there also right matrix is not being displayed.</p>