Howdy! I want to sort the data inputed 3 different ways, as a choice from the user. Either Alphabetical by name, by ascending order for the ID or grades. How would I manipulate the program to sort the strings? maybe by using strcmp()?
This is also from the assignment "To sort the record, one can use a bubble sort function modified to work with name/id/grade"
Code:
#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("\n");
}
printf("how do you want to sort the records? [0 - name, 1 - id, 2 - grade]: ");
scanf("%d", &sort);
if (sort==0)
for(i = 0; i < students; i++)
printf(" Name: %s \n Sudent id: %d\n Grade: %s\n\n",
stu[i].name,stu[i].student_id,stu[i].grade);
else if (sort==1)
for(i = 0; i < students; i++)
printf(" Student id: %d \n Name: %s\n Grade: %s\n\n",
stu[i].student_id,stu[i].name, stu[i].grade);
else if (sort==2)
for(i = 0; i < students; i++)
printf(" Grade: %s \n Name: %s\n Student id: %d\n\n",
stu[i].grade,stu[i].name,stu[i].student_id);
return0;
}