Bubble Sort in ANSI C

181 Views Asked by At

I have an assignment that needs the data sorted. Ive been trying to use the bubble sort method to sort the data however it does not seem to work. I have attached the assignment to allow you to understand what I am doing. I am not looking for the answer but for help. I am also wondering how the sales_id would incorporate into that since it is not an array and also needs to be sorted.

#include <stdio.h>

struct sales_commissions {
    float commissions[3];
};

struct sales {
    char sales_name[30];
    int sales_id;
    struct sales_commissions sc;
};

void comp_name(char company_name[15]);
int agent_number();
void agent_info(char[], int sales_agents, int sales_id, float[]);
void sorting(char[], int sales_agents, int sales_id, float[], char []);

main() {
    /* --- Declaring Variables --- */
    char company_name[15];
    char c;
    int i;
    int sales_agents;

    struct sales Sales;

    comp_name(company_name);    

    sales_agents = agent_number();
    
    agent_info(Sales.sales_name, sales_agents, Sales.sales_id, Sales.sc.commissions);
    
    sorting(Sales.sales_name, sales_agents, Sales.sales_id, Sales.sc.commissions, company_name);

    return 0;
}

void comp_name(char company_name[15]) {
    printf("Welcome to the Sears Commission analysis\n");

    printf("Enter the Company Name: ");
    scanf("%[^\n]s", company_name);
    
    return company_name;
}

int agent_number() {
    char c;
    int sales_agents = 0;

    printf("Enter the number of sales agents (1 - 20): ");
    scanf("%d", &sales_agents);
    while ((c = getchar() != '\n') && c != EOF);

    while (sales_agents < 1 || sales_agents > 20) {
        while (sales_agents == 0) {
            printf("*** No number of sales agents entered. Quitting program. ***\n");
            return 0;
        }

        printf("*** Invalid number of sales agents. Please enter 1 - 20. ***\n");
        printf("Enter the number of sales agents (1 - 20): ");
        scanf("%d", sales_agents);
        while ((c = getchar() != '\n') && c != EOF);
    }
    return sales_agents;
}

void agent_info(char sales_name[], int agent_number, int sales_id,
                float commissions[])
{
    char c;
    int i;

    for (i = 0; i < agent_number; i++) {
        printf("Enter the name of sales agent #%d:  ", i + 1);
        scanf("[^\n]s", sales_name[i]);
        while ((c = getchar() != '\n') && c != EOF);
        
        printf("Enter ID number for sales agent #%d:  ", i + 1);
        scanf("%[^\n]s", sales_id);                
        while ( (c = getchar() != '\n') && c != EOF);
        
        printf("Enter Commission 1 for sales agent #%d:  ", i + 1);
        scanf("%[^\n]s", &commissions[0]);                
        while ( (c = getchar() != '\n') && c != EOF);

        printf("Enter Commission 2 for sales agent #%d:  ", i + 1);
        scanf("%[^\n]s", &commissions[1]);                
        while ( (c = getchar() != '\n') && c != EOF);

        printf("Enter Commission 3 for sales agent #%d:  ", i + 1);
        scanf("%[^\n]s", &commissions[2]);                
        while ( (c = getchar() != '\n') && c != EOF);
    }
    return sales_name;
}

void sorting(char sales_name[], int agent_number, int sales_id,
             float commissions[], char company_name[])
{
    int i;
    int u;
    int temp;
    char c;
    char temp2;
    char swapped = 'Y';
    
    printf("Did we get to sorting?\n");
    
    while (swapped == 'Y') {
        swapped = 'N';

        for (i = 0; i < agent_number - 1; i++) {
            if (commissions[i] < commissions[i + 1]) {
                temp = commissions[i];
                commissions[i] = commissions[i + 1];
                commissions[i + 1] = temp;

                for (u = 0; u < 50; u++) {
                    temp2 = sales_name[i][u];
                    sales_name[i][u] = sales_name[i + 1][u];
                    sales_name[i + 1][u] = temp2;
                }
                swapped = 'Y';
            }
        }
    }
    printf("%s\n", swapped);
    
    printf("\n\nCommission Report -- %s\n\n", c);

    printf("Agent Name\n\n");
    for (i = 0; i < agent_number; i++) {
        printf("%-12s     \n\n", sales_name[i]);
    }
}

I was trying to first try and see if I could get a bubble sort running given what is an array. I have tried to see if it could pair the name to the commission.

P.S I am exhausted so I will be checking responses in the morning, pardon my delayed response.

1

There are 1 best solutions below

1
chqrlie On

There are many problems in the code. You should enable more compiler warnings and fix the problems reported. Use gcc -Wall -Wextra -Werror or clang -Wall -Weverything -Werror.

The sort function prototype is incorrect: the argument Sales.sales_name is probably a 2D array char sales_name[20][50] and the sales_id is probably also an array, so the function prototype should be

void sorting(char sales_name[][50], int agent_number, int sales_id,
             float commissions[], char company_name[]);

Furthermore, the swapping operation sales_name[i] = sales_name[i + 1][u]; should store to sales_name[i][u] instead and the temp variable must have the same type as the commissions element type: float.

Here is a modified version:

void sorting(char sales_name[][50], int agent_number, int sales_id[],
             float commissions[], char company_name[]) {
    char swapped = 'Y';
    
    while (swapped == 'Y') {
        swapped = 'N';

        for (int i = 0; i < agent_number - 1; i++) {
            if (commissions[i] < commissions[i + 1]) {
                // swap the commission value?
                float temp = commissions[i];
                commissions[i] = commissions[i + 1];
                commissions[i + 1] = temp;

                // swap the agent ID?
                int id = sales_id[i];
                sales_id[i] = sales_id[i + 1];
                sales_id[i + 1] = id;

                // swap the agent name
                for (int u = 0; u < 50; u++) {
                    char temp2 = sales_name[i][u];
                    sales_name[i][u] = sales_name[i + 1][u];
                    sales_name[i + 1][u] = temp2;
                }
                swapped = 'Y';
            }
        }
    }

    printf("\n\nCommission Report -- %s\n\n", company_name);

    printf("Agent Name  Commission\n\n");
    for (int i = 0; i < agent_number; i++) {
        printf("%-12s  %.2f\n", sales_name[i], commissions[i]);
    }
}

The while loop to read and discard the remainder of the input line is incorrect too: c must have type int to handle all possible byte values (all values of the type unsigned char) and the negative value EOF. You should encapsulate this inside a function:

// read and discard the remainder of the input line
// return the newline if found or EOF if none before the end of file
int flush_stdin(void) {
    int c;
    while ((c = getchar() != '\n') && c != EOF)
        continue;
    return c;
}

Most of the scanf conversions are incorrect:

  • scanf("[^\n]s", sales_name[i]) should be scanf("%49[^\n]", sales_name[i])
  • scanf("%[^\n]s", sales_id) should be scanf("%d", &sales_id[i])
  • scanf("%[^\n]s", &commissions[0]) should be scanf("%f", &commissions[0]) but it unclear what the commissions array represents: is the one per agent?

Instead of using a separate array for each sales agent data item, you should define a structure to describe each sales agent and use an array of such structures.