title: Creating 2 Random Matrices in C using malloc and pointers
body: <p>I have an assignment to create two randomly generated matrices of size <code>n</code> (where <code>n</code> = 4, 16, 32, 64, 128, 256, 512, 1024, 2048, 4096, 8192) and multiply these matrices.</p>
  
  <p>I am creating the matrices using <code>malloc</code>. I'm using just <code>long** intCreateMatrix(n)</code>
  function to create both the matrices <code>A</code> and <code>B</code>. This function returns a pointer to the matrix. Another function 
  <code>long** intInitializeMatirx(a,n)</code> is used to initialize the matrix with random values. </p>
  
  <p>When I call these functions for two different matrices it prints the same contents of matrix for both the matrices.The code is given below:</p>
  
  <pre><code>void intAlgo(int n,int algo){
  
      long **a;
      long **b;
      long **c;
  
      a = intCreateMatrix(n);
      printf("Matrix A:\n");
      a = intInitializeMatrix(a,n);
  
      b = intCreateMatrix(n);
      printf("Matrix B:\n");
      b = intInitializeMatrix(b,n);
  
  }
  
  long** intCreateMatrix(int n){
  
      long **arr;
      int i,j;
      arr = (long **)malloc(n * sizeof(long *));
      for (i=0; i&lt;n; i++)
          arr[i] = (long *)malloc(n * sizeof(long));
      return arr;
  
  }
  
  long** intInitializeMatrix(long **arr,int n){
  
      int i,j;
      srand(time(0));
      for(i=0;i&lt;n;i++){
          for(j=0;j&lt;n;j++)
              arr[i][j] = rand()%10;
      }
      for(int i=0;i&lt;n;i++){
          for(int j=0;j&lt;n;j++)
              printf("%ld ",arr[i][j]);
          printf("\n");
      }
      printf("\n");
      return arr;
      free (arr);
  
  }
  </code></pre>
  
  <p><code>intAlgo</code> is the function that calls the <code>createMatrix</code> and Initialises matrix.</p>
  
  <p><code>CreateMAtrix</code> takes n as input and returns a pointer to the matrix created using <code>malloc</code>.</p>
  
  <p><code>InitialiseMatrix</code> initializes the matrix using rand function and prints the matrix and returns the matrix.</p>
  
  <p>When I execute it <code>a</code> and <code>b</code> should have different values 
  but they have the same values example I'm getting these values for Matrix <code>A</code> and `B? :</p>
  
  <p>Matrix <code>A</code>:</p>
  
  <pre class="lang-none prettyprint-override"><code>0 7 2 3 
  
  0 6 3 1 
  
  7 2 0 2 
  
  6 3 2 6 
  </code></pre>
  
  <p>Matrix <code>B</code>:</p>
  
  <pre class="lang-none prettyprint-override"><code>0 7 2 3 
  
  0 6 3 1 
  
  7 2 0 2 
  
  6 3 2 6 
  </code></pre>
  
  <p>These two matrices should be different I'm not sure where 
  I am going wrong. Could you please guide me?</p>