#include <iostream>
using namespace std;
int main() {
// Initialize a 2x2 matrix
int matrix[2][2];
// Input the matrix elements
cout << "Enter elements of 2x2 matrix:\n";
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 2; j++) {
cout << "Element [" << i << "][" << j << "]: ";
cin >> matrix[i][j];
}
}
// Display the original matrix
cout << "\nOriginal Matrix:\n";
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 2; j++) {
cout << matrix[i][j] << " ";
}
cout << endl;
}
// Calculate and display the transpose of the matrix
cout << "\nTranspose of the Matrix:\n";
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 2; j++) {
cout << matrix[j][i] << " ";
}
cout << endl;
}
return 0;
}
#include <iostream>
const int MAX_SIZE = 100; // Define maximum size for simplicity
// Function to input matrix elements
void inputMatrix(int mat[][MAX_SIZE], int rows, int cols)
{
cout << "Enter matrix elements:" << endl;
for (int i = 0; i < rows; ++i) {
for (int j = 0; j < cols; ++j) {
cin >> mat[i][j];
}
}
}
// Function to display a matrix
void displayMatrix(int mat[][MAX_SIZE], int rows, int cols)
{
cout << "Matrix:" << endl;
for (int i = 0; i < rows; ++i) {
for (int j = 0; j < cols; ++j) {
cout << mat[i][j] << " ";
}
cout <<endl;
}
}
// Function to find the transpose of a matrix
void transposeMatrix(int mat[][MAX_SIZE], int transposed[][MAX_SIZE], int rows, int cols)
{
for (int i = 0; i < rows; ++i)
{
for (int j = 0; j < cols; ++j)
{
transposed[j][i] = mat[i][j];
}
}
}
int main() {
int rows, cols;
int matrix[MAX_SIZE][MAX_SIZE]; // Original matrix
int transposed[MAX_SIZE][MAX_SIZE]; // Transposed matrix
// Input matrix dimensions
cout << "Enter number of rows and columns: ";
cin >> rows >> cols;
// Ensure dimensions are within limits
if (rows > MAX_SIZE || cols > MAX_SIZE) {
cout << "Dimensions exceed maximum size of " << MAX_SIZE << endl;
return 1;
}
// Input matrix values
inputMatrix(matrix, rows, cols);
// Find the transpose of the matrix
transposeMatrix(matrix, transposed, rows, cols);
// Display original matrix
cout << "Original ";
displayMatrix(matrix, rows, cols);
// Display transposed matrix
cout << "Transposed ";
displayMatrix(transposed, cols, rows);
return 0;
}
Another Method