Write a C program to sort the list of students id numbers for five different cou
ID: 3760857 • Letter: W
Question
Write a C program to sort the list of students id numbers for five different courses. Assume number of students registered in all five courses are same. The following table shows id numbers of students registered in each course row wise. Declare a two dimensional array to populate the data in the table.
C1
89
65
90
45
23
10
43
23
18
12
C2
85
60
95
40
21
18
40
27
13
17
C3
80
62
91
42
22
12
41
25
10
71
C4
81
61
94
49
29
13
46
21
12
88
C5
83
66
96
47
27
14
48
28
19
55
Accomplish the task by writing following functions:
(i) populateArray(): Write a function to read the id numbers in the table shown above. Populate course numbers in first column and id numbers in rest of the columns. Use 1 for C1, 2 for C2 and so on. It does not return anything.
(ii) sortRows(): Write a function which sorts the id numbers of the students for each course separately. Here sorting should be done row-wise.
Call both the functions in main. Print the final array in main to see the list of sorted id numbers for each course.
C1
89
65
90
45
23
10
43
23
18
12
C2
85
60
95
40
21
18
40
27
13
17
C3
80
62
91
42
22
12
41
25
10
71
C4
81
61
94
49
29
13
46
21
12
88
C5
83
66
96
47
27
14
48
28
19
55
Explanation / Answer
#include <stdio.h>
void populateArray(int Array[5][11])
{
int i, j;
int A[5][11] = {{1,89,65,90,45,23,10,43,23,18,12},
{2,85,60,95,40,21,18,40,27,13,17},
{3,80,62,91,42,22,12,41,25,10,71},
{4,81,61,94,49,29,13,46,21,12,88},
{5,83,66,96,47,27,14,48,28,19,55}};
for(i = 0; i < 5; i++)
for(j = 0; j < 11; j++)
Array[i][j] = A[i][j];
}
void sortRows(int A[5][11])
{
int i, j, k, temp;
for(int i = 0; i < 5; i++) //For each row.
{
for(j = 1; j < 11; j++)
for(k = 1; k < 11-j; k++)
if(A[i][k] > A[i][k+1])
{
temp = A[i][k];
A[i][k] = A[i][k+1];
A[i][k+1] = temp;
}
}
}
int main()
{
int Array[5][11], i, j;
populateArray(Array);
sortRows(Array);
printf("After Sorting: ");
for(i = 0; i < 5; i++)
{
for(j = 0; j < 11; j++)
printf("%i ", Array[i][j]);
printf(" ");
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.