Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

How do i use Selection sort to sort Student ID in this program?

acubibeoacubibeo anh8652147Posts: 1

include <stdio.h>

include <string.h>

define SIZE 5

typedef struct{
int sid;
char name[30];
int physics;
int eng;
int math;
int total;
}STUD;

// Declaration
void INPUT_ST(STUD st[], int index);
void PRT_MENU(void);
int GET_LAST(STUD st[]);
void PRT_ST(STUD st[], int index);
void SAVE_ST(STUD st[], int index);
void LOAD_ST(STUD st[]);

// Definition
void PRT_MENU(void){
printf("---------------------------\n");
printf("Student Management Program \n");
printf("---------------------------\n");
printf("1: Input \n");
printf("2: Print \n");
//printf("3: Search (Modify / Delete) \n");
//printf("4: Save \n");
//printf("5: Load \n");
printf("6: Terminate program\n");
printf("---------------------------\n");
printf(">");
}

void INPUT_ST(STUD st[], int index){

printf("ID number: ");
scanf("%d", &st[index].sid);
printf("Name: ");
char nameinput[30];
scanf("%s", nameinput);
strcpy(st[index].name, nameinput);
printf("Physics: ");
scanf("%d", &st[index].physics);
printf("English: ");
scanf("%d", &st[index].eng);
printf("Math: ");
scanf("%d", &st[index].math);

st[index].total = st[index].physics + st[index].eng + st[index].math;

}

int GET_LAST(STUD st[]){
int i;
for (i=0; i<SIZE; i++){
if(st[i].total == -1){
return i;
}
}
}

void PRT_ST(STUD st[], int index){
int i;
printf("------------------------------\n");
printf("%8s %7s %5s %5s %5s %5s \n", "ID", "Name", "Kor", "English", "Math", "Total");
printf("------------------------------\n");

for (i=0; i<index; i++){
    printf("%8d %7s %4d %4d %4d %5d \n", st[i].sid, st[i].name, st[i].physics, st[i].eng, st[i].math, st[i].total);
}

}

int main(void){
int menu, i;
int index = 0;
STUD ST[SIZE];

for (i=0; i<SIZE; i++){
    ST[i].total = -1;
}

while(1){

    PRT_MENU();
    scanf("%d", &menu);
    index = GET_LAST(ST);

    switch (menu){
        case 1:
            INPUT_ST(ST, index);
            break;
        case 2:
            PRT_ST(ST, index);
            break;
        case 3:
            //SEARCH_ST();
            break;
        case 4:
            //SAVE_ST(ST, index);
            break;
        case 5:
            //LOAD_ST(ST);
            break;
        case 6:
            printf("Program terminated\n");
            return 0;
        default:
            printf("Input is not correct");
    }
}
return 0;

}

Tagged:
Sign In or Register to comment.