Beginner C/C++ Exercises

Transpose a Matrix in C and C++

Turn rows into columns: the value at [i][j] moves to [j][i].

  • Beginner
  • 2 min read
  • 2 runnable examples
What you'll learn
  • Nested loops over a 2D array
  • Swapping row and column indexes
  • Printing the transposed matrix
Transpose of [[1 2 3] [4 5 6]]
1
4
2
5
3
6
The second row 4 5 6 is now the second column.

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.

From a seating chart to code
matrix[2][3]🪑 2 rows of 3 seats
for (int column = 0; ...)↕️ Read down each column
matrix[row][column]👤 The student in that seat
1 4 / 2 5 / 3 6🔄 Columns printed as rows
Swap the roles of rows and columns.
In one sentence

The 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

  1. matrix has 2 rows and 3 columns.
  2. The outer loop walks column and the inner loop walks row, reversing the usual order.
  3. Each output line prints one original column: 1 4, then 2 5, then 3 6.
  4. The original matrix is 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

C
Run code →
main.c
#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(""); }
}
Get a Run button for this example Result of running the code
1 4
2 5
3 6
C++
Run code →
main.cpp
#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'; }
}
Get a Run button for this example Result of running the code
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

C exposes small procedural APIs and makes representation details explicit.

C++

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.

Problem
matrix[column][row] = matrix[row][column];
Fix
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.

All challenges