Transpose a Matrix in C and C++
Turn rows into columns: the value at [i][j] moves to [j][i].
- Nested loops over a 2D array
- Swapping row and column indexes
- Printing the transposed matrix
Rows become columns
The problem
Spreadsheets, images and math libraries often need rows turned into columns. Transposing a matrix is the basic operation, and it practices nested loops over two indexes.
Think of it like this
Transposing a matrix is like turning a class photo seating chart on its side: the first row of students becomes the first column. Nobody changes seats relative to each other; only the direction you read in changes.
matrix[2][3]🪑 2 rows of 3 seatsfor (int column = 0; ...)↕️ Read down each columnmatrix[row][column]👤 The student in that seat1 4 / 2 5 / 3 6🔄 Columns printed as rowsThe transpose of a matrix turns its rows into columns, so the element at row r, column c moves to row c, column r.
Important points
matrixhas 2 rows and 3 columns.- The outer loop walks
columnand the inner loop walksrow, reversing the usual order. - Each output line prints one original column: 1 4, then 2 5, then 3 6.
- The original
matrixis not changed; it is only read in a different order.
When to use it
- Matrix math and linear algebra.
- Turning table rows into columns, for example in reports.
- Practicing nested loops with two indexes.
When not to use it
- Storing the transposed result: create a new array of size [3][2].
- Large matrices in numeric work: use a library such as Eigen.
See how it works
Press transpose to swap rows and columns. Point at a cell to see its new position and memory offset.
C and C++ code examples
#include <stdio.h>
int main(void) {
int matrix[2][3] = {{1, 2, 3}, {4, 5, 6}};
// Visit columns first to print the transposed shape.
for (int column = 0; column < 3; ++column) { for (int row = 0; row < 2; ++row) printf("%s%d", row ? " " : "", matrix[row][column]); puts(""); }
}
1 4
2 5
3 6
#include <array>
#include <iostream>
int main() {
std::array<std::array<int, 3>, 2> matrix{{{{1, 2, 3}}, {{4, 5, 6}}}};
// Visit columns first to print the transposed shape.
for (size_t column = 0; column < 3; ++column) { for (size_t row = 0; row < 2; ++row) std::cout << (row ? " " : "") << matrix[row][column]; std::cout << '\n'; }
}
1 4
2 5
3 6
C and C++ comparison
The outer loop goes through the columns and the inner loop through the rows, so each printed line contains one column of the original. The first line is 1 4, the second 2 5 and the third 3 6. The matrix itself is only read, never changed.
C exposes small procedural APIs and makes representation details explicit.
C++ retains the low-level model while adding safer library types and abstractions.
Practice exercises
Run both versions, then modify them to observe the different language guarantees.
- Add invalid and boundary-value inputs.
- Compile with -Wall -Wextra -Wpedantic.
- Move reusable declarations into a header and implementation file.
Common mistakes
Transposing a non-square matrix in place
A 2 by 3 matrix does not fit in its own 2 by 3 array when transposed.
matrix[column][row] = matrix[row][column];
int transposed[3][2];
transposed[column][row] = matrix[row][column];
Finished this lesson?
Mark it as learned to track your progress.
Practice with a challenge
Use what this lesson covers to solve a real problem. Each challenge runs in your browser with instant tests and an AI review.
- Row Sums Add up every row of a grid. Easy
- Matrix Multiplication Multiply two matrices, the operation behind every layer of a neural network. Easy