Help please

This is a discussion on Help please within the C Programming forums, part of the General Programming Boards category; I have 2 C programming labs that are killing me. I almost have one done, I think, but I need ...

  1. #1
    Registered User
    Join Date
    May 2010
    Posts
    8

    Unhappy Help please

    I have 2 C programming labs that are killing me. I almost have one done, I think, but I need help sorting the functions in it by Male and Female structures and also by zip code for one function. The second I have no clue where to start please help me. Attached are the files for the one I am almost done with and below is the information for the other lab.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    #define FILE_NAME "payfile.txt"
    #define MAX 55 
    #define BUF_SIZE 100 
    
    typedef struct Address
    {
        char street[17]; 
        char city[12];  
        char state[3]; 
        char zip[6];  
    }Address;
    
    typedef struct Employee
    {
        char first[8]; 
        char init[2]; 
        char last[10]; 
        Address ad;
        int age;
    	char sex[1];
    	int years;
        double pay;
    }Employee;
    
    void strSub(char s1[], char s2[], int start, int length); 
    int getEmploy(Employee s[], int max); 
    void printEmploy(Employee s[], int num); 
    void printMen(Employee s[], int num);
    int topPaywoman(Employee s[], int num); 
    int lowPayman(Employee s[], int num);
    double avgPay(Employee s[], int num); 
    void printBelowavg(Employee s[], int num);
    void printRatiomen(Employee s[], int num);
    void printSenior(Employee s[], int num);
    void printRaise(Employee s[], int num);
    void sortzip(Employee arr[], int num);
    
    
    
    
    int main()
    {
        int numEmploy, topIndex, lowIndex;
        Employee employ[MAX]; 
    
        numEmploy = getEmploy( employ, MAX );
        if( numEmploy == 0 ) return 0;
        
    	printEmploy( employ, numEmploy );
    
        printMen( employ, numEmploy );
    	
    	topIndex = topPaywoman( employ, numEmploy );
        printf("\nThe woman with the most pay is %s %s earning $%.2f per week\n",
                employ[topIndex].first, employ[topIndex].last, employ[topIndex].pay);
    
        lowIndex = lowPayman( employ, numEmploy );
        printf("\nThe man with the least pay is %s %s earning $%.2f per week\n",
                employ[lowIndex].first, employ[lowIndex].last, employ[lowIndex].pay);
    	
    	printf("\nThe average pay is $%.2f per week\n", avgPay(employ, numEmploy));
    
        
    	printBelowavg( employ, numEmploy );
    
        printRatiomen( employ, numEmploy );
    
    	printSenior( employ, numEmploy );
    
    	printRaise( employ, numEmploy );
    
    	/*sort by zip code
    	sortzip( employ, numEmploy );
        printf("\nThe employee data sorted in ascending order by zip code ...");
        printEmploy( employ, numEmploy );*/
    
        return 0;
    }
    
    void strSub(char s1[], char s2[], int start, int length) {
        int i;
        for( i = 0; i < length; ++i)
            s2[i] = s1[start+i];
        s2[i] = 0;
    }
    
    
    int getEmploy(Employee s[], int maxNum) {
        int i = 0;
        char buf[BUF_SIZE];
    	char tmpa[32];
    	char tmp[32];
        FILE* fp = fopen( FILE_NAME, "r" );
         
        while( i<maxNum && fgets( buf, BUF_SIZE, fp ) )
        {
    
            strSub( buf, s[i].first, 0, 7 );
            strSub( buf, s[i].init, 8, 1 );
            strSub( buf, s[i].last, 10, 9 );
    
            strSub( buf, s[i].ad.street, 20, 16 );
            strSub( buf, s[i].ad.city, 37, 11 );
            strSub( buf, s[i].ad.state, 49, 2 );
            strSub( buf, s[i].ad.zip, 52, 5 );
    
            strSub( buf, tmp, 58, 2 );
            s[i].age = atoi(tmp);
    
    		strSub( buf, s[i].sex, 60, 2 );
           
    		strSub( buf, tmpa, 62, 2 );
    		s[i].years = atoi(tmpa);
            
            strSub( buf, tmp, 65, 5 );
            s[i].pay = atof(tmp);
            ++i;
        }
        fclose( fp );
        return i; 
    }
    
    
    void printEmploy(Employee s[], int num) {
        int i;
         printf( "The employees are\n");
    	 for( i = 0; i<num; ++i )
        {
           
    			printf( "%s %s %s %s %s %s %s %2d %s %1d %.2f\n", s[i].first, s[i].init, s[i].last, s[i].ad.street, 
    			s[i].ad.city, s[i].ad.state, s[i].ad.zip, s[i].age, s[i].sex, s[i].years, s[i].pay );
        }
    }
    
    void printMen(Employee s[], int num) {
        int i;
    	printf("\nThe men employeed are\n");
    	for( i = 0; i<num; ++i )
        {	
    		if(s[i].sex != 'F') /*need help sorting by men*/
    		{
    		printf("%s %s %s %s %s %s %s %2d %s %1d %.2f\n", s[i].first, s[i].init, s[i].last, s[i].ad.street, 
    			s[i].ad.city, s[i].ad.state, s[i].ad.zip, s[i].age, s[i].sex, s[i].years, s[i].pay);}
        }}
    
    int topPaywoman(Employee s[], int num)
    {
        int i, j = 0;
        double top = s[0].pay;
        for(i = 1; i < num; ++i)
        {
            if(s[i].pay > top /*&& code to sort by sex still needed*/) { top = s[i].pay; j = i; }
        }
    
        return j;
    }
    
    int lowPayman(Employee s[], int num){
        int i, j = 0;
        double low = s[0].pay;
        for(i = 1; i < num; ++i)
        {
            if(s[i].pay < low /*&& code to sort by sex still needed*/) { low = s[i].pay; j = i; }
        }
    
        return j;
    }
    double avgPay(Employee s[], int num)
    {
        int i;
        double sum = 0;
        for(i = 0; i < num; ++i)
            sum += s[i].pay;
    
        return sum/num;
    }
    
    
    void printBelowavg(Employee s[], int num) {
        int i;
        double avg = avgPay(s, num);
        printf("\nThe women with pay below the average are\n");
    	for( i = 0; i < num; ++i)
        {
            if( s[i].pay < avg /*&& code to sort by sex is needed*/)
                printf("%s %s %s %.2f\n", s[i].first, s[i].init, s[i].last, 
    s[i].pay);
        }
    }
    
    void printRatiomen(Employee s[], int num) {
        int i, above = 0, below = 0;
       	double avg = avgPay(s, num);
        for( i = 0; i < num; ++i)
        {
            above += s[i].pay > avg;
    		below += s[i].pay < avg;
    		
        }/*code to sort by sex is needed*/
    	printf("\nThe ratio of men above average pay to men below average pay is\n%d : %d\n", above, below);
    }
    void printSenior(Employee s[], int num) {
        int i;
        double avg = avgPay(s, num);
        printf("\nThe men who make more than $35000 per year and have worked for over 5 years and are over 30 years old are\n");
    	for( i = 0; i < num; ++i)
        {
            
    		if( s[i].pay > (35000/52) && s[i].years > 5 && s[i].age > 30/*&& code to sort for sex needed*/)
    		{printf("%s %s %.2f\n", s[i].first, s[i].last, s[i].pay);}
        }
    }
    void printRaise(Employee s[], int num) {
        int i, y = 350;
        printf("\nThe employees who get raises are\n");
    	for(i = 0; i < num; ++i)
        {
    		if( s[i].pay < y){
    			printf("%s %s\n", s[i].first, s[i].last);
    			printf("With a raise to %.2f\n", s[i].pay * 1.1);}
        }
    }
    /*void sortzip(Employee arr[], int num)
    {
        int i, j; 
        Employee temp; 
        for(i = 1; i < num; ++i)
        {   strcpy(temp.ad.zip, arr[i].ad.zip);
    
            j = i - 1;
            while(j >= 0 && temp.ad.zip < arr[j].ad.zip)
            {
                strcpy(arr[j+1].ad.zip, arr[j].ad.zip);
    			
    			j--;
            }
    		strcpy(arr[j+1].ad.zip, temp.ad.zip);
    
        }
    }
    */
    Attached Files Attached Files
    Last edited by usmcrewcheif; 05-14-2010 at 11:02 PM.

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    8

    Unhappy Other Lab

    I definitely don't want you to do this entire thing by yourself, I want to learn it but am LOST so if you can maybe get things rolling and show me what you did through comments within the code I would greatly appreciate it. Please help me however you can.

    Here is the assignment

    Construct a C program, dbase.c, utilizing dynamic variables, which will
    retrieve, update and manipulate a small payroll database. The payroll data
    can be found in the file payfile.txt. The data for each employee should
    be read into a struct containing at least the following fields:
    • firstName - 10 characters maximum
    • lastName - 15 characters maximum
    • gender - m/f
    • tenure - integer between 0 and 50
    • rate - h/w
    • salary - float

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

    a) Read data from payroll.txt into a struct for each employee and insert
    the struct as a node into the end of a linear linked list.

    b) Output the contents of each of the nodes in the list into an easily read
    format.

    c) Traverse the list and output the number of employees.

    d) Output the first and last name of all women on the payroll.

    e) Output the first and last name and salary of all weekly employees who
    make more than $35,000 per year and who have been with the company for
    at least five years.

    f) Give a raise of $.75 per hour to all employees who are paid on an hourly
    basis and make less than $10.00 per hour; and give a raise of $50.00 per
    week to all employees who are paid on a weekly basis and make less than
    $350.00 per week. Output the first and last name and salary for each
    employee on the payroll who has received a raise.

    g) Sort the nodes of the list into alphabetical order according to last name
    and output the first and last name for each employee.

    h) The file hirefile.txt contains data for three employees to be hired by
    the company. Insert the record for each of the new employees into the
    correct location in the linear linked list.

    i) The file firefile.txt contains data for two employees to be fired by
    the company. Delete the corresponding node for each of the employees to be
    fired.

    j) Print the first and last name and salary for each employee on the payroll.

    Note: Here's a rather simple algorithm to sort a linked list. Assume list
    points to an unsorted list and sortList points to NULL.
    while list != null
    remove first node from list
    insert node into correct location in sortList
    list = sortList

    The teacher provided the list.h and list.c files for for this lab. The
    list.h header file contains the declaration for the employee struct as well
    as the function prototypes. The list.c file contains list functions for use with the employee struct for this lab.

    Code:
    // list.h
    #ifndef _LIST_H
    #define _LIST_H
    typedef struct node {
    char firstName[11];
    char lastName[16];
    char gender;
    int tenure;
    char rate;
    float salary;
    struct node *next;
    } *NODEPTR;
    NODEPTR getNode(void);
    void freeNode(NODEPTR p);
    void addNodeFront(NODEPTR *list, NODEPTR t);
    NODEPTR removeNodeFront(NODEPTR *list);
    void addNodeLast(NODEPTR *list, NODEPTR t);
    void insertNode(NODEPTR *list, NODEPTR t);
    void insertNodeAfter(NODEPTR p, NODEPTR t);
    NODEPTR removeNode(NODEPTR *list, NODEPTR t);
    NODEPTR deleteNodeAfter(NODEPTR *list, NODEPTR p);
    void error(char *msg);
    #endif
    // list.c
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include "list.h"
    // returns the address of a newly created node
    NODEPTR getNode(void) {
    NODEPTR p = (NODEPTR) malloc(sizeof(struct node));
    if (p == NULL)
    error("List overflow.");
    return(p);
    }
    // frees the node pointed to by p
    void freeNode(NODEPTR p) {
    free(p);
    }
    // adds a node to the front of the list
    void addNodeFront(NODEPTR *list, NODEPTR t) {
    t->next = *list;
    *list = t;
    }
    // removes a node to the front of the list
    NODEPTR removeNodeFront(NODEPTR *list) {
    NODEPTR p = *list;
    *list = p->next;
    return p;
    }
    // adds a node to the end of a list
    void addNodeLast(NODEPTR *list, NODEPTR t) {
    NODEPTR p;
    if (*list == NULL)
    *list = t;
    else {
    p = *list;
    while (p->next)
    p = p->next;
    p->next = t;
    }
    }
    // insert a node into its correct location within the list
    void insertNode(NODEPTR *list, NODEPTR t) {
    NODEPTR p, q;
    p = *list;
    q = NULL;
    while (p != NULL && strcmp(p->lastName, t->lastName) < 0) {
    q = p;
    p = p->next;
    }
    if (q == NULL)
    addNodeFront(list, t);
    else
    insertNodeAfter(q, t);
    }
    // inserts a node after the node pointed to by p
    void insertNodeAfter(NODEPTR p, NODEPTR t) {
    if (p == NULL) {
    printf("Void insertion.\n");
    exit(1);
    }
    t->next = p->next;
    p->next = t;
    }
    // removes a node from a list
    NODEPTR removeNode(NODEPTR *list, NODEPTR t) {
    NODEPTR p, q;
    p = *list;
    q = NULL;
    while (p != NULL && strcmp(p->firstName, t->firstName) && strcmp(p-
    >lastName, t->lastName)) {
    q = p;
    p = p->next;
    }
    return q == NULL ? removeNodeFront(list) : deleteNodeAfter(list,
    q);
    }
    // deletes the node after the node pointed to by p
    NODEPTR deleteNodeAfter(NODEPTR *list, NODEPTR p) {
    NODEPTR q;
    if (p == NULL || p->next == NULL)
    error("Void deletion.");
    q = p->next;
    p->next = q->next;
    return q;
    }
    // display an error message and aborts the program
    void error(char *msg) {
    puts(msg);
    exit(1);
    }
    (below are the summaries of the attached files)

    payfile.txt
    Debbie Starr F 3 W 1000.00
    Joan Jacobus F 9 W 925.00
    David Renn M 3 H 4.75
    Albert Cahana M 3 H 18.75
    Douglas Sheer M 5 W 250.00
    Shari Buchman F 9 W 325.00
    Sara Jones F 1 H 7.50
    Ricky Mofsen M 6 H 12.50
    Jean Brennan F 6 H 5.40
    Jamie Michaels F 8 W 150.00

    hirefile.txt
    Barry Allen M 0 H 6.75
    Nina Pinella F 0 W 425.00
    Lane Wagger M 0 W 725.00

    firefile.txt
    Jean Brennan F
    Ricky Mofsen M
    Attached Files Attached Files
    Last edited by usmcrewcheif; 05-14-2010 at 11:08 PM.

  3. #3
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Ok so what do you need to sort exactly?
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    There are very few people who will just, with no hint at all what to look for or expect, copy/paste/save/compile/run/fix your program. You need to do more than just say "Here's some stuff, fix it."

    At a glance:
    Code:
    char sex[1];
    Why do you have an array of one character?


    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Registered User
    Join Date
    May 2010
    Posts
    8
    The code for the first lab needs to sort different functions by male and female depending on the function and it also needs a function that sorts all employees by zip

    thanks in advance

  6. #6
    Registered User
    Join Date
    May 2010
    Posts
    8
    Quote Originally Posted by quzah View Post
    There are very few people who will just, with no hint at all what to look for or expect, copy/paste/save/compile/run/fix your program. You need to do more than just say "Here's some stuff, fix it."

    At a glance:
    Code:
    char sex[1];
    Why do you have an array of one character?


    Quzah.
    Its not an array its part of a structure and I tried
    Code:
    char sex[1];
    with no joy any other ideas?

  7. #7
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Quote Originally Posted by usmcrewcheif View Post
    The code for the first lab needs to sort different functions by male and female.
    You can't sort functions. This makes no sense. And what are these "different" functions, be more specific.
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  8. #8
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Of course it's an array. What do you think those brackets mean anyway? Why don't you just use a single character?
    Code:
    struct stuff
    {
        ...stuff...
        char sex;
    };

    Quzah.
    Hope is the first step on the road to disappointment.

  9. #9
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Quote Originally Posted by usmcrewcheif View Post
    Its not an array its part of a structure and I tried
    Code:
    char sex[1];
    with no joy any other ideas?
    It is an array and a useless one for that matter. You can't store anything in that because there is enough space for 1 character which will be the string terminator '\0'.
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  10. #10
    Registered User
    Join Date
    May 2010
    Posts
    8
    Quote Originally Posted by claudiu View Post
    You can't sort functions. This makes no sense. And what are these "different" functions, be more specific.
    What I meant is that there are several different functions that need code within them to sort by male or female in some different way before I display the output also as far as the "array" goes an array of 1 is actually 0 and 1 leaving room for a single int, char or whatever and then the null that's why when calling an array you start with 0. I understand how the sex[] is similar to an array but it is actually part of a structure in which an entire string of information was read into different smaller strings that I am now trying to sort that's why its just a single character.

  11. #11
    Registered User
    Join Date
    May 2010
    Posts
    8
    Why cant I figure this out? I have spent the last 8 hours trying to sort this data by the sex and its driving me crazy please help me understand what to do.

  12. #12
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    If you're sorting by any given value in the structure already, then you know how to sort by any other value in the structure. Just change what you compare. So the real question is, do you understand how to sort whatever it is you've got by some value already? If not, what are you having problems with exactly?


    Quzah.
    Hope is the first step on the road to disappointment.

  13. #13
    Registered User
    Join Date
    May 2010
    Posts
    8
    I have been sorting it the same way I have sorted everything else the only difference is that itr is a character and not a number so I must have missed something somewhere but cant figure out what it is.

Popular pages Recent additions subscribe to a feed

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21