forked from MAYANK25402/Hactober-2023-1
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #301 from irvn0x/main
add transpose matrix in c++
- Loading branch information
Showing
1 changed file
with
36 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
#include <iostream> | ||
using namespace std; | ||
|
||
const int MAX = 10; | ||
|
||
void transposeMatrix(int matrix[MAX][MAX], int row, int col) { | ||
int transposed[MAX][MAX]; | ||
|
||
// Moving matrix elements to the transposed matrix. | ||
for (int i = 0; i < row; i++) { | ||
for (int j = 0; j < col; j++) { | ||
transposed[j][i] = matrix[i][j]; | ||
} | ||
} | ||
|
||
// Displaying the transposed matrix. | ||
cout << "Transposed Matrix:" << endl; | ||
for (int i = 0; i < col; i++) { | ||
for (int j = 0; j < row; j++) { | ||
cout << transposed[i][j] << " "; | ||
} | ||
cout << endl; | ||
} | ||
} | ||
|
||
int main() { | ||
int row = 3, col = 3; | ||
int matrix[MAX][MAX] = {{1, 2, 3}, | ||
{4, 5, 6}, | ||
{7, 8, 9}}; | ||
|
||
// Calling a function to perform the transpose. | ||
transposeMatrix(matrix, row, col); | ||
|
||
return 0; | ||
} |