Thread: Frustration with Homework assignment

  1. #1
    Registered User
    Join Date
    Oct 2001
    Posts
    3

    Frustration with Homework assignment

    I have been given a homework assignment on arrays and I have found that my instructor is not very helpful. Practically everything I have learned has been on my own. I am having big trouble grasping the concept of how to sort arrays and getting info out of the text file. Can anyone help?

    My assignment is to scan the text file for emp_id, wage, hours. Calculate the week_pay, sort by emp_id in descending order and print the info to a text file.

    When I do a watch and trace into the program I am finding that the values of these variable is incorrect.....I also get a run-time error: Floating Point Overflow.......I believe this error is generated because the info is not being scanned properly??? Any help you can give me would be greatly appreciated.


    Here is the copy of the code I am using:

    #include<stdio.h>
    #include<conio.h>
    #include<stdlib.h>
    #define MAX 10

    void read_record(long *emp_id, float *wage, float *hours, int *record_num);

    void calc_pay(float *wage, float *hours, float *week_pay, int record_num);

    void print_results(const long *emp_id, const float *wage, const float *hours, const float *week_pay,int record_num);

    void sort_array(long *emp_id, float *wage, float *hours, float *week_pay, int record_num);


    int main ()
    {

    long emp_id[MAX];
    float wage[MAX], hours[MAX], week_pay[MAX];
    int record_num;

    read_record(emp_id, wage, hours, &record_num);
    calc_pay(wage, hours, week_pay, record_num);
    sort_array(emp_id, wage, hours, week_pay, record_num);
    print_results(emp_id, wage, hours, week_pay, record_num);



    return(0);
    }

    void read_record(long *emp_id, float *wage, float *hours,int *record_num)
    {
    int index = 0, stat;
    FILE *infile;

    infile = fopen("a:\\empout.txt","r");
    if (infile == NULL)
    {
    printf("\nError reading file.");
    exit(1);
    }

    stat = fscanf(infile, "%ld %f %f",&emp_id[index],&wage[index],&hours[index]);
    while (index < MAX)
    {
    index = index + 1;
    stat = fscanf(infile, "%ld, %f, %f", &emp_id[index], &wage[index], &hours[index]);
    }
    *record_num = index;
    fclose(infile);
    }

    void calc_pay(float *wage, float *hours, float *week_pay, int record_num)
    {
    int i = 0;

    while (i < record_num)
    {
    week_pay[i] = wage[i] * hours[i];
    i = i + 1;
    }
    }


    void print_results(const long int *emp_id, const float *wage, const float *hours, const float *week_pay,int record_num)

    {

    int index;
    FILE *outfile;

    outfile = fopen("a:\\empout.rpt", "w");
    if (outfile == NULL)
    {
    printf("\nError reading outfile.");
    exit(1);
    }
    fprintf(outfile,"\nEMPLOYEE ID\t\tWAGE\t\tHOURS\t\tWEEKLY PAY");
    for(index = 0; index < record_num; index++)
    {
    fprintf(outfile, "\n%ld\t $%.2f\t %.2f\t $%.2f\n",emp_id[index],wage[index],hours[index],week_pay[index]);
    }
    fclose(outfile);
    }


    void sort_array(long int *emp_id, float *wage, float *hours, float *week_pay, int record_num)
    {
    int x, y;
    long int temp;
    float temp1, temp2, temp3; file://temp = emp_id, temp1 = wage, temp2 = hours, temp3 = week_pay

    for (x = 0; x < record_num; x++)
    {
    for(y = record_num - 1; y > x; y--)
    if(emp_id[y-1] > emp_id[y])
    {
    temp = emp_id[y-1];
    emp_id[y-1] = emp_id[y];
    emp_id[y] = temp;
    }
    /* else if (emp_id[y] = temp)
    {
    temp1 = wage[y];
    temp2 = hours[y];
    temp3 = week_pay[y];
    }
    }
    } */

    else if(wage[y-1] > wage[y])
    {
    temp1 = wage[y-1];
    wage[y-1] = wage[y];
    wage[y] = temp1;
    }
    else if(hours[y-1] > hours[y])
    {
    temp2 = hours[y-1];
    hours[y-1] = hours[y];
    hours[y] = temp2;
    }
    else if(week_pay[y-1] > week_pay[y])
    {
    temp3 = week_pay[y-1];
    week_pay[y-1] = week_pay[y];
    week_pay[y] = temp3;
    }
    }
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    > stat = fscanf(infile, "%ld %f %f",&emp_id[index],&wage[index],&hours[index]);
    It's a good idea to check that stat is actually 3 in this case.
    If it isn't, then some values will not have been updated (they will continue to contain garbage), which will create all sorts of problems when you come to do maths on them.

    Your sort function needs work

    > if(emp_id[y-1] > emp_id[y])
    I would suggest you call another function to swap the [y-1] and [y] elements of each array.

    Because you need to perform 4 swaps at this point.

    I don't suppose you've got as far as structures yet?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Oct 2001
    Posts
    3
    No Salem I haven't started on structures yet. I knew there was problems with the sort function....I still am not sure how to swap the values for each element in the arrays. I checked the value of stat and it starts out as 3 then changes to 1 then to 0 and stays at 0. Why would it start at 3 then change to 0.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    > Why would it start at 3 then change to 0.
    Because it's going wrong

    Ha!
    stat = fscanf(infile, "%ld %f %f",&emp_id[index],&wage[index],&hours[index]);
    stat = fscanf(infile, "%ld, %f, %f", &emp_id[index], &wage[index], &hours[index]);
    Spot the difference....

    > I still am not sure how to swap
    Well this is one
    Code:
    void swap_float ( float *a, float *b ) {
        float temp = *a;
        *a = *b;
        *b = temp;
    }
    Call it with
    swap_float ( &wage[y-1], &wage[y] );
    swap_float ( &hours[y-1], &hours[y] );
    swap_float ( &week_pay[y-1], &week_pay[y] );

    And do the same for emp_id
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  2. cool math homework assignment
    By BobMcGee123 in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-23-2007, 08:21 PM
  3. Replies: 1
    Last Post: 10-27-2006, 01:21 PM
  4. Need help with homework assignment plz..
    By RVDFan85 in forum C++ Programming
    Replies: 13
    Last Post: 10-05-2006, 10:33 PM
  5. VERY newb homework assignment
    By ajguerrero in forum C Programming
    Replies: 5
    Last Post: 04-14-2006, 12:25 AM