Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Database Lab *Note this needs to be done in C, NOT C++* I use CodeBlocks for thi

ID: 3695919 • Letter: D

Question

Database Lab
*Note this needs to be done in C, NOT C++* I use CodeBlocks for this class*

This lab will focus on the use and manipulation of structures. Each section of the lab should be designed within its own function, passing parameters as necessary. You are to construct a C program that will retrieve and manipulate a company's payroll database, payfile.txt. The data for each employee should be read into a structure containing the following field identifiers:

The data for each employee should be read into a struct containing the following field indentifiers:

first      7 characters maximum

initial 1 character maximum

last      9 characters maximum

street 16 characters maximum

city      11 characters maximum

state    2 characters maximum

zip       5 characters maximum

age     integer

sex      1 character maximum (M/F)

tenure integer representing years of employment

salary double representing weekly salary


Your program should perform each of the operations indicated below. Be sure to clearly label your output for each section.

a) Read data for employees into an array of structures.

b) Output the contents of each structure into an easily read format, similar to the format of the input file.

c) Output out the first and last name of all men on the payroll.

d) Output out the first and last name of the highest paid woman on the payroll.

e) Output out the first and last name of the lowest paid man on the payroll.

f) Output out the average salary for all the employees.

g) Output out the first and last name of all women earning less than the average salary.

h) Output to three decimal places the ration of the number of men above the average salary to the number of men below the average salary.

i) Output out the first and last name of all employees who make more than $35,000 per year, have been with the company for at least 5 years, and who are over 30 years old.

j) Give a 10% raise to all employees who make less than $350.00 per week and output the first and last name and new salary for each of the employees who received the raise.

k) Sort the structures according to zip codes and output the first and last name and zip code for each of the employees. (Extra Credit)

Here is a C function, strsub(), that grabs a substring, sub, from a string, buf, given the start and end index within the string.


void strsub(char buf[], char sub[], int start, int end) {
int i, j;
for (j=0, i=start; i<=end; i++, j++) {
sub[j] = buf[i];
}
sub[j] = '';
}

This function might be useful when you read a line of data from the file and need to grab the different information from the line of data to place into the fields of the struct. Remember that arrays use zero-base indexing.

While (!feof(fp)) {
           fgets(buf, MAX, fp);
           strsub(buf, workers[i].first, 0, 6);
          strsub(buf, workers[i].first, 8, 8);
          strsub(buf, workers[i].first, 10, 18);

            ….

}

You can assume the following declaration:

#define MAX 100

The following three functions from the C library, stdlib.h and string.h, might be useful for the lab:

atoi ( ) – converts a string to an integer

atof ( ) – converts a string to a float

strcmp ( ) – compares two strings



The contents of payfile.txt are given below:

ADA A AGUSTA 33 BABBAGE ROAD LOVELACE GB 19569 28 F 2 350.50
ISSAC A ASIMOV 99 FICTION WAY AMHERST MA 63948 58 M 6 423.88
HUMPHRY R BOGART 71 SAM STREET HOLLYWOOD CA 48482 56 M 5 366.00
ALBERT G EINSTEIN 94 ENERGY WAY PRINCETON NJ 47474 67 M 8 780.00
EMMYLOU L HARRIS 66 COUNTRY ROAD NASHVILLE TN 72647 38 F 2 767.42
JAMES T KIRK 11 SPACE STREET VULCAN CA 82828 46 M 1 235.70
TED L KOPPEL 55 ABC PLACE WASHINGTON DC 37376 48 M 9 909.44
DAVID T LETTERMAN 14 WNBC AVENUE NEW YORK NY 19338 47 M 5 445.65
STEVIE R NICKS 31 MUSIC ROAD CHICAGO IL 23459 38 F 8 460.88
MONTY P PYTHON 76 SILLY STREET LONDON GB 80939 44 M 2 320.50
ROGER R RABBIT 15 LOONEY TOONS HOLLYWOOD CA 91343 24 M 4 259.53
SALLY W RIDE 21 COLUMBIA WAY HOUSTON TX 91123 30 F 9 707.80
ROD Q SERLING 11 TWLIGHT ZONE SAN DIEGO CA 93939 56 M 1 440.00
LUKE R SKYWALKER 43 MILKY WAY NEW YORK NY 12343 35 M 5 660.00

