program to swap max and min in matrix
Here is a C++ program that swaps the maximum and minimum values in a matrix:
#include <iostream>
// Function to find the maximum and minimum values in a matrix
void findMaxMin(int matrix[][3], int rows, int cols, int& maxVal, int& minVal) {
maxVal = matrix[0][0]; // Assume the first element as maximum
minVal = matrix[0][0]; // Assume the first element as minimum
// Traverse through the matrix and update maximum and minimum values
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
if (matrix[i][j] > maxVal) {
maxVal = matrix[i][j];
}
if (matrix[i][j] < minVal) {
minVal = matrix[i][j];
}
}
}
}
// Function to swap the maximum and minimum values in a matrix
void swapMaxMin(int matrix[][3], int rows, int cols) {
int maxVal, minVal;
// Find the maximum and minimum values in the matrix
findMaxMin(matrix, rows, cols, maxVal, minVal);
// Find the positions of the maximum and minimum values
int maxRow = 0, maxCol = 0, minRow = 0, minCol = 0;
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
if (matrix[i][j] == maxVal) {
maxRow = i;
maxCol = j;
}
if (matrix[i][j] == minVal) {
minRow = i;
minCol = j;
}
}
}
// Swap the maximum and minimum values
int temp = matrix[maxRow][maxCol];
matrix[maxRow][maxCol] = matrix[minRow][minCol];
matrix[minRow][minCol] = temp;
}
// Function to display the matrix
void displayMatrix(int matrix[][3], int rows, int cols) {
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
std::cout << matrix[i][j] << " ";
}
std::cout << std::endl;
}
}
int main() {
int matrix[3][3] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
std::cout << "Original matrix:" << std::endl;
displayMatrix(matrix, 3, 3);
// Swap the maximum and minimum values in the matrix
swapMaxMin(matrix, 3, 3);
std::cout << "Matrix after swapping max and min:" << std::endl;
displayMatrix(matrix, 3, 3);
return 0;
}
Explanation:
The program starts by including the necessary header file
iostream
for input and output operations.The
findMaxMin
function is defined to find the maximum and minimum values in a matrix. It takes the matrix, number of rows, number of columns, and two reference variablesmaxVal
andminVal
.Inside the
findMaxMin
function, we assume the first element of the matrix as both the maximum and minimum values. Then, we traverse through the matrix using nested loops to compare each element with the current maximum and minimum values. If a larger element is found, we updatemaxVal
, and if a smaller element is found, we updateminVal
.The
swapMaxMin
function is defined to swap the maximum and minimum values in the matrix. It takes the matrix, number of rows, and number of columns as input.Inside the
swapMaxMin
function, we call thefindMaxMin
function to find the maximum and minimum values in the matrix. Then, we traverse through the matrix again to find the positions (row and column indices) of the maximum and minimum values.Finally, we swap the maximum and minimum values by storing the maximum value in a temporary variable
temp
, assigning the minimum value to the position of the maximum value, and assigning the maximum value to the position of the minimum value.The
displayMatrix
function is defined to display the matrix. It takes the matrix, number of rows, and number of columns as input. Inside the function, we traverse through the matrix and print each element.In the
main
function, we declare a 3x3 matrixmatrix
and initialize it with some values.We then display the original matrix using the
displayMatrix
function.Next, we call the
swapMaxMin
function to swap the maximum and minimum values in the matrix.Finally, we display the matrix after swapping the maximum and minimum values using the
displayMatrix
function.
This program finds the maximum and minimum values in a matrix and swaps them, resulting in the matrix with the maximum and minimum values swapped.