"program to swap max and min in matrix" Code Answer's
You're definitely familiar with the best coding language C++ that developers use to develop their projects and they get all their queries like "program to swap max and min in matrix" answered properly. Developers are finding an appropriate answer about program to swap max and min in matrix related to the C++ coding language. By visiting this online portal developers get answers concerning C++ codes question like program to swap max and min in matrix. Enter your desired code related query in the search bar and get every piece of information about C++ code related question on program to swap max and min in matrix.
program to swap max and min in matrix
#include <stdio.h>
int main(void)
{
int rows, cols;
do
{
printf("Give me the number of rows :");
scanf("%d",&rows);
}while(rows<1);
do
{
printf("Give me the number of cols :");
scanf("%d",&cols);
}while(cols<1);
int arr[rows][cols];
printf("\nThe filling of the Matrix :\n");
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
{
scanf("%d", &arr[i][j]);
}
}
int i_max = 0,j_max=0;
int i_min = 0,j_min=0;
int max=arr[0][0];
int min=arr[0][0];
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
{
if (arr[i][j]>max)
{
max=arr[i][j];
i_max=i;// i_max mean's the index i of the maximum element
j_max=j;// j_max mean's the index j of the maximum element
}
if(arr[i][j]<min)
{
min=arr[i][j];
i_min=i;// i_max mean's the index i of the minimum element
j_min=j;// j_max mean's the index j of the minimum element
}
}
}
printf("\nDisplay of the Matrix before the swap :\n");
for (int i = 0; i < rows; i++)
{printf("\n");
for (int j = 0; j < cols; j++)
{
printf("[%d]", arr[i][j]);
}
}
int temp=arr[i_min][j_min];
arr[i_min][j_min]=arr[i_max][j_max];
arr[i_max][j_max]=temp;
printf("\n\nDisplay of the Matrix after the swap :\n");
for (int i = 0; i < rows; i++)
{printf("\n");
for (int j = 0; j < cols; j++)
{
printf("[%d]", arr[i][j]);
}
}
return 0;
}
Source: stackoverflow.com
All those coders who are working on the C++ based application and are stuck on program to swap max and min in matrix can get a collection of related answers to their query. Programmers need to enter their query on program to swap max and min in matrix related to C++ code and they'll get their ambiguities clear immediately. On our webpage, there are tutorials about program to swap max and min in matrix for the programmers working on C++ code while coding their module. Coders are also allowed to rectify already present answers of program to swap max and min in matrix while working on the C++ language code. Developers can add up suggestions if they deem fit any other answer relating to "program to swap max and min in matrix". Visit this developer's friendly online web community, CodeProZone, and get your queries like program to swap max and min in matrix resolved professionally and stay updated to the latest C++ updates.