hi,
the solution you’re giving:
int add_diagonal(int arr[3][3], int row, int col) {
int sum = 0;
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++) {
if (i == j) {
sum = sum + arr[i][j];
}
}
}
return sum;
}
doesn’t need nested loops and a condition.
a more efficient solution is just to do:
int add_diagonal(int arr[3][3], int row, int col) {
int sum = 0;
for (int i = 0; i < row; i++) sum += arr[i][i];
return sum;
}
since you need a diagonal sum, rows and columns will always have to be the same, so they can use the same index.
Type your question above this line.
Course: https://www.educative.io/collection/10370001/6619096843026432
Lesson: https://www.educative.io/collection/page/10370001/6619096843026432/4898883168632832