Question

CSCI 112 C Programming Database Lab This lab will focus on the use and manipulation of...

CSCI 112 C Programming Database Lab 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, database.c, 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: 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 : float representing weekly salary. Your program should perform each of the operations indicated below. Be sure to clearly label your output for each section. Remember, each section of the lab should be designed within its own function, passing parameters as necessary.

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 the first and last name of all men on the payroll.

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

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

f) Output the average salary for all the employees.

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

h) Output the ratio of the number of men above the average salary to the number men below the average salary.

i) Output 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.

Here is a C function, strsub(), that grabs a substring, sub, from a string, buf, given the starting, start, and ending, 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] = '\0';

}

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-based indexing.

while (!feof(fp)) {

fgets(buf, MAX, fp);

strsub(buf, workers[i].first, 0, 6);

strsub(buf, workers[i].initial, 8, 8);

strsub(buf, workers[i].last, 10, 18);

...

...

}

You can assume the following declaration:

#define MAX 100

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

atoi() - converts a string to an integer

atof() - converts a string to a float Here's some code to sort an array of max integers.

strcmp() - compares two strings

You will need to modify this code to sort the array of structs for k's portion of the lab:

void insertionSort(int list[], int max) {

int i, j;

int temp;

for (i = 1; i < max; i++) {

temp = list[i]; j = i - 1;

while (j >= 0 && temp < list[j]) {

list[j+1] = list[j];

j = j - 1;

} list[j+1] = temp;

}

}

The following function from the C library, string.h, might be useful for part k.: 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

Be sure to include the output file, csis.txt, in your zip archive.

0 0
Add a comment Improve this question Transcribed image text
Answer #1


Please let me know if you have any doubts or you want me to modify the answer. And if you find this answer useful then don't forget to rate my answer as thumps up. Thank you! :)


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

#define SIZE 20
#define MAX 100

FILE *fp, *fpIn;
FILE *fpIn;

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

int main(void);
int inputData(Employee workers[]);
void outputContents (Employee workers[], int workerCounter);
void outputMen (Employee workers[], int workerCounter);
void outputBallerLady (Employee workers[], int workerCounter);
void outputPoorGuy (Employee workers[], int workerCounter);
double averageSalary (Employee workers[], int workerCounter);
void womenGetLess (Employee workers[], int workerCounter, double averageSalaries);
void ratioMen (Employee workers[], int workerCounter, double averageSalaries);
void outputVariousInfo (Employee workers[], int workerCounter);
void gettingRaise (Employee workers[], int workerCounter);

int main(void) {
    Employee workers[SIZE];
    int workerCounter;
    double averageSalaries;

    fp = fopen("/Users/swapnil/CLionProjects/Database/csis.txt", "w");


    workerCounter = inputData(workers);
    outputContents(workers, workerCounter);

    outputMen(workers, workerCounter);
    outputBallerLady(workers, workerCounter);
    outputPoorGuy(workers, workerCounter);
    averageSalaries = averageSalary(workers, workerCounter);
    womenGetLess(workers, workerCounter, averageSalaries);
    ratioMen(workers, workerCounter, averageSalaries);
    outputVariousInfo(workers, workerCounter);
    gettingRaise (workers, workerCounter);

    fclose(fpIn);
    fclose(fp);
    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] = '\0';
}

int inputData(Employee workers[]) {
    int i=0;
    char buf[MAX];
    char temp[MAX];
    FILE *fp;

    fp = fopen("/Users/swapnil/CLionProjects/Database/payfile.txt", "r");

    while (!feof(fp)) {
        fgets(buf, MAX, fp);
        strsub(buf, workers[i].first, 0, 6);
        strsub(buf, workers[i].initial, 8, 8);
        strsub(buf, workers[i].last, 10, 18);
        strsub(buf, workers[i].street, 20, 35);
        strsub(buf, workers[i].city, 37, 47);
        strsub(buf, workers[i].state, 49, 50);
        strsub(buf, workers[i].zip, 52, 56);
        strsub(buf, temp, 58, 59);
        workers[i].age = atoi(temp);
        strsub(buf, workers[i].sex, 61, 61);
        strsub(buf, temp, 63, 63);
        workers[i].tenure = atoi(temp);
        strsub(buf, temp, 65, 70);
        workers[i].salary = atof(temp);
        ++i;
    }
    fclose(fp);
    return i;
}

