Lesson 3

Hello there! Are you ready to enhance your **C++** programming skills with another exciting exercise? In this unit, we are plunging into the world of **matrices**. More specifically, we'll be transposing a given matrix. Let's dive into this matrix manipulation game without delay!

To begin, let's elaborate on the task at hand. You are required to write a C++ function named `transformMatrix()`

. This function will accept a 2D `std::vector`

(which represents a matrix) that contains integers as inputs. Your responsibility is to return another 2D `std::vector`

, which is the transposed version of the given matrix.

Remember, when we mention 'transposing a matrix,' we are referring to the process of switching its rows and columns. In other words, all the rows of the original matrix should convert into columns in the transposed matrix, and vice versa.

For instance, if the original matrix (input 2D `std::vector`

) is:

C++`1std::vector<std::vector<int>> matrix = { 2 {1, 2, 3}, 3 {4, 5, 6} 4};`

Then the transposed matrix (output 2D `std::vector`

) will be:

C++`1std::vector<std::vector<int>> transposed = { 2 {1, 4}, 3 {2, 5}, 4 {3, 6} 5};`

It is vital for your result to maintain the integrity of the data type that is present in the original matrix. In layman's terms, the values seen in the input matrix are integers, and they should be integers in the output matrix as well.

The initial step of building our solution involves determining the dimensions of the matrix. We need to know the number of rows and columns present in it. In C++, the `.size()`

method of `std::vector`

can offer this information. The number of rows is simply the size of the matrix (the outer vector), and the number of columns matches the size of any of the inner vectors.

C++`1#include <iostream> 2#include <vector> 3 4std::vector<std::vector<int>> transformMatrix(const std::vector<std::vector<int>>& matrix) { 5 int rows = matrix.size(); 6 int cols = rows > 0 ? matrix[0].size() : 0; 7}`

The subsequent step is to create a "placeholder" for the transposed matrix that is in alignment with its required dimensions. This will be a new 2D `std::vector`

, but with the number of rows and columns swapped. Initially, we can populate this matrix with all zeros.

C++`1#include <iostream> 2#include <vector> 3 4std::vector<std::vector<int>> transformMatrix(const std::vector<std::vector<int>>& matrix) { 5 int rows = matrix.size(); 6 int cols = rows > 0 ? matrix[0].size() : 0; 7 std::vector<std::vector<int>> result(cols, std::vector<int>(rows, 0)); 8}`

It's time to get to the crux of the matter — transposing the matrix. For every element in the original matrix, we want to move it from the `i`

th row and `j`

th column to the `j`

th row and `i`

th column of the transposed matrix. A straightforward nested for-loop can effortlessly execute this swap for all elements of the matrix.

C++`1#include <iostream> 2#include <vector> 3 4std::vector<std::vector<int>> transformMatrix(const std::vector<std::vector<int>>& matrix) { 5 int rows = matrix.size(); 6 int cols = rows > 0 ? matrix[0].size() : 0; 7 std::vector<std::vector<int>> result(cols, std::vector<int>(rows, 0)); 8 9 for (int i = 0; i < rows; ++i) { 10 for (int j = 0; j < cols; ++j) { 11 result[j][i] = matrix[i][j]; 12 } 13 } 14 15 return result; 16} 17 18 19int main() { 20 // Initial matrix (2D vector) 21 std::vector<std::vector<int>> matrix = { 22 {1, 2, 3}, 23 {4, 5, 6} 24 }; 25 26 // Call our function on the matrix and output the result 27 std::vector<std::vector<int>> transposed = transformMatrix(matrix); 28 for (const auto& row : transposed) { 29 for (int elem : row) { 30 std::cout << elem << " "; 31 } 32 std::cout << std::endl; 33 } 34 return 0; 35}`

This step concludes our solution!

With this, we've brought this lesson to a close! Congratulations on successfully implementing a function that can transpose matrices! This task is not simple, but by accomplishing it, you've exhibited your understanding of and proficiency in **C++**'s `std::vector`

, for-loops, and the concept of matrix transposition.

Your hard work continues though. Now that you have acquired this valuable skill, it's time to reinforce it with more practice. During the next session, you will encounter practice problems that build on this concept. So, get ready and start coding!