basics · beginner · ~15 min

Index a flattened matrix

Convert 2-D indices to a 1-D offset (r*cols + c).

Challenge

A rows x cols matrix is stored row-major in a flat array. Implement int flat_get(const int *m, int cols, int r, int c) returning element (r, c).

Starter code

int flat_get(const int *m, int cols, int r, int c) {
    /* TODO */
    return 0;
}

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