Search
 
SCRIPT & CODE EXAMPLE
 

CPP

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("
The filling of the Matrix :
");
    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("
Display of the Matrix before the swap :
");
    for (int i = 0; i < rows; i++)
    {printf("
");
        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("

Display of the Matrix after the swap :
");

    for (int i = 0; i < rows; i++)
    {printf("
");
        for (int j = 0; j < cols; j++)
        {
            printf("[%d]", arr[i][j]);
        }
    }
    return 0;
}
Comment

PREVIOUS NEXT
Code Example
Cpp :: c++ get data type 
Cpp :: z transfrom mathlab 
Cpp :: c++ if statement 
Cpp :: if argv == string 
Cpp :: copy constructor c++ syntax 
Cpp :: string in c++ 
Cpp :: rethrow exception c++ 
Cpp :: iterate const vector 
Cpp :: c++ garbage collection 
Cpp :: Reverse a linked list geeksforgeeks in c++ 
Cpp :: c++ call by value 
Cpp :: flag of georgia 
Cpp :: cpp malloc 
Cpp :: pow without math.h 
Cpp :: how can I delete a substring from a string in c++? 
Cpp :: DSA 2. Complexity Analysis Google drive Educative excellent courses!!!! [Educative.io] Competitive Programming in C++ The Keys to Success 
Cpp :: memset in cpp 
Cpp :: cin c++ 
Cpp :: ex: cpp 
Cpp :: c++ threadpool 
Cpp :: c++ itoa 
Cpp :: cpp compare strings 
Cpp :: cuda shared array 
Cpp :: c++ how to do a pointer char to take varols from keyboard 
Cpp :: why the << operator is friend 
Cpp :: c++ online 
Cpp :: c++ throe 
Cpp :: practice problems for nested loops in c++ 
Cpp :: move semantics in c++ 
Cpp :: sideways triangle c++ xy plane 
ADD CONTENT
Topic
Content
Source link
Name
1+6 =