Why doesnt this output the sorted fxn when printed? IE. Why isnt it in alphabeti
ID: 3658156 • Letter: W
Question
Why doesnt this output the sorted fxn when printed? IE. Why isnt it in alphabetical order when 0 is entered, numerical when 1 and alphabetical when 2?
#include <string.h>
#include <stdio.h>
struct student
{
int student_id;
char name[30];
char grade[15];
};
void sort(struct student st[],int cnt,int choice);
int main(void)
{
int i;
int students;
int input;
struct student stu[20];
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", &input);
sort(stu,students,input);
for(i = 0; i < students; i++)
{
printf(" %s, ID: %d, has a grade: %s ",
stu[i].name,stu[i].student_id,stu[i].grade);
}
}
void sort(struct student st[],int cnt,int choice)
{
int i,j;
struct student temp;
for(i=0;i<cnt-2;i++)
{
for(j=0;j<cnt-1;j++)
{
if(
(choice==0 && strcmp(st[j].name,st[j+1].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
use do while or change iRelated Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.