Question
Need help to make sure my program is ANSI C coding. Can you add more comments in code and after end block markers ? #include
Need help to make sure my program is ANSI C coding. Can you add more comments in code and after end block markers ?
#include
#define MAX_AGENTS 20 #define MAX_NAME_LEN 15
/* Define structure to store sales comission data */ struct sales_commissions { float commissions[3]; };
/* Define structure to store sales agent data */ struct sales { char sales_name[30]; int sales_id; struct sales_commissions sc; };
/* Function prototypes */ void get_company_name(char *company_name) { printf("Enter the company name: "); /* prompt user input */ fgets(company_name, MAX_NAME_LEN, stdin); int len = strlen(company_name); if (company_name[len - 1] == ' ') { company_name[len - 1] = '\0'; } /* end if loop*/ }
/* Function number of sales agents to process */ int get_num_agents() { int num_agents; printf("Enter the number of sales agents (1-20): "); /* prompt user input */ scanf("%d", &num_agents); /* scanf function to accept integer value */ while (num_agents < 1 || num_agents > MAX_AGENTS) { printf("Incorrect number of sales agents, please re-enter. "); /* prompt incorrect number */ printf("Enter the number of sales agents (1-20): "); scanf("%d", &num_agents); } return num_agents; }
/* Function to get sales agent name, id number, and commission */ void get_agent_data(struct sales *agent) { printf("Enter the name for sales agent: "); /* prompt user input */ scanf("%s", agent->sales_name); int c; while ((c = getchar()) != ' ' && c != EOF); printf("Enter the id for sales agent: "); /* prompt user input */ scanf("%d", &agent->sales_id);
for (int i = 0; i < 3; i++) { printf("Enter commission %d for sales agent: ", i + 1); /* prompt user input */ scanf("%f", &agent->sc.commissions[i]); } }
/* Function to sort the data */ void sort_data(struct sales *agents, int num_agents) { for (int i = 0; i < num_agents - 1; i++) { for (int j = i + 1; j < num_agents; j++) { if (agents[i].sales_id > agents[j].sales_id) { struct sales temp = agents[i]; agents[i] = agents[j]; agents[j] = temp; } } } }
/*Begin main*/ int main() { printf("Welcome to the Sears Commission Analysis "); /* print welcome statement */
/* Declare variables */ char company_name[MAX_NAME_LEN]; float m=0.0; /* Get company name */ get_company_name(company_name); /* Declare variables */ int num_agents = get_num_agents(); struct sales agents[MAX_AGENTS]; for (int i = 0; i < num_agents; i++) { printf("Enter data for sales agent %d ", i + 1); /* prompt user input */ get_agent_data(&agents[i]); }
sort_data(agents, num_agents); /* Call function to display report */ printf(" \t\t\t\tCommission Report "); printf("\t\t\t\t %s ", company_name); printf("\t\t\t\t----------------- "); printf("Id No.\tName\t\tCommissions\t\tAverage "); /* Daclare and calculate total comission */ float total_commission = 0; for (int i = 0; i < num_agents; i++) { struct sales *agent = &agents[i]; float avg = (agent->sc.commissions[0] + agent->sc.commissions[1] + agent->sc.commissions[2]) / 3; printf("%05d\t %-20s\t%8.2f\t\t%8.2f ", agent->sales_id, agent->sales_name, agent->sc.commissions[0] + agent->sc.commissions[1] + agent->sc.commissions[2], avg); total_commission += agent->sc.commissions[0] + agent->sc.commissions[1] + agent->sc.commissions[2]; m = m + avg; } /* Call function to display total commision */ printf("Company\t\t\t\t%8.2f\t\t%8.2f ", total_commission, m / num_agents);
return 0; } /*end main*/
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started