← All examples

Matrix transpose

C++ Loops

nested loops, swap

Flowchart (ISO 5807)

StartInput a[][100], ni = 0, n - 1, 1j = i + 1, n - 1, 1t = a[i][j]a[i][j] = a[j][i]a[j][i] = tEndFigure 1 — transpose

Source code

void transpose(int a[][100], int n) {
    for (int i = 0; i < n; i++) {
        for (int j = i + 1; j < n; j++) {
            int t = a[i][j];
            a[i][j] = a[j][i];
            a[j][i] = t;
        }
    }
}