Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Create a C program .... Problem 1. The user will first input the number of stude

ID: 3677807 • Letter: C

Question

Create a C program ....

Problem 1. The user will first input the number of students(less than 100). Then input the detailed information for each students including ID, name (less than 20 characters and do not contain spaces), grade for math and physics. 2. Define a structure type and use a structure array to store the data input by the user Use the gsort function in header file stdlib.h to sort the structure array. Then output the sorted array on the screen. Do not write your own code for sorting 3. The comparison standard is as follows. (a) The student with greater total grade will rank first. (b) If two students have the same total grade, the student with lower difference between the student's own grade for math and physics will rank first. (c) If two students have the same total grade and the same grade difference, the students with less student ID will rank first. Sample Input 101 Bob 80 90 125 Tom 81 91 133 Jim 82 90 131 Lucy 82 90 Sample output 131 Lucy 82 90 133 Jim 82 90 125 Tom 8191 101 Bob 80 90

Explanation / Answer

#include <stdio.h>
#include <stdlib.h>

struct student_info{
int id;
char name[21];
int math_grade;
int physics_grade;
   int total_grade;
  
};
int cmpfunc (const void * x, const void * y)
{
   struct student_info* a= (struct student_info*)x;
  
   struct student_info* b= (struct student_info*)y;
  
   int totalGrade1 = a->total_grade;
   int totalGrade2 = b->total_grade;
  
   int math_grade1 = a->math_grade;
   int math_grade2 = b->math_grade;
  
   int physics_grade1 = a->physics_grade;
   int physics_grade2 = b->physics_grade;
  
   int own_grade_diff1 = abs(math_grade1-physics_grade1);
   int own_grade_diff2 = abs(math_grade2-physics_grade2);
  
   int id1 = a->id;
   int id2 = b->id;
  
   int result;
  
  
   result = totalGrade1-totalGrade2;
   if(result==0){
       result = own_grade_diff1-own_grade_diff2;
       if(result==0){
           result = id1 - id2;
       }
   }
return result;
}
int main()
{
int n,i;
struct student_info* students;
scanf("%d",&n);
students = (struct student_info *)malloc(n*sizeof(struct student_info));
for(i=0;i<n;i++){
scanf("%d %s %d %d",&(students[i].id),&(students[i].name),&(students[i].math_grade),&(students[i].physics_grade));
   students[i].total_grade = students[i].math_grade+students[i].physics_grade;
}
printf(" ");
qsort(students,n,sizeof(struct student_info),cmpfunc);
  
for(i=0;i<n;i++){
   printf("%d %s %d %d ",students[i].id,students[i].name,students[i].math_grade,students[i].physics_grade);
}
return 0;
}