title: Diagonal difference on HackerRank
body: <p>I was trying to solve <a href="https://www.hackerrank.com/challenges/diagonal-difference/problem" rel="nofollow noreferrer">this</a>. My algorithm gives right answer in ofline compiler. I dont know where is the error. I am completely new to C language.</p>
  
  <p>The problem asked to create a function to find the absolute difference of right and left diagonals of a square matrix. </p>
  
  <p>This is my solution for the website. When I run the code online it gives the output 12.</p>
  
  <pre><code>int diagonalDifference(int arr_rows, int arr_columns, int** arr) {
  
      int primary_sum, secondary_sum = 0;
      for(int row,column = 0; row &lt; arr_rows &amp;&amp; column &lt; arr_columns; row++, column++){
  
          primary_sum += arr[row][column];
          secondary_sum += arr[row][arr_columns - column - 1];
  
      }
  
      return abs(primary_sum - secondary_sum);
  
  }
  </code></pre>
  
  <p>I wrote a similar program to test ofline. </p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  
  void matrix_sum(int arr_rows, int arr_columns, int arr[3][3]);
  int main(void){
  
      int arr[3][3] = {
                      {11, 2, 4},
                      {4, 5, 6},
                      {10, 8, -12}
                    };
  
      matrix_sum(3, 3, arr);
  
  
  }
  
  
  void matrix_sum(int arr_rows, int arr_columns, int arr[3][3]){
  
      int row,column = 0;
      int primary_sum, secondary_sum = 0;
      int digonals[3];
      int s_digonals[3];
  
      for(; row &lt; arr_rows &amp;&amp; column &lt; arr_columns; row++, column++){
          primary_sum += arr[row][column];
          secondary_sum += arr[row][arr_columns - column - 1];
  
          digonals[row] = arr[row][column];
          s_digonals[row] = arr[row][arr_columns - column - 1];
  
      }
  
      printf("primary_sum = %i\n", primary_sum);
      printf("secondary_sum = %i\n", secondary_sum);
  
      for(int i = 0; i &lt; row; i++){
          printf("%i ",digonals[i]);
      }
      printf("\n");
  
      for(int i = 0; i &lt; row; i++){
          printf("%i ",s_digonals[i]);
      }
      printf("\n%i\n", abs(primary_sum - secondary_sum) );
  
  
  
  }
  </code></pre>
  
  <p>second program gives correct result which is 15 and I don't know why online compiler gives output 12.</p>
  
  <p>Input was.</p>
  
  <p>3</p>
  
  <p>11 2 4</p>
  
  <p>4 5 6</p>
  
  <p>10 8 -12</p>