- Back to Home »
- c++ , code , coding , matrix , program , project , project c++(basic) , transpose »
- Code to make transpose of matrix in C++ (algo)

Posted by : Waqas Javed
Thursday, 25 December 2014

This program demonstrate the conversion of matrix to its transpose using array and loops. For more detail on int, array, for loop and other basic commands see our other post on respective topic.

#include<iostream>

#include<conio.h>

using namespace std;

void main(){

int Matrix[50][50];

int transpose[50][50];

int x;

int y;

cout << "Enter No. of rows:";

cin >> x;

cout << "Enter No. of column:";

cin >> y;

for (int i = 0; i < x; i++){

for (int j = 0; j < y; j++){

cout << "please enter elements: ";

cin >> Matrix[i][j];

}

}

cout << "\nOrignal matrix:\n";

for (int i = 0; i < x; i++){

for (int j = 0; j < y; j++){

cout << Matrix[i][j] << '\t';

}

cout << endl;

}

for (int i = 0; i < x; i++){

for (int j = 0; j < y; j++){

transpose[j][i] = Matrix[i][j];

}

}

cout << "\nTranpose matrix:\n";

for (int i = 0; i < y; i++){

for (int j = 0; j < x; j++){

cout << transpose[i][j] << '\t';

}

cout << endl;

}

_getch();

}

#include<iostream>

#include<conio.h>

using namespace std;

void main(){

int Matrix[50][50];

int transpose[50][50];

int x;

int y;

cout << "Enter No. of rows:";

cin >> x;

cout << "Enter No. of column:";

cin >> y;

for (int i = 0; i < x; i++){

for (int j = 0; j < y; j++){

cout << "please enter elements: ";

cin >> Matrix[i][j];

}

}

cout << "\nOrignal matrix:\n";

for (int i = 0; i < x; i++){

for (int j = 0; j < y; j++){

cout << Matrix[i][j] << '\t';

}

cout << endl;

}

for (int i = 0; i < x; i++){

for (int j = 0; j < y; j++){

transpose[j][i] = Matrix[i][j];

}

}

cout << "\nTranpose matrix:\n";

for (int i = 0; i < y; i++){

for (int j = 0; j < x; j++){

cout << transpose[i][j] << '\t';

}

cout << endl;

}

_getch();

}