#include <iostream>#include <vector>void rotateMatrix(int** matrix, int n) { // Transpose the matrix for (int i = 0; i < n; i++) { for (int j = i; j < n; j++) { std::swap(matrix[i][j], matrix[j][i]); } } // Reverse each row for (int i = 0; i < n; i++) { int left = 0; int right = n - 1; while (left < right) { std::swap(matrix[i][left], matrix[i][right]); left++; right--; } }}void printMatrix(int** matrix, int n) { for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { std::cout << matrix[i][j] << ' '; } std::cout << std::endl; }}int main() { int n; std::cin >> n; if (n <= 0) { std::cout << "Size of the matrix should be positive!" << std::endl; return 0; } // Allocate dynamic memory for the matrix int** matrix = new int*[n]; for (int i = 0; i < n; i++) { matrix[i] = new int[n]; } // Read the matrix elements for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { std::cin >> matrix[i][j]; } } std::cout << "Original Matrix:" << std::endl; printMatrix(matrix, n); rotateMatrix(matrix, n); std::cout << "Matrix after 90-degree clockwise rotation:" << std::endl; printMatrix(matrix, n); // Deallocate the dynamic memory for (int i = 0; i < n; i++) { delete[] matrix[i]; } delete[] matrix; return 0;}
Question
#include <iostream>#include <vector>void rotateMatrix(int** matrix, int n) { // Transpose the matrix for (int i = 0; i < n; i++) { for (int j = i; j < n; j++) { std::swap(matrix[i][j], matrix[j][i]); } } // Reverse each row for (int i = 0; i < n; i++) { int left = 0; int right = n - 1; while (left < right) { std::swap(matrix[i][left], matrix[i][right]); left++; right--; } }}void printMatrix(int** matrix, int n) { for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { std::cout << matrix[i][j] << ' '; } std::cout << std::endl; }}int main() { int n; std::cin >> n; if (n <= 0) { std::cout << "Size of the matrix should be positive!" << std::endl; return 0; } // Allocate dynamic memory for the matrix int** matrix = new int*[n]; for (int i = 0; i < n; i++) { matrix[i] = new int[n]; } // Read the matrix elements for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { std::cin >> matrix[i][j]; } } std::cout << "Original Matrix:" << std::endl; printMatrix(matrix, n); rotateMatrix(matrix, n); std::cout << "Matrix after 90-degree clockwise rotation:" << std::endl; printMatrix(matrix, n); // Deallocate the dynamic memory for (int i = 0; i < n; i++) { delete[] matrix[i]; } delete[] matrix; return 0;}
Solution
This is a C++ program that reads a square matrix of size 'n' from the user, rotates it 90 degrees clockwise, and then prints the rotated matrix. Here's a step-by-step explanation:
-
The program starts with the
main()function. It reads an integer 'n' from the user, which represents the size of the square matrix. If 'n' is not positive, it prints an error message and terminates. -
It then dynamically allocates memory for a 2D array (the matrix) using the 'new' operator.
-
The program reads 'n' x 'n' integers from the user and stores them in the matrix.
-
It prints the original matrix by calling the
printMatrix()function. -
The
rotateMatrix()function is called to rotate the matrix 90 degrees clockwise. This function first transposes the matrix (i.e., swaps rows with columns) and then reverses each row. -
The rotated matrix is printed.
-
Finally, the program deallocates the memory allocated for the matrix using the 'delete' operator and terminates.
This program uses the <iostream> and <vector> libraries for input/output and dynamic array handling, respectively. It also uses the std::swap() function to swap two elements in the matrix.
Similar Questions
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.