which sorts the rows of a given 2D array M with respect to (w.r.t.) the values i
ID: 3891800 • Letter: W
Question
which sorts the rows of a given 2D array M with respect to (w.r.t.) the values in a given column c. Hint: You can simply use the selection sort strategy over the given column c. That is, when you are in the ih iteration, assume row i column c has the current minimum and search the rows after row i for the actual minimum in column c. Suppose row j has the actual minimum in column c. Simply exchange row i and row i. Then increase i and do the same until you are 3. (20 pt) Implement a function void sort2D(int M[Row] [Col], int c) done with all the rows! Here is how your function can be called in a driver program with /* suppose all standard c 1ibraries are included here */ 7Row wi11 be large value in an actual program / Col will be large value in an actual program #define col 5 int M[Row] [Col]- (12, 7, 9, 5, 3) 9, 3, 4, 3, 2) (4, 5, 8, 6, 411 void main) int c, i, printf ("which column you want to use to sort 2D array") scanf ("%d", &c;); sort2D (M, c) for ( 1-0; iExplanation / Answer
ScreenShort
----------------------------------------------------------------
Program
//Header file
#include<stdio.h>
//Global variables
const int Row=3;
const int Col = 5;
//2d array for test
int M[Row][Col] = {{2,7,9,5,3},{9,3,4,3,2},{4,5,8,6,4}};
//Function prototype
void sort2D(int M[Row][Col], int c);
//main method
int main()
{
//Variables for array display and index of the column
int c, j, i;
//Enter column index
printf("Which column you want to use to sort 2D array:");
scanf("%d", &c);
//Sort function call
sort2D(M,c);
//Display elements
for (i = 0; i < Row; i++) {
for (j = 0; j <Col; j++) {
printf("%d, ", M[i][j]);
}
printf(" ");
}
return 0;
}
//Sort function definition
void sort2D(int M[Row][Col], int c) {
//Variables for sort
int key, k, j;
//Loop for sort
for (j = 1; j<Row; j++)
{
key = M[j][c];
k = j-1;
while (k >= 0 && M[k][c]>key)
{
M[k + 1][c] = M[k][c];
k--;
}
M[k + 1][c] = key;
}
}
------------------------------------------------------
Output
Which column you want to use to sort 2D array:2
2, 7, 4, 5, 3,
9, 3, 8, 3, 2,
4, 5, 9, 6, 4,
Press any key to continue . . .
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.