-2

for some reason my program does not give me the correct diagonal difference.for example in the input of (11 2 4) (4 5 6) (10 8 -12) its supposed to give me 15 by adding (11+5+(-12)) and (4+5+10) then doing abs difference of two, but gives me 6 for some reason.

Here is my function for the code:

int diagonalDifference(vector<vector<int>> arr) {
       int result = 0;
       int difference = 0;
       int first_diagonal =0;
       int second_diagonal = arr.size();
       int add_for_first_diagonal = 0;
       int add_for_second_diagonal = 0;
       for(int row = 0; row < (int)arr.size(); row++) {
           for(int column =0; column < (int)arr.size(); column++) {
               add_for_first_diagonal += arr[column][first_diagonal]; 
               add_for_second_diagonal += arr[column][second_diagonal];
        }
          first_diagonal += row;
          second_diagonal -= row;
    }
      difference = first_diagonal - second_diagonal;
      result = abs(difference);
      return result;
}

Any help is appreciated. Thank you.

Marek Puchalski
  • 3,286
  • 2
  • 26
  • 35
somebody
  • 1
  • 2
  • 5
    Welcome to Stack Overflow! It sounds like you may need to learn how to use a debugger to step through your code. With a good debugger, you can execute your program line by line and see where it is deviating from what you expect. This is an essential tool if you are going to do any programming. Further reading: [How to debug small programs](http://ericlippert.com/2014/03/05/how-to-debug-small-programs/) and [Debugging Guide](http://idownvotedbecau.se/nodebugging/) – NathanOliver Jul 17 '20 at 20:00
  • 3
    On an unrelated note, whenever you feel the need to do a C-style cast (like you do with e.g. `(int)arr.size()`) you should take that as a sign that you're doing something wrong. Using C-style casts to silence warnings is the wrong solution. – Some programmer dude Jul 17 '20 at 20:03
  • The accesses to the matrix are probably wrong. It should be ```arr[row][column]``` – Lluís Alemany-Puig Jul 17 '20 at 20:03
  • 2
    [What is a debugger and how can it help me diagnose problems?](https://stackoverflow.com/q/25385173/5910058) – Jesper Juhl Jul 17 '20 at 20:14
  • @NathanOliver Thank you very much for the links Nathan. I will definitely check it out. – somebody Jul 18 '20 at 01:21
  • @Someprogrammerdude Ok got it. I will keep that in mind. thank you for the advice. – somebody Jul 18 '20 at 01:22
  • @Lluís oh ok I will look into it. Thank you Lluis. – somebody Jul 18 '20 at 01:24

1 Answers1

0

Well, you are accessing every element of the matrix, you only need the diagonals. So, from here, in spite of having a few more mistakes, they do not matter much.

This is probably the code you want (I haven't done any debugging). I hope it is right.

int diagonalDifference(const vector<vector<int>>& M) {
      const int n = static_cast<int>(M.size());
      int sum_1st_diag = 0;
      int sum_2nd_diag = 0;
      
      for (int row = 0; row < n; ++row) {
          sum_1st_diag += M[row][row];
          sum_2nd_diag += M[row][n - row - 1];
      }
      return abs(sum_1st_diag - sum_2nd_diag);
}
  • Hi, Thank you very much. Yes it works. However, I am kinda confused about why you use M[row][row] and M[row][n-row-1]. how does the second array [row] and [n-row-1] work? Again, thank you. – somebody Jul 18 '20 at 01:20
  • `n-row-1` might seem magic, but it's actually very logical: if row=0,1,2,..., then the expression `n-row-1` gives you `n-1, n-2, n-3, ...` which is precisely what you need in order to access the cells of the matrix that are part of the second diagonal, which are: `(0,n-1), (1,n-2), (2,n-3), ...`. A diagram might make this clearer. – Lluís Alemany-Puig Jul 18 '20 at 08:50
  • Ok got it. It does makes sense. Thank you. – somebody Jul 18 '20 at 13:48
  • Glad I could help. But this kind of problems better be tackled a little bit more thouroughly. I would recommend doing some drawings. Also, a few `cout`'s showing the values of the variables would have done wonders for finding out that the matrix was accessed in an incorrect manner for this problem. – Lluís Alemany-Puig Jul 18 '20 at 18:04