Question

In: Computer Science

USE C PROGRAMMING, call all functions in main, and use a 2 by 3 2d array...

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}}.

Solutions

Expert Solution

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******


Related Solutions

In C, build a connect 4 game with no GUI. Use a 2D array as the...
In C, build a connect 4 game with no GUI. Use a 2D array as the Data structure. First, build the skeleton of the game. Then, build the game. Some guidelines include... SKELETON: Connect Four is a game that alternates player 1 and player 2. You should keep track of whose turn it is next. Create functions: Initialization – print “Setting up the game”. Ask each player their name. Teardown – print “Destroying the game” Accept Input – accept a...
C++ Recursive Functions: Please call functions in a main function as well. 1. A recursive function...
C++ Recursive Functions: Please call functions in a main function as well. 1. A recursive function that print the reverse of a string. (e.g., void printReverse(string exp)). For example, if exp =”coding”, then the function should print out “gnidoc”. 2. Implement a non-recursion-based binary search function. Convert this function into a recursion-based function. 3. Implement a recursive and non-recursive Fibonacci function.
Challenge 3 – Make 2D Array Write a function that takes 3 parameters and makes use...
Challenge 3 – Make 2D Array Write a function that takes 3 parameters and makes use of your prior two functions to create a 2D array filled with a default parameter. var twoD = Init2D(<width>, <height>, <fill val>); Challenge 4 – Random Integer in Range Write a function to return a random integer between a minimum value and maximum value. var ival = IntRandomRange(<min>, <max>); Challenge 5 – Random Int 2D Array Use your prior functions to provide a function...
Using c++ to find all the peaks elements in a 2d array, each element has eight...
Using c++ to find all the peaks elements in a 2d array, each element has eight neighbours
Given a 2D array a, sum up ALL the edges of the array. Ex. int a[...
Given a 2D array a, sum up ALL the edges of the array. Ex. int a[ ][ ] = { {1, 2, 3, 4},                        {5, 6, 7, 8},                        {9, 10, 11, 12} }; OUTPUT: Sum of the edges = 65
Use C Programming - Given an array of integers and a number K, find the smallest...
Use C Programming - Given an array of integers and a number K, find the smallest element in array greater than or equal to K. If such element exists in the array, display it otherwise display "-1". Example: Input:     8     1 3 4 7 8 9 9 10     5     where: First line represents the number of elements in the array. Second line represents the elements in the array. Third line represents the value of K. Output:     7 Explanation:...
Instructions (In C++ Please) Verification This programming lab makes use of an array of characters. The...
Instructions (In C++ Please) Verification This programming lab makes use of an array of characters. The program will validate the characters in the array to see if they meets a set of requirements. Requirements: Must be at least 6 characters long (but not limited to 6, can be greater) Contains one upper case letter Contains one lower case letter Contains one digit Complete the code attached. Must use pointer while checking the characters. Download Source Lab 3 File: #include using...
in C programming language Write a function removeDups that removes all duplicates in a given array...
in C programming language Write a function removeDups that removes all duplicates in a given array of type int. Sample Test Case: input -> {1,2,2,2,3,3,4,2,4,5,6,6} output -> {1,2,3,4,5,6,0,0,0,0,0,0} More specifically, the algorithm should only keep the first occurance of each element in the array, in the order they appear. In order to keep the array at the same length, we will replace the removed elements with zeros, and move them to the end of the array.
How to make a 2D array Tic Tac Toe game in C?
How to make a 2D array Tic Tac Toe game in C?
.......Subject Java..... main() main() will ask the user for input and then call functions to do...
.......Subject Java..... main() main() will ask the user for input and then call functions to do calculations. The calculations will be returned to main() where they will be printed out. First function Create a function named computeBill that receives on parameter. It receives the price of an item. It will then add 8.25% sales tax to this and return the total due back to main(). Second function Create another function named computeBill that receives 2 parameters. It will receive the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT