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.
Aucun commentaire:
Enregistrer un commentaire