Where would I put printf ( \" %s, ID: %d, has a grade: %s\ \ \" , #include <stdi
ID: 3658152 • Letter: W
Question
Where would I put
printf(" %s, ID: %d, has a grade: %s ",
#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 ans;
void sort(struct student st[],int cnt,int choice);
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", &ans);
}
void sort(struct student st[],int cnt,int choice)
{
int i,j;
struct student temp;
//Bubble sorting
//(go through index 0 to cnt-1 and if current element is larger than next element, swap them)
//does this cnt-2 times
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)//if choice 0, sort by name
||
(choice==1 && st[j].student_id>st[j+1].student_id)//if choice 1,sort by id
||
(choice==2 && st[j].grade>st[j+1].grade)//if choice 2, sort by grade
)
{
temp=st[j];
st[j]=st[j+1];
st[j+1]=temp;
}
}
}
return;
}
Explanation / Answer
no, it is correct to use that statement. You would put that right after sorting the array. to print each student, you would need to use a for loop to iterate through the array. Also, I think grade only need to be a char instead of char[]
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.