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

I have a database (shown below) and a function that is suppose to help me sort t

ID: 3658108 • Letter: I

Question

I have a database (shown below) and a function that is suppose to help me sort the data via the input of 0-2 if 0 then alphabetical by name, if 1 then decending order by ID, if 2 then decending by grade... How do i sort the strings in the program and does the fuction below help at all? Thanks a LOT!

#include <stdio.h>

#include <string.h>

struct student

{

int student_id;

char name[30];

char grade[15];

};


int i;

struct student stu[20];

int students;

int sort;

int main(void)

{

printf("How many students in the Class?: ");

scanf("%d", &students);

for(i = 0; i < students; i++)

{

printf("Enter name: ");

scanf("%s",stu[i].name);

printf("Enter id: ");

scanf("%d", &stu[i].student_id);

printf("Enter grade: ");

scanf("%s",stu[i].grade);

printf(" ");

}


printf("how do you want to sort the records? [0 - name, 1 - id, 2 - grade]: ");

scanf("%d", &sort);

if (sort==0)

printf(" %s, ID: %d, has a grade: %s ",

stu[i].name,stu[i].student_id,stu[i].grade);

else if (sort==1)

for(i = 0; i < students; i++)

printf(" Student ID: %d, %s has a grade: %s ",

stu[i].student_id,stu[i].name, stu[i].grade);

else if (sort==2)

for(i = 0; i < students; i++)

printf(" A grade of: %s for %s ID: %d ",

stu[i].grade,stu[i].name,stu[i].student_id);

return 0;

}

//This is the Fuction part, i dont know how to implament it in the above program...

void sort(struct student st[],int cnt,int choice){//(0 - name, 1 - id, 2 - grade)
int i,j;
struct student temp;
/* char last_name[30];
int student_id; // five digits
char grade; */
for(i=0;i<cnt-2;i++){
for(j=0;j<cnt-1;j++){
if((choice==0 && strcmp(st[j].last_name,st[j+1].last_name)>0)
||(choice==1 && st[j].student_id>st[j+1].student_id)
||(choice==2 && st[j].grade>st[j+1].grade))
{
temp=st[j];
st[j]=st[j+1];
st[j+1]=temp;
}
}
}

return;
}

Explanation / Answer

The bottom fxn works. You just have to call it in your function right after these two line,but you might either want to rename the fxn or your variable sort: printf("how do you want to sort the records? [0 - name, 1 - id, 2 - grade]: "); scanf("%d", &sort); //fxn call sort(stu,students,sort);