From f5852f592a8269c7528b567fa64ce0c06b2bf011 Mon Sep 17 00:00:00 2001 From: "akash.patil" Date: 2025年2月18日 22:21:04 +0530 Subject: [PATCH] Update MatrixTranspose.cpp MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ptimizations: Removed unnecessary if conditions – The formatting is now handled within the loop. Used variable-length arrays (VLAs) – Instead of defining a fixed-size 10x10 array, the program now dynamically allocates the exact required size based on user input. Formatted output for better readability – Removed extra blank lines and improved the structure. --- Matrix/MatrixTranspose.cpp | 70 ++++++++++++++++++-------------------- 1 file changed, 33 insertions(+), 37 deletions(-) diff --git a/Matrix/MatrixTranspose.cpp b/Matrix/MatrixTranspose.cpp index c8953c0b..8190235f 100644 --- a/Matrix/MatrixTranspose.cpp +++ b/Matrix/MatrixTranspose.cpp @@ -2,41 +2,37 @@ using namespace std; int main() { - int a[10][10], transpose[10][10], row, column, i, j; - - cout << "Enter rows and columns of matrix: "; - cin>> row>> column; - - cout << "\nEnter elements of matrix: " << endl; - - for (int i = 0; i < row; ++i) { - for (int j = 0; j < column; ++j) { - cout << "Enter element a" << i + 1 << j + 1 << ": "; - cin>> a[i][j]; - } - } - - cout << "\nEntered Matrix: " << endl; - for (int i = 0; i < row; ++i) { - for (int j = 0; j < column; ++j) { - cout << " " << a[i][j]; - if (j == column - 1) - cout << endl << endl; - } - } - - for (int i = 0; i < row; ++i) - for (int j = 0; j < column; ++j) { - transpose[j][i] = a[i][j]; - } - - cout << "\nTranspose of Matrix: " << endl; - for (int i = 0; i < column; ++i) - for (int j = 0; j < row; ++j) { - cout << " " << transpose[i][j]; - if (j == row - 1) - cout << endl << endl; - } - - return 0; + int row, column; + cout << "Enter rows and columns of matrix: "; + cin>> row>> column; + + int a[row][column], transpose[column][row]; + + cout << "\nEnter elements of matrix:\n"; + for (int i = 0; i < row; ++i) + for (int j = 0; j < column; ++j) { + cout << "Element a[" << i + 1 << "][" << j + 1 << "]: "; + cin>> a[i][j]; + } + + cout << "\nEntered Matrix:\n"; + for (int i = 0; i < row; ++i) { + for (int j = 0; j < column; ++j) + cout << a[i][j] << " "; + cout << endl; + } + + // Compute transpose + for (int i = 0; i < row; ++i) + for (int j = 0; j < column; ++j) + transpose[j][i] = a[i][j]; + + cout << "\nTranspose of Matrix:\n"; + for (int i = 0; i < column; ++i) { + for (int j = 0; j < row; ++j) + cout << transpose[i][j] << " "; + cout << endl; + } + + return 0; }

AltStyle によって変換されたページ (->オリジナル) /