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.
There are many problems in the code. You should enable more compiler warnings and fix the problems reported. Use
gcc -Wall -Wextra -Werrororclang -Wall -Weverything -Werror.The sort function prototype is incorrect: the argument
Sales.sales_nameis probably a 2D arraychar sales_name[20][50]and thesales_idis probably also an array, so the function prototype should beFurthermore, the swapping operation
sales_name[i] = sales_name[i + 1][u];should store tosales_name[i][u]instead and thetempvariable must have the same type as thecommissionselement type:float.Here is a modified version:
The
whileloop to read and discard the remainder of the input line is incorrect too:cmust have typeintto handle all possible byte values (all values of the typeunsigned char) and the negative valueEOF. You should encapsulate this inside a function:Most of the
scanfconversions are incorrect:scanf("[^\n]s", sales_name[i])should bescanf("%49[^\n]", sales_name[i])scanf("%[^\n]s", sales_id)should bescanf("%d", &sales_id[i])scanf("%[^\n]s", &commissions[0])should bescanf("%f", &commissions[0])but it unclear what thecommissionsarray 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.