basics · beginner · ~15 min

Diagonal sum

Walk the diagonal (i, i) of a flattened square matrix.

Challenge

Implement long diag_sum(const int *m, int n) returning the sum of the main diagonal of an n x n row-major matrix.

Starter code

long diag_sum(const int *m, int n) {
    /* TODO */
    return 0;
}

Solve this exercise in the browser editor — compile and run against the test harness, no setup required.