Matrices: think in two coordinates
Traverse tables, total rows and columns, inspect diagonals, and protect grid neighbours.
Rows and columns have separate bounds
int a[2][3] contains two rows of three cells each. a[1][2] is valid; a[2][1] is not. The first index selects the row, and the second selects a column in that row.
For active dimensions rows and columns, require 0 ≤ r < rows and 0 ≤ c < columns. a[0][3] is not a legal shortcut for a[1][0]. A complete traversal visits rows × columns cells, even if the physical capacity is larger.
Fix one coordinate, vary the other
For a row sum, keep the row fixed and visit all columns. For a column sum, keep the column fixed and visit all rows. Start a new long long sum for each new row or column.
To print a transpose, visit original columns in the outer loop and original rows in the inner loop. A 2 × 3 table becomes 3 × 2. Access remains a[r][c]; only the visiting order changes. Storing a transpose separately uses b[c][r] = a[r][c].
#include <iostream>
int main() {
int a[30][30]{};
int rows = 0, columns = 0;
std::cin >> rows >> columns; // Each dimension is 1..30.
for (int r = 0; r < rows; ++r) {
for (int c = 0; c < columns; ++c) { std::cin >> a[r][c]; }
}
for (int c = 0; c < columns; ++c) {
for (int r = 0; r < rows; ++r) {
if (r > 0) { std::cout << ' '; }
std::cout << a[r][c];
}
std::cout << '\n';
}
return 0;
}Two diagonals can share a centre
In a square n × n matrix, main-diagonal cells satisfy r == c. The other diagonal satisfies r + c == n − 1. Equivalently, its column in row r is n − 1 − r.
For odd n the diagonals share a centre cell. If printing two separate sums, the centre belongs in both. If summing distinct diagonal cells, count it once. One condition joined by || naturally includes each visited cell at most once.
Guard the four grid neighbours
A grid of characters can use an array of strings, one string per row. After converting a chosen one-based position to zero-based, inspect up, down, left, and right individually. Diagonals are excluded unless the task explicitly includes them.
Bounds must come first: r > 0 && grid[r − 1][c] == '.' prevents an invalid upward access at row 0. Test a 1 × 1 grid, a single row, a single column, and a nonsquare grid to expose swapped bounds.
In a 5 × 5 matrix, which cell belongs to both diagonals?
Turn the idea into code
For 1 ≤ n ≤ 30 and matrix values in [−10⁹, 10⁹], sum all distinct cells on either diagonal. For the 3 × 3 matrix containing 1 through 9 by rows, print 25.
Give me a hint
Use one if condition: r == c OR r + c == n − 1.
Show the explained solution
The shared centre enters one addition, because a single true condition performs one update. A long long safely holds the sum under these bounds.
#include <iostream>
int main() {
int n = 0;
std::cin >> n;
int a[30][30]{};
for (int r = 0; r < n; ++r) {
for (int c = 0; c < n; ++c) { std::cin >> a[r][c]; }
}
long long sum = 0;
for (int r = 0; r < n; ++r) {
for (int c = 0; c < n; ++c) {
if (r == c || r + c == n - 1) { sum += a[r][c]; }
}
}
std::cout << sum << '\n';
return 0;
}Your notes
Mark complete when you can explain it yourself.