In: Computer Science
USE C PROGRAMMING, call all functions in main, and use a 2 by 3 2d array to test, thanks.
get_location_of_min This function takes a matrix as a 2-D array of integers with NUM_COLS width, the number of rows in the matrix and two integer pointers. The function finds the location of the minimum value in the matrix and stores the row and column of that value to the memory location pointed to by the pointer arguments. If the minimum value occurs in more than one row, the function choses the one with the highest row number. If the minimum value occurs more than once in that row, the function chose the one with the highest column number. You can assume the matrix is not empty. Examples: if get_location_of_min is called with matrix {{1,7},{4,2},{2,-1}}, and NUM_COLS is 2, the number of rows is 3 the smallest value (-1) is at row 2 column 1. After the function is complete the values 2 and 1 are stored at the corresponding locations pointed to by the arguments. if get_location_of_min is called with matrix {{3,7},{4,2},{2,6}}, and NUM_COLS is 2, the number of rows is 3 the smallest value (2) occurs twice and the one at the highest row is at row 2 column 0. After the function is complete the values 2 and 0 are stored at the corresponding locations pointed to by the arguments. if get_location_of_min is called with matrix {{3,7},{2,4},{2,2}}, and NUM_COLS is 2, the number of rows is 3 the smallest value (2) occurs three times and the one at the highest row and column is at row 2 column 1. After the function is complete the values 2 and 1 are stored at the corresponding locations pointed to by the arguments.
get_location_of_max This function takes a matrix as a 2-D array of integers with NUM_COLS width, the number of rows in the matrix and two integer pointers. The function finds the location of the minimum value in the matrix and stores the row and column of that value to the memory location pointed to by the pointer arguments. If the minimum value occurs in more than one row, the function choses the one with the highest row number. If the minimum value occurs more than once in that row, the function chose the one with the highest column number. You can assume the matrix is not empty. Examples: if get_location_of_max is called with matrix {{1,7},{4,2},{2,9}}, and NUM_COLS is 2, the number of rows is 3 the largest value (9) is at row 2 column 1. After the function is complete the values 2 and 1 are stored at the corresponding locations pointed to by the arguments. if get_location_of_max is called with matrix {{3,7},{4,8},{8,6}}, and NUM_COLS is 2, the number of rows is 3 the largest value (8) occurs twice and the one at the highest row is at row 2 column 0. After the function is complete the values 2 and 0 are stored at the corresponding locations pointed to by the arguments. if get_location_of_max is called with matrix {{3,7},{8,4},{8,8}}, and NUM_COLS is 2, the number of rows is 3 the largest value (8) occurs three times and the one at the highest row and column is at row 2 column 1. After the function is complete the values 2 and 1 are stored at the corresponding locations pointed to by the arguments.
swap_min_max_values This function takes a matrix as a 2-D array of integers with NUM_COLS width, the number of rows in the matrix. The function will find the locations of the minimum and maximum values and swap the locations of these values in the matrix. The locations of minimum and maximum values follow the specifications of the get_location_of_min and get_location_of_max functions. You can assume the matrix is not empty. Examples: if get_location_of_max is called with matrix {{1,7},{4,2},{2,9}}, and NUM_COLS is 2, the number of rows is 3. The minimum value (1) and is at row 0 column 0 and the largest value (9) is at row 2 column 1. After the function is complete matrix is {{9,7},{4,2},{2,1}}.
CODE:
OUTPUT:
Raw_Code:
#include<stdio.h>
#define NUM_COLS 100
void get_location_of_min(int arr[][NUM_COLS],int rows,int cols,int*
min_row,int* min_col){
*min_row = *min_col =0;
//Function to find the location of min
int temp,i,j;
for(i=0;i<rows;i++){
for(j=0;j<cols;j++){
if(arr[i][j]
<= arr[*min_row][*min_col]){
*min_row = i;
*min_col = j;
}
}
}
}
void get_location_of_max(int arr[][NUM_COLS],int rows,int cols,int*
max_row,int* max_col){
*max_row = *max_col =0;
//Function to find the location of max
int temp,i,j;
for(i=0;i<rows;i++){
for(j=0;j<cols;j++){
if(arr[i][j]
>= arr[*max_row][*max_col]){
*max_row = i;
*max_col = j;
}
}
}
}
void swap_min_max_values(int arr[][NUM_COLS],int rows,int
cols){
int min_row,min_col,max_row,max_col,temp;
//Function to swap the min
and max
get_location_of_min(arr,rows,cols,&min_row,&min_col);
get_location_of_max(arr,rows,cols,&max_row,&max_col);
temp = arr[min_row][min_col];
arr[min_row][min_col] = arr[max_row][max_col];
arr[max_row][max_col] =temp;
}
void main(){
int i,j,rows,cols;
int min_col,min_row,max_row,max_col;
int arr[100][NUM_COLS];
printf("\nEnter Number of rows and
cols:"); //Taking rows and cols as input
scanf("%d%d",&rows,&cols);
printf("\nEnter Elements of Array:\n");
//Taking input as array
for(i=0;i<rows;i++){
for(j=0;j<cols;j++){
scanf("%d",&arr[i][j]);
}
}
get_location_of_min(arr,rows,cols,&min_row,&min_col);
//Calling all functions
get_location_of_max(arr,rows,cols,&max_row,&max_col);
swap_min_max_values(arr,2,3);
printf("\nThe Smallest Elements is at row =%d and cols
= %d",min_row,min_col); //Printing the values that we
get from functions
printf("\nThe Smallest Elements is at row =%d and cols
= %d",max_row,max_col);
printf("\nThe Array after Swapping min and max
values:\n");
for(i=0;i<2;i++){
for(j=0;j<3;j++){
printf("%d
",arr[i][j]);
}
printf("\n");
}
}
****Comment me for any Queries and Rate me up******