I need to calculate the sum of two diagonals in a matrix in C++, I already have a solution for that but I must be dumb because I cant understand what it is doing, so I would
you must use i + j == n + 1 instead of i + j == n - 1 for secondary diagonal i.e
i + j == n + 1
i + j == n - 1
for(i = 1; i <= n; i++) { for(j = 1; j <= n; j++) { if(i == j) d += a[i][j]; //principal diagonal if(i + j == n+1) s += a[i][j];//secondary diagonal } }