# Find the transpose of a matrix in place of 3×3 matrix in C++

In this C++ program, we are going to find the transpose of a given matrix in place with simple array commands and nested loop. This problem is based on the application of array which has many applications. Following is a short and easy solution to perform this task and complete source code is also available.

## How to find the transpose of a given matrix in C++

**Problem statement**

There is a matrix of size 3×3 ( 2D array). Find the transpose of that matrix.

**Definition**

The transpose of a matrix is defined as a matrix formed my interchanging all rows with their corresponding column and vice versa of previous matrix.

Consider the following example-

**Problem approach**

- Initialize a 2D array to work as matrix.
- Store values in it.
- Find transpose by using logic.
- Print output on screen.

**Program/Source code**

Following is the program code to **find trace and normal of a matrix**. It is written and successfully compiled in CodeBlocks v 16.01 in windows 10.

/* C++ program to transpose of a given matrix** ** enter matrix values of your choice** ***enter different values for different results*** note: No other matrix is declared and same matrix is transposed Enter the array(matrix) values within array limit only.... */ #include <iostream> using namespace std; int main() { int matrix[3][3]={{43,23,55},{65,76,32},{33,51,22}}; //array declaration and values // change array values here int temp; cout<<"Given matrix is-"<<endl; for(int i=0;i<3;i++){ // print given matrix for(int j=0;j<3;j++) cout<<matrix[i][j]<<"\t"; cout<<endl; } cout<<endl; for(int i=0;i<3;i++){ // transpose for(int j=i;j<3;j++){ //NESTED loop temp=matrix[i][j]; //swap variables matrix[i][j]=matrix[j][i]; matrix[j][i]=temp; } } cout<<"Transpose of given matrix is-"<<endl; for(int i=0;i<3;i++){ //print transpose of matrix for(int j=0;j<3;j++) cout<<matrix[i][j]<<"\t"; cout<<endl; } return 0; }

**Output**

Given matrix is- 43 23 55 65 76 32 33 51 22 Transpose of given matrix is- 43 65 33 23 76 51 55 32 22 Process returned 0 (0x0) execution time : 4.607 s Press any key to continue.

**Program explanation**

- Initialize an integer array (2D) variable “
**marix**“. - Store value in it.
- Print the initial values using nested for loop.
- Transpose of that matrix in calculated by using following logic
for(int i=0;i<3;i++){ // transpose for(int j=i;j<3;j++){ //NESTED loop temp=matrix[i][j]; //swap variables matrix[i][j]=matrix[j][i]; matrix[j][i]=temp; } }

All the corresponding rows and columns are interchanged using nested for loop.

- Print the matrix using the same logic as in point no.3

Also, read

## Leave a Reply