Search code examples
csortingstructinsertion-sortfunction-definition

bubble sort in an structure in C


I've coded a program based on an array of structure and all parts of it are ok but one of my functions that is sort_student function has a bug. In this code at first the user will enter some students and their information included first name last name id and average after that user will have several options and all of them except sort function are ok . The main problem with this function is that it can just sort the averages and then it will put the first name and last name and id of the first student in structure for all of students .And I don't know how can I solve it. And I think I have to say that I am amateur in coding so please help if want to help if you don't want please don't post negative comments. Thanks.

this is the function that has problem :

void sort_student()
{
    float selected_item;
    int j;

    for (int i = 0; i < students_count; i++)
    {
        selected_item = student[i].average;
        j = i - 1;
        while ((j >= 0) && (selected_item < student[j].average))
        {
            student[j + 1].average = student[j].average;
            strcpy(student[j + 1].first_name, student[j].first_name);
            strcpy(student[j + 1].last_name, student[j].last_name);
            strcpy(student[j + 1].id, student[j].id);

            j--;
        }
        student[j + 1].average = selected_item;
    }

    printf("The students are sorted now.");
}

and if you need this is all of my code :

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

struct school
{
    char first_name[80];
    char last_name[80];
    char id[12];
    float average;
};

struct school *student;

int students_count;
int con = 1;
int chosen_option;

FILE *fptr;

void options();
void add_student();
void remove_student();
void search_student();
void sort_student();
void save_to_file();
void upload_students();

int main() {
    student = calloc(300, sizeof *student);

    printf("How many students do you have ? (MAX = 300)\n");
    scanf("%d", &students_count);

    for (int i = 0; i < students_count; i++)
    {
        printf("Whats your student number %d first name ? \n", i+1);
        scanf("%79s", student[i].first_name);

        printf("Whats your student number %d last name ? \n", i+1);
        scanf("%79s", student[i].last_name);

        printf("Whats your student number %d id ? \n", i+1);
        scanf("%11s", student[i].id);

        printf("Whats your student number %d average ? \n", i+1);
        scanf("%f", &student[i].average);
    }

    printf("\nOK! you've entered the students and their information successfully.\n");

    while (con == 1)
    {
        options();
    }
    return 0;
}

void options()
{
    printf("\nOK! Do you want to continue? (Yes : 1/N : 0)");
    scanf("%d", &con);

    if (con == 1)
    {
        printf("What do you want to do now ? \n");

        printf("\n1. Add student\n");
        printf("\n2. Remove student\n");
        printf("\n3. Search student\n");
        printf("\n4. Sort by average\n");
        printf("\n5. save the students information in a file\n");
        printf("\n6. upload all of students information\n");

        printf("\nPlease enter the number of you choice : ");
        scanf("%d", &chosen_option);

        switch (chosen_option) {
            case 1:
                add_student();
                break;
            case 2:
                remove_student();
                break;
            case 3:
                search_student();
                break;
            case 4:
                sort_student();
                break;
            case 5:
                save_to_file();
                break;
            case 6:
                upload_students();
                break;
            default:
                printf("Your chosen option is invalid\n");
                break;
        }
    }
    else if (con == 0)
    {
        printf("OK! You don't want to continue. Bye!\n");
        exit(0);
    } else
    {
        printf("You can't continue with invalid options.\n");
        exit(0);
    }
}

void add_student()
{
        printf("Whats your (new) student number %d first name ? \n", students_count+1);
        scanf("%79s", student[students_count].first_name);

        printf("Whats your (new) student number %d last name ? \n", students_count+1);
        scanf("%79s", student[students_count].last_name);

        printf("Whats your (new) student number %d id ? \n", students_count+1);
        scanf("%11s", student[students_count].id);

        printf("Whats your (new) student number %d average ? \n", students_count+1);
        scanf("%f", &student[students_count].average);

        students_count ++;


    printf("OK! You've entered the new student information successfully.\n");
}
void remove_student()
{
    char removing_id[12];
    int i;

    printf("\nPlease enter the id of student that you want to remove : ");
    scanf("%11s", removing_id);

    for (i = 0; i < students_count; i++)
    {
        if (strcmp(removing_id, student[i].id) == 0)
        {
            for (int j = i; j < students_count - 1; j++)
            {
                strcpy(student[j].first_name, student[j+1].first_name);
                strcpy(student[j].last_name, student[j+1].last_name);
                strcpy(student[j].id, student[j+1].id);
                student[j].average = student[j+1].average;
            }
            break;
        }
    }

    if(i == students_count)
    {
        printf("The id is not available!");
        exit(0);
    }
    else
    {
        students_count--;
        printf("The student removed successfully.");
    }
}
void search_student()
{
    char searching_id[12];
    int i;

    printf("\nPlease enter the id of student that you want to search : ");
    scanf("%11s",searching_id);

    for (i = 0; i < students_count; i++)
    {
        if (strcmp(searching_id, student[i].id) == 0)
        {
            printf("\nFIRSTNAME    LASTNAME    ID    AVERAGE\n");

            printf("\n%s\t%s\t%s\t%.2f\n", student[i].first_name, student[i].last_name, student[i].id,
                   student[i].average);
        }
    }

    if (i == students_count)
    {
        printf("We don't have student with this id.");
    }
}
void sort_student()
{
    float selected_item;
    int j;

    for (int i = 0; i < students_count; i++)
    {
        selected_item = student[i].average;
        j = i - 1;
        while ((j >= 0) && (selected_item < student[j].average))
        {
            student[j + 1].average = student[j].average;
            strcpy(student[j + 1].first_name, student[j].first_name);
            strcpy(student[j + 1].last_name, student[j].last_name);
            strcpy(student[j + 1].id, student[j].id);

            j--;
        }
        student[j + 1].average = selected_item;
    }

    printf("The students are sorted now.");
}
void save_to_file()
{
    fptr = fopen("MY SCHOOL FILE.txt", "w");

    fprintf(fptr, "#    FIRSTNAME    LASTNAME    ID    AVERAGE\n");

    for(int i = 0; i < students_count; i++)
    {
        fprintf(fptr, "%d\t%s\t%s\t%s\t%.2f\n", i+1, student[i].first_name, student[i].last_name,
                student[i].id, student[i].average);
    }

    fclose(fptr);

    printf("\nThe file is in project folder.\n");
}

void upload_students()
{
    printf("#    FIRSTNAME    LASTNAME    ID    AVERAGE\n");
    for (int i = 0; i < students_count; i++)
    {
        printf("%d\t%s\t%s\t%s\t%.2f\n", i+1, student[i].first_name, student[i]. last_name,
               student[i].id, student[i].average);
    }
}


Solution

  • For starters neither bubble sort method is used in the function sort_student. Actually you are trying to implement the insertion sort method.

    in this while loop

        while ((j >= 0) && (selected_item < student[j].average))
        {
            student[j + 1].average = student[j].average;
            strcpy(student[j + 1].first_name, student[j].first_name);
            strcpy(student[j + 1].last_name, student[j].last_name);
            strcpy(student[j + 1].id, student[j].id);
    
            j--;
        }
    

    all data members of the element student[i] are overwritten.

    The function can look the following way

    void sort_student( void )
    {
        for ( int i = 1; i < students_count; i++ )
        {
            struct school current_student = student[i];
    
            int j = i;
    
            for ( ; j != 0 && current_student.average < student[j - 1].average; --j )
            {
                student[j] = student[j-1];
            }
    
            if ( j != i ) student[j] = current_student;
        }
    
        printf("The students are sorted now.");
    }