Explanation / Answer

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX 100
//Everything working as far as I know as long as ages stay under 3 digits, tenure under 2 digits and salary under 6 digits

typedef struct employees{
   char first[8];
   char initial[2];
   char last[10];
   char street[17];
   char city[12];
   char state[3];
   char zip[6];
   int age[4];
   char sex[2];
   int tenure[4];
   double salary[10];
} Employee;

//Func. Proto
void strsub(char buf[], char sub[], int start, int end);
int readEmployees(Employee list[]);
void displayEmployees(Employee list[], int maxWorkers);
void maleEmployees(Employee list[], int maxWorkers);
void highestWoman(Employee list[], int maxWorkers);
void lowestMan(Employee list[], int maxWorkers);
double averageSalary(Employee list[], int maxWorkers);
void belowAvgWomen(Employee list[], int maxWorkers, double avgSalary);
void maleSalaryRatio(Employee list[], int maxWorkers, double avgSalary);
void specialEmployee(Employee list[], int maxWorkers);
void poorEmployee(Employee list[], int maxWorkers);
void zipCode(Employee list[], Employee temp[], int maxWorkers);

int main(void) {
   int maxWorkers;
   double avgSalary = 0.00;

   Employee myWorkers[MAX];
   Employee temp[MAX];

   maxWorkers = readEmployees(myWorkers); //calc total workers
   displayEmployees(myWorkers, maxWorkers);
   maleEmployees(myWorkers, maxWorkers);
   highestWoman(myWorkers, maxWorkers);
   lowestMan(myWorkers, maxWorkers);
   avgSalary = averageSalary(myWorkers, maxWorkers); //calc avg salary
   belowAvgWomen(myWorkers, maxWorkers, avgSalary);
   maleSalaryRatio(myWorkers, maxWorkers, avgSalary);
   specialEmployee(myWorkers, maxWorkers);
   poorEmployee(myWorkers, maxWorkers);
   zipCode(myWorkers, temp, maxWorkers);

   getchar();
   return 0;
}

void strsub(char buf[], char sub[], int start, int end) {
   int i, j;
   for (j = 0, i = start; i <= end; i++, j++) {
       sub[j] = buf[i];
   }
   sub[j] = '';
}

int readEmployees(Employee list[]) {
   char buf[MAX];

   FILE *fp;
   if (!(fp = fopen("payfile.txt", "r"))) {
       printf("payfile.txt could not be opened for input.");
       getchar(); //debug added so it actually gives user time to read in MVS
       exit(1);
   }

   int i = 0;
   while (!feof(fp)) {
       fgets(buf, MAX, fp);
       strsub(buf, list[i].first, 0, 6);
       strsub(buf, list[i].initial, 8, 8);
       strsub(buf, list[i].last, 10, 18);
       strsub(buf, list[i].street, 20, 35);
       strsub(buf, list[i].city, 37, 46);
       strsub(buf, list[i].state, 48, 49);
       strsub(buf, list[i].zip, 50, 55);
       strsub(buf, (char *) list[i].age, 57, 58);           //Ensure typecast of (char *) to avoid 4133 error
       *list[i].age = atoi((char *) list[i].age);           //Ensure typecast of (char *) to avoid 4133 error
       strsub(buf, list[i].sex, 60, 61);
       strsub(buf, (char *) list[i].tenure, 62, 62);       //Ensure typecast of (char *) to avoid 4133 error
        *list[i].tenure = atoi((char *) list[i].tenure);   //Ensure typecast of (char *) to avoid 4133 error
       strsub(buf, (char *) list[i].salary, 63, 69);       //Ensure typecast of (char *) to avoid 4133 error
       *list[i].salary = atof((char *) list[i].salary);   //Ensure typecast of (char *) to avoid 4133 error
       i++;
   }

   fclose(fp);
   return i;
}

void displayEmployees(Employee list[], int maxWorkers) {
   int i = 0;

   printf("EMPLOYEE LIST: ");
   printf("-------------- ");
   printf("Full Name %9c Street %10c City %8c State %2c Zip %4c Age %c M/F %c Tenure %c Salary ",
       ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ');

   while (i < maxWorkers) {
       printf("%7s %1s %9s %16s %11s %5s %4c %s %5d %2c %3s %3d Years %7.2lf ", list[i].first, list[i].initial,
           list[i].last, list[i].street, list[i].city, list[i].state, ' ',
           list[i].zip, *list[i].age, ' ', list[i].sex, *list[i].tenure, *list[i].salary);
       i++;
   }
}

void maleEmployees(Employee list[], int maxWorkers) {
   int i, j;
   char m[] = "M";

   printf(" Male Workers ");
   printf("------------ ");

   i = 0;
   while (i < maxWorkers) {
       j = (strcmp(list[i].sex, m));
       if (j > 0) {
           printf("%s %s ", list[i].first, list[i].last);
       }
       i++;
   }
}

void highestWoman(Employee list[], int maxWorkers) {
   int i, j, k;
   double salary;
   char m[] = "M";

   i = 0;
   salary = 0.00;
   while (i < maxWorkers) {
       j = (strcmp(list[i].sex, m));
       if ((j < 0) && (*list[i].salary >= salary)) {
           salary = *list[i].salary;
           k = i;
           //printf("%.2lf ", salary);
       }
       i++;
   }
   printf(" Highest Paid Woman ");
   printf("------------------ ");
   printf("%s %s ", list[k].first, list[k].last);
}

void lowestMan(Employee list[], int maxWorkers) {
   int i, j, k, initialFlag;
   double salary;
   char m[] = "M";

   initialFlag = 0;
   for (i = 0; i < maxWorkers; i++) {
       j = (strcmp(list[i].sex, m));
       if ((j > 0) && (initialFlag == 0)) {
           salary = *list[i].salary;
           k = i;
           initialFlag = 1;
       }
       else if ((j > 0) && (initialFlag == 1) && (*list[i].salary < salary)) {
           salary = *list[i].salary;
           k = i;
       }
       else
           ;
   }
   printf(" Lowest Paid Man ");
   printf("--------------- ");
   printf("%s %s ", list[k].first, list[k].last);
}

double averageSalary(Employee list[], int maxWorkers) {
   int i;
   double salary = 0.00, totalAverage;

   printf(" Average Salary ");
   printf("-------------- ");

   i = 0;
   while (i < maxWorkers) {
       salary += *list[i].salary;
       i++;
   }
   totalAverage = (salary / (double)maxWorkers);
   printf("%.2lf ", totalAverage);

   return totalAverage;
}

void belowAvgWomen(Employee list[], int maxWorkers, double avgSalary) {
   int i, j;
   char m[] = "M";

   printf(" Women Below Average Salary ");
   printf("-------------------------- ");

   i = 0;
   while (i < maxWorkers) {
       j = (strcmp(list[i].sex, m));
       if ((j < 0) && (*list[i].salary < avgSalary)) {
           printf("%s %s ", list[i].first, list[i].last);
       }
       i++;
   }

}

void maleSalaryRatio(Employee list[], int maxWorkers, double avgSalary) {
   int i, j;
   double above, below, ratio;
   char m[] = "M";

   printf(" Male Workers ");
   printf("------------ ");

   i = 0;
   above = 0.0;
   below = 0.0;
   ratio = 0.0;
   while (i < maxWorkers) {
       j = (strcmp(list[i].sex, m));
       if ((j > 0) && (*list[i].salary > avgSalary)) {
           above += 1.0;
       }
       if ((j > 0) && (*list[i].salary <= avgSalary)) {
           below += 1.0;
       }
       i++;
   }

   if (below > above) {
       ratio = below / above;
       printf("Ratio is %.2lf below average to 1 above average ", ratio);
   }
   else if (below < above) {
       ratio = above / below;
       printf("Ratio is %.2lf above average to 1 below average ", ratio);
   }
   else {
       printf("Ratio is 1 above to 1 below.");
   }

   printf("Below Average: %.2lf ", below);
   printf("Above Average: %.2lf ", above);
}

void specialEmployee(Employee list[], int maxWorkers) {
   int i;
   double expensiveSalary = 35000.00;
   int veteranEmployee = 5;
   int notYoungAnymore = 30;

   printf(" These Employees are Special: ");
   printf("---------------------------- ");

   i = 0;
   while (i < maxWorkers) {
       if ((*list[i].salary * 52) >= expensiveSalary){
           if ((*list[i].tenure) >= veteranEmployee) {
               if ((*list[i].age) >= notYoungAnymore) {
                   printf("%s %s ", list[i].first, list[i].last);
               }
           }
       }
       i++;
   }
}

void poorEmployee(Employee list[], int maxWorkers) {
   int i;
   double poorSalary = 350.00;
   double raisePercent = 0.10;

   printf(" These Employees are receiving a %.2lf percent raise: ", raisePercent * 100);
   printf("---------------------------------------------------- ");

   i = 0;
   while (i < maxWorkers) {
       if ((*list[i].salary) <= poorSalary) {
           *list[i].salary += (*list[i].salary) * (raisePercent);
           printf("%s %s New Salary: %.2lf ", list[i].first, list[i].last, *list[i].salary);
       }
       i++;
   }

}

void zipCode(Employee list[], Employee temp[], int maxWorkers) {
   int i, j;

   printf(" Zip Codes ");

   for (i = 1; i < maxWorkers; i++)
       for (j = 0; j < maxWorkers - i; j++){
           if (strcmp(list[j].zip, list[j + 1].zip) > 0) {
               temp[j] = list[j];
               list[j] = list[j + 1];
               list[j + 1] = temp[j];
           }
   }
   for (i = 0; i < maxWorkers; i++) {
       printf("%s %s %s ", list[i].first, list[i].last, list[i].zip);
   }

}

payfile.txt
ADA     A AGUSTA    33 BABBAGE ROAD LOVELACE   GB 19569 28 F 2 350.50
ISSAC   A ASIMOV    99 FICTION WAY   AMHERST    MA 63948 58 M 6 423.88
HUMPHRY R BOGART    71 SAM STREET    HOLLYWOOD CA 48482 56 M 5 366.00
ALBERT G EINSTEIN 94 ENERGY WAY    PRINCETON NJ 47474 67 M 8 780.00
EMMYLOU L HARRIS    66 COUNTRY ROAD NASHVILLE TN 72647 38 F 2 767.42
JAMES   T KIRK      11 SPACE STREET VULCAN     CA 82828 46 M 1 235.70
TED     L KOPPEL    55 ABC PLACE     WASHINGTON DC 37376 48 M 9 909.44
DAVID   T LETTERMAN 14 WBNC AVENUE   NEW YORK   NY 19338 47 M 5 445.65
STEVIE R NICKS     31 MUSIC ROAD    CHICAGO    IL 23459 38 F 8 460.88
MONTY   P PYTHON    76 SILLY STREET LONDON     GB 80939 44 M 2 320.50
ROGER   R RABBIT    15 LOONEY TOONS HOLLYWOOD CA 91343 24 M 4 259.53
SALLY   W RIDE      21 COLUMBIA WAY HOUSTON    TX 91123 30 F 9 707.80
ROD     Q SERLING   11 TWLIGHT ZONE SAN DIEGO CA 93939 56 M 1 440.00
LUKE    R SKYWALKER 43 MILKY WAY     NEW YORK   NY 12343 35 M 5 660.00
TESTER T TESTMEN   00 TEST ROAD     TEST CITY TT 11111 40 M 9 805.00

EMPLOYEE LIST:                                                                                                                                              
--------------                                                                                                                                              
Full Name           Street            City          State    Zip      Age   M/F   Tenure    Salary                                                          
ADA     A AGUSTA    33 BABBAGE ROAD   LOVELACE      GB       19569    28     F    2 Years   350.50                                                          
ISSAC   A ASIMOV    99 FICTION WAY    AMHERST       MA       63948    58     M    6 Years   423.88                                                          
HUMPHRY R BOGART    71 SAM STREET     HOLLYWOOD     CA       48482    56     M    5 Years   366.00                                                          
ALBERT G EINSTEIN 94 ENERGY WAY     PRINCETON     NJ       47474    67     M    8 Years   780.00                                                          
EMMYLOU L HARRIS    66 COUNTRY ROAD   NASHVILLE     TN       72647    38     F    2 Years   767.42                                                          
JAMES   T KIRK      11 SPACE STREET   VULCAN        CA       82828    46     M    1 Years   235.70                                                          
TED     L KOPPEL    55 ABC PLACE      WASHINGTON    DC       37376    48     M    9 Years   909.44                                                          
DAVID   T LETTERMAN 14 WBNC AVENUE    NEW YORK      NY       19338    47     M    5 Years   445.65                                                          
STEVIE R NICKS     31 MUSIC ROAD     CHICAGO       IL       23459    38     F    8 Years   460.88                                                          
MONTY   P PYTHON    76 SILLY STREET   LONDON        GB       80939    44     M    2 Years   320.50                                                          
ROGER   R RABBIT    15 LOONEY TOONS   HOLLYWOOD     CA       91343    24     M    4 Years   259.53                                                          
SALLY   W RIDE      21 COLUMBIA WAY   HOUSTON       TX       91123    30     F    9 Years   707.80                                                          
ROD     Q SERLING   11 TWLIGHT ZONE   SAN DIEGO     CA       93939    56     M    1 Years   440.00                                                          
LUKE    R SKYWALKER 43 MILKY WAY      NEW YORK      NY       12343    35     M    5 Years   660.00                                                          
TESTER T TESTMEN   00 TEST ROAD      TEST CITY     TT       11111    40     M    9 Years   805.00                                                          
                                                                                                                                                            
Male Workers                                                                                                                                                
------------                                                                                                                                                
ISSAC   ASIMOV                                                                                                                                              
HUMPHRY BOGART                                                                                                                                              
ALBERT EINSTEIN                                                                                                                                            
JAMES   KIRK                                                                                                                                                
TED     KOPPEL                                                                                                                                              
DAVID   LETTERMAN                                                                                                                                           
MONTY   PYTHON                                                                                                                                              
ROGER   RABBIT                                                                                                                                              
ROD     SERLING                                                                                                                                             
LUKE    SKYWALKER                                                                                                                                           
TESTER TESTMEN                                                                                                                                             
Highest Paid Woman                                                                                                                                          
------------------                                                                                                                                          
EMMYLOU HARRIS                                                                                                                                              
                                                                                                                                                            
Lowest Paid Man                                                                                                                                             
---------------                                                                                                                                             
JAMES   KIRK                                                                                                                                                
                                                                                                                                                            
Average Salary                                                                                                                                              
--------------                                                                                                                                              
528.82                                                                                                                                                      
                                                                                                                                                            
Women Below Average Salary                                                                                                                                  
--------------------------                                                                                                                                  
ADA     AGUSTA                                                                                                                                              
STEVIE NICKS                                                                                                                                               

Male Workers                                                                                                                                                
------------                                                                                                                                                
Ratio is 1.75 below average to 1 above average                                                                                                              
Below Average: 7.00                                                                                                                                         
Above Average: 4.00                                                                                                                                         
                                                                                                                                                            
These Employees are Special: