Question: Program 4 In Program4.c, we will consider the structure student from Program2.c. Modify the function sort() from Program3.c to sort an array of n students

 Program 4 In Program4.c, we will consider the structure student fromProgram2.c. Modify the function sort() from Program3.c to sort an array of

Program 4 In Program4.c, we will consider the structure student from Program2.c. Modify the function sort() from Program3.c to sort an array of n students based on their initials. The function prototype is void sort(struct student* students, int n). In sort(), if two students have the same first initial, you should compare their second initial. As in Program2.c, initials and scores of students are to be generated randomly by rand(). Make sure that you can handle the case when the user provides input n=0. Grading of Program 4: maximum 40 points Sorts an array of student structures correctly = 40 points A penalty of negative 10 points if your code does not correctly address the case when two students have the same first initial. A penalty of negative 10 points if your code does not correctly address an empty array of students. Zero points if the code cannot be executed or gives "Segmentation fault". 1/* CS261- HW1 - Program4.c*/ 2/* Name: Michael Hwang * Date: 1/15/21 * Solution description: HW1 Program 4 5 */ 3 4 6 7 #include 8 #include 9 10 struct student 11 char initials[2]; 12 int score; 13 }; 14 15 void sort(struct student* students, int n){ /*Sort n students based on their initials*/ void sort(struct students* students, int n){ int j; int i; 16 17 18 19 20 struct student temp; 21 22 23 24 25 26 27 28 29 for(i = n - 1; i > 0; i--){ if(students[j].initials > students[j + i].initials) { temp = students[j]; students[j] = students[j + i]; students[j + i] = temp; } } 30 } 34 35 31 } 32 33 int main() { /*Declare an integer n and assign it a value. */ int n = 0; /*Allocate memory for n students using malloc. */ /*Generate random initials and scores for the n students, using rand(). */ /*Print the contents of the array of n students. */ /*Pass this array along with n to the sort() function*/ /*Print the contents of the array of n students. */ 36 37 38 39 40 41 42 43 44 45 return 0; 46 47 } 48 Program 4 In Program4.c, we will consider the structure student from Program2.c. Modify the function sort() from Program3.c to sort an array of n students based on their initials. The function prototype is void sort(struct student* students, int n). In sort(), if two students have the same first initial, you should compare their second initial. As in Program2.c, initials and scores of students are to be generated randomly by rand(). Make sure that you can handle the case when the user provides input n=0. Grading of Program 4: maximum 40 points Sorts an array of student structures correctly = 40 points A penalty of negative 10 points if your code does not correctly address the case when two students have the same first initial. A penalty of negative 10 points if your code does not correctly address an empty array of students. Zero points if the code cannot be executed or gives "Segmentation fault". 1/* CS261- HW1 - Program4.c*/ 2/* Name: Michael Hwang * Date: 1/15/21 * Solution description: HW1 Program 4 5 */ 3 4 6 7 #include 8 #include 9 10 struct student 11 char initials[2]; 12 int score; 13 }; 14 15 void sort(struct student* students, int n){ /*Sort n students based on their initials*/ void sort(struct students* students, int n){ int j; int i; 16 17 18 19 20 struct student temp; 21 22 23 24 25 26 27 28 29 for(i = n - 1; i > 0; i--){ if(students[j].initials > students[j + i].initials) { temp = students[j]; students[j] = students[j + i]; students[j + i] = temp; } } 30 } 34 35 31 } 32 33 int main() { /*Declare an integer n and assign it a value. */ int n = 0; /*Allocate memory for n students using malloc. */ /*Generate random initials and scores for the n students, using rand(). */ /*Print the contents of the array of n students. */ /*Pass this array along with n to the sort() function*/ /*Print the contents of the array of n students. */ 36 37 38 39 40 41 42 43 44 45 return 0; 46 47 } 48

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!