void outputContents (Employee workers[], int workerCounter){
    int i;
    for(i = 0; i < workerCounter; i++){
        printf(     "\n%s", workers[i].first);
        fprintf(fp, "\n%s", workers[i].first);
        printf(     " %s", workers[i].initial);
        fprintf(fp, " %s", workers[i].initial);
        printf(     " %s", workers[i].last);
        fprintf(fp, " %s", workers[i].last);
        printf(     " %s", workers[i].street);
        fprintf(fp, " %s", workers[i].street);
        printf(     " %s", workers[i].city);
        fprintf(fp, " %s", workers[i].city);
        printf(     " %s", workers[i].state);
        fprintf(fp, " %s", workers[i].state);
        printf(     " %s", workers[i].zip);
        fprintf(fp, " %s", workers[i].zip);
        printf(     " %d", workers[i].age);
        fprintf(fp, " %d", workers[i].age);
        printf(     " %s", workers[i].sex);
        fprintf(fp, " %s", workers[i].sex);
        printf(     " %d", workers[i].tenure);
        fprintf(fp, " %d", workers[i].tenure);
        printf(     " %.2f", workers[i].salary);
        fprintf(fp, " %.2f", workers[i].salary);
    }
}

void outputMen (Employee workers[], int workerCounter){
    int i;
    printf(     "\n\nAll men on the payroll:\n");
    fprintf(fp, "\n\nAll men on the payroll:\n");
    for(i = 0; i < workerCounter; i++){
        if(workers[i].sex[0] == 'M'){
            printf(     "%s %s\n", workers[i].first, workers[i].last);
            fprintf(fp, "%s %s\n", workers[i].first, workers[i].last);
        }
    }
}

void outputBallerLady (Employee workers[], int workerCounter){
    int i;
    int highestWage = 0;
    int indexOfHighestWage = 0;

    printf(     "\n\n Highest paid woman on the payroll:\n");
    fprintf(fp, "\n\n Highest paid woman on the payroll:\n");

    for(i = 0; i < workerCounter; i++){

        if(workers[i].sex[0] == 'F' && workers[i].salary > highestWage){
            highestWage = workers[i].salary;
            indexOfHighestWage = i;
    }
}
    printf(     "%s %s\n", workers[indexOfHighestWage].first, workers[indexOfHighestWage].last);
    fprintf(fp, "%s %s\n", workers[indexOfHighestWage].first, workers[indexOfHighestWage].last);

}
void outputPoorGuy (Employee workers[], int workerCounter){
    int i;
    int lowestWage = 10000;
    int indexOfLowestWage = 0;

    printf(     "\n\n Lowest paid man on the payroll:\n");
    fprintf(fp, "\n\n Lowest paid man on the payroll:\n");

    for(i = 0; i < workerCounter; i++){
        if(workers[i].sex[0] == 'M' && workers[i].salary < lowestWage){
            lowestWage = workers[i].salary;
            indexOfLowestWage = i;
        }
    }
    printf(     "%s %s\n", workers[indexOfLowestWage].first, workers[indexOfLowestWage].last);
    fprintf(fp, "%s %s\n", workers[indexOfLowestWage].first, workers[indexOfLowestWage].last);
}

double averageSalary (Employee workers[], int workerCounter){
    double allSalarysTotal = 0;
    double averageSalarys;
    int i;

    printf(     "\n\n Average salary for all employees:\n");
    fprintf(fp, "\n\n Average salary for all employees:\n");

    for(i = 0; i < workerCounter; i++){
        allSalarysTotal += workers[i].salary;

    }
    averageSalarys = allSalarysTotal / workerCounter;

    printf(     "%.2f \n", averageSalarys);
    fprintf(fp, "%.2f \n", averageSalarys);
    return averageSalarys;
}

void womenGetLess (Employee workers[], int workerCounter, double averageSalaries){
    int i;

    printf(     "\n\n All women earning less than the average salary:\n");
    fprintf(fp, "\n\n All women earning less than the average salary:\n");

    for(i = 0; i < workerCounter; i++){
        if(workers[i].sex[0] == 'F' && workers[i].salary < averageSalaries){
            printf(     "%s %s\n", workers[i].first, workers[i].last);
            fprintf(fp, "%s %s\n", workers[i].first, workers[i].last);
        }
    }
}

void ratioMen (Employee workers[], int workerCounter, double averageSalaries){
    int i;
    int numMenTotal = 0;
    int numMenEarningMore = 0;
    int numMenEarningLess = 0;
    double ratioMenEarningMore;
    double ratioMenEarningLess;

    printf(     "\n\n The ratio of men earning above the average salary:\n");
    fprintf(fp, "\n\n The ratio of men earning above the average salary:\n");

    for(i = 0; i < workerCounter; i++){
        if(workers[i].sex[0] == 'M'){
            numMenTotal++;
        }
        if(workers[i].sex[0] == 'M' && workers[i].salary > averageSalaries){
            numMenEarningMore++;
        }
    }
    ratioMenEarningMore = (double) numMenEarningMore / numMenTotal;

    printf(     "%.3f\n",ratioMenEarningMore);
    fprintf(fp, "%.3f\n",ratioMenEarningMore);

    printf(     "\n\n The ratio of men earning below the average salary:\n");
    fprintf(fp, "\n\n The ratio of men earning below the average salary:\n");

    for(i = 0; i < workerCounter; i++){
        if(workers[i].sex[0] == 'M' && workers[i].salary < averageSalaries){
            numMenEarningLess++;
        }
    }
    ratioMenEarningLess = (double) numMenEarningLess / numMenTotal;

    printf(     "%.3f\n", ratioMenEarningLess);
    fprintf(fp, "%.3f\n", ratioMenEarningLess);

}

void outputVariousInfo (Employee workers[], int workerCounter){
    int i;
    double annualSalary = 0;
    int numWeeksInYear = 52;

    printf(     "\n\n The name(s) of all employees who make more than 35k, have been with the company for at least 5 years, and who are over 30 years old:\n");
    fprintf(fp, "\n\n The name(s) of all employees who make more than 35k, have been with the company for at least 5 years, and who are over 30 years old:\n");

    for(i = 0; i < workerCounter; i++){
        annualSalary = workers[i].salary * numWeeksInYear;
        if(annualSalary > 35000 && workers[i].tenure >= 5 && workers[i].age > 30){
            printf(     "%s %s\n", workers[i].first, workers[i].last);
            fprintf(fp, "%s %s\n", workers[i].first, workers[i].last);
        }
    }
}


void gettingRaise (Employee workers[], int workerCounter){
    int i;
    double raiseAmount;
    double newRaisedSalaryAmount;

    printf(     "\n\n All employees who made less than $350 per week received a raise of 10 percent. Below are their names and their new salary amounts:\n");
    fprintf(fp, "\n\n All employees who made less than $350 per week received a raise of 10 percent. Below are their names and their new salary amounts:\n");
    for(i = 0; i < workerCounter; i++){
        if(workers[i].salary < 350.00){
            raiseAmount = workers[i].salary * .10;
            newRaisedSalaryAmount = raiseAmount + workers[i].salary;
            printf(     "%s %s %.2f\n", workers[i].first, workers[i].last, newRaisedSalaryAmount);
            fprintf(fp, "%s %s %.2f\n", workers[i].first, workers[i].last, newRaisedSalaryAmount);
        }
    }
}



Add a comment
Know the answer?
Add Answer to:
CSCI 112 C Programming Database Lab This lab will focus on the use and manipulation of...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Use the HR database for all the questions in this lab. For each item in this lab, paste the SQL y...

    Use the HR database for all the questions in this lab. For each item in this lab, paste the SQL you used to complete the task and the number of rows retrieved. Ensure you follow the coding standards listed in the ETSU SQL standards guide found on D2L. Save this document as a PDF and name it your_name_lab7.pdf Display the employees’ first name, last name and phone number of employees with salaries greater than 10,000 dollars. SELECT first_name, last_name, phone_number,...

  • Use the code below to answer the questions that follow. Assume that all proper libraries and...

    Use the code below to answer the questions that follow. Assume that all proper libraries and name spaces are included and that the code will compile without error. Within the main function, for the variable int last_sid , write in the last digit of your SMC student ID. int foo(int a, int b) { //First int c = a+b; while(c>=3) c-=3; return c; } //------------------------------------ char foo(string a, int b) { //Second return a[b]; } //------------------------------------ string foo(int b, string...

  • I need only one  C++ function . It's C++ don't write any other language. Hello I need...

    I need only one  C++ function . It's C++ don't write any other language. Hello I need unzip function here is my zip function below. So I need the opposite function unzip. Instructions: The next tools you will build come in a pair, because one (zip) is a file compression tool, and the other (unzip) is a file decompression tool. The type of compression used here is a simple form of compression called run-length encoding (RLE). RLE is quite simple: when...

  • Write in C Spring 2016 Lab Assignment 11 ET2100 In computer programming in general a "string"...

    Write in C Spring 2016 Lab Assignment 11 ET2100 In computer programming in general a "string" is a sequence of characters. In the C language anything within double quotes is a "string constant" so you have been seeing strings all semester. But we can also have string variables. In the C language these are implemented as an array of char, e.g. char name (10]: In order to make these variables easier to work with, it has been universally agreed that...

  • Need C++ coding for this lab exercise given below :) 4. Lab Exercise — Templates Exercise...

    Need C++ coding for this lab exercise given below :) 4. Lab Exercise — Templates Exercise 1 - Function Templating For this part of the lab make a template out of the myMax function and test it on different data types. Copy maxTemplate.cpp to your Hercules account space. Do that by: Entering the command:cp /net/data/ftp/pub/class/115/ftp/cpp/Templates/maxTemplate.cpp maxTemplate.cpp Compile and run the program to see how it works. Make a template out of myMax. Don't forget the return type. Modify the prototype appropriately. Test your myMax template on int, double,...

  • Deliverable A zipped NetBeans project with 2 classes app ZipCode Address Classes Suggestion: Use Netbeans to...

    Deliverable A zipped NetBeans project with 2 classes app ZipCode Address Classes Suggestion: Use Netbeans to copy your last lab (Lab 03) to a new project called Lab04. Close Lab03. Work on the new Lab04 project then. The Address Class Attributes int number String name String type ZipCode zip String state Constructors one constructor with no input parameters since it doesn't receive any input values, you need to use the default values below: number - 0 name - "N/A" type...

  • Any help in the compiler error //Driver Program //***************** /** * * CSCI 241 Assignment 8...

    Any help in the compiler error //Driver Program //***************** /** * * CSCI 241 Assignment 8, Part 3 * * Author: your name * z-ID: your z-ID * Date: due date of assignment * * This program builds, sorts and prints lists using the quicksort and * merge sort algorithms. */ #include <iostream> #include <iomanip> #include <vector> #include <string> #include "sorts.h" #include "quicksort.h" #include "mergesort.h" using std::cout; using std::fixed; using std::left; using std::setprecision; using std::string; using std::vector; // Data files...

  • Expand the payroll program to combine two sorting techniques (Selection and Exchange sorts) for better efficiency...

    Expand the payroll program to combine two sorting techniques (Selection and Exchange sorts) for better efficiency in sorting the employee’s net pay. //I need to use an EXSEL sort in order to combine the selection and Exchange sorts for my program. I currently have a selection sort in there. Please help me with replacing this with the EXSEL sort. //My input files: //My current code with the sel sort. Please help me replace this with an EXSEL sort. #include <fstream>...

  • Need this in C The starter code is long, if you know how to do it...

    Need this in C The starter code is long, if you know how to do it in other way please do. Do the best you can please. Here's the starter code: // ----------------------------------------------------------------------- // monsterdb.c // ----------------------------------------------------------------------- #include #include #include // ----------------------------------------------------------------------- // Some defines #define NAME_MAX 64 #define BUFFER_MAX 256 // ----------------------------------------------------------------------- // Structs typedef struct { char name[NAME_MAX]; int hp; int attackPower; int armor; } Character; typedef struct { int size; Character *list; } CharacterContainer; // ----------------------------------------------------------------------- //...

  • Lab 2: (one task for Program 5): Declare an array of C-strings to hold course names,...

    Lab 2: (one task for Program 5): Declare an array of C-strings to hold course names, and read in course names from an input file. Then do the output to show each course name that was read in. See Chapter 8 section on "C-Strings", and the section "Arrays of Strings and C-strings", which gives an example related to this lab. Declare the array for course names as a 2-D char array: char courseNames[10] [ 50]; -each row will be a...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT