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

How would I get this outer for loop to iterate one extra time? this will make th

ID: 3658178 • Letter: H

Question

How would I get this outer for loop to iterate one extra time? this will make the program work correctly.

for(i=0;i<cnt-1;i++)

Coming from the code:

#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);

}


return 0;

}

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-2;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;

}


}

}

}

Explanation / Answer

use do while or change i
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote