In various fields of mathematics and computer science, manipulating matrices is a crucial task. Shifting rows within a matrix is a commonly executed procedure. This action is beneficial for reorganizing data and enhancing calculations, among other functions.
Introduction to Matrix:
A matrix refers to a two-dimensional arrangement of numbers structured in rows and columns. This format is commonly employed for mathematical computations and data depiction. Columns are aligned vertically, while rows are positioned horizontally within a traditional matrix.
For example, consider 3*3 matrix:
1 3 5
5 2 7
1 6 4
Algorithm: Matrix Row Swapping
A 2D array with dimensions M*N is provided.
To perform the swap operation, specify the row indices as row1 and row2.
- The matrix mat with its first and second rows interchanged.
Step 1:
- Validate the input indices to ensure they are within the acceptable range.
- Verify that rows 1 and 2 fall within the permissible range of 0 to M-1 (where M represents the total number of rows in the matrix).
Step 2:
- Swap the rows.
- Create a temporary row or array called temp to save the values of row row1 .
- Perform the following actions for each column col between 0 and N-1 (where N is the number of columns in the matrix):
- Assign row row1 values to temp[col] = matrow1.
- Assign matrow1 = matrow2 to replicate the data from row row2 to row row1.
- Assign matrow2 = temp[col] to duplicate the values from temp to row row2 .
Step 3:
- Return the modified matrix mat.
Pseudo Code:
function swapRows(matrix mat, int row1, int row2):
if row1 and row2 are valid indices in mat:
// Create a temporary array to store row1 values
array temp of size N // N is the number of columns in the matrix
// Copy row1 to temp and row2 to row1
for col from 0 to N-1:
temp[col] = mat[row1][col]
mat[row1][col] = mat[row2][col]
// Copy temp (original row1) to row2
for col from 0 to N-1:
mat[row2][col] = temp[col]
return mat
Program:
Let's take an C++ program to swap rows of matrix:
#include <iostream>
// Function to swap two rows of a matrix
void swapRows(int matrix[][3], int row1, int row2) {
int temp;
for (int i = 0; i < 3; i++) {
// Swap the elements of row1 and row2
temp = matrix[row1][i];
matrix[row1][i] = matrix[row2][i];
matrix[row2][i] = temp;
}
}
int main() {
int matrix[3][3] = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
};
int row1, row2;
// Input rows to swap
std::cout << "Enter the rows to swap (0 to 2): ";
std::cin >> row1 >> row2;
if (row1 >= 0 && row1 < 3 && row2 >= 0 && row2 < 3) {
// Call the swapRows function
swapRows(matrix, row1, row2);
// Display the matrix after swapping
std::cout << "Matrix after swapping rows " << row1 << " and " << row2 << ":\n";
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
std::cout << matrix[i][j] << " ";
}
std::cout << "\n";
}
} else {
std::cout << "Invalid row numbers. Please enter rows between 0 and 2.\n";
}
return 0;
}
Output:
Code Explanation:
Step 1:
- Include Necessary Header.
- The essential header for the C++ standard library is added at the beginning of the code. Functions related to input and output rely on this header file.
Step 2:
- After that, define the swapRows Function .
- A matrix's two rows can be switched using the swapRows method.
- It requires two-row indices (row1 and row2), which indicate the rows to be switched, and a 2D matrix (matrix) of size 3x3.
- Begin by setting up the Main Function.
- In the main function, a 3x3 matrix is defined and initialized with specific values. This matrix serves as the data that will be manipulated in the program.
Step 4:
- Specify the Row Numbers for Exchange.
- In case the user intends to interchange rows 1 and 2, the application prompts for these specific numbers. The variables store the user-provided data.
Step 5:
- Check Input Row Numbers for Accuracy.
- The software validates the correctness of the provided row numbers. To be deemed valid, both row1 and row2 must fall within the acceptable range of 0 to 2 in a 3x3 matrix.
Step 6:
- Execute the swapRows Function.
- Invoking the swapRows method occurs when the row numbers are confirmed to be valid. This method facilitates the interchange of rows by traversing through the columns and temporarily holding and swapping the elements of the two designated rows.
Display the Adjusted Matrix:
The software presents the revised matrix following the horizontal flipping of rows. By looping through the rows and columns of the matrix using nested loops, each item is displayed. At this point, the user can observe the result of the row interchange operation.
Handle Invalid Row Numbers.
When the user inputs invalid row numbers, the application will alert them about the issue and prompt them to enter row numbers within the range of 0 to 2.
Step 9:
- Terminate the program by returning 0.
- Exiting the program occurs when the main function returns 0, indicating successful execution.