Skip to content

Matrix transpose

C++ 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

C++
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;
        }
    }
}

The editor opens with this code — change it and the chart redraws.

Language
C++
Constructs
nested loops, swap
Topic
Loops
Formats
Word, Visio, draw.io, PDF, SVG

Related examples