My Program

This is a discussion on My Program within the C Programming forums, part of the General Programming Boards category; I had to write this code for a CIS course. It reads a .txt file into an array of structures ...

  1. #1
    Unregistered
    Guest

    Unhappy My Program

    I had to write this code for a CIS course. It reads a .txt file into an array of structures and the formats it and produces a nice output. Sorted by Name of course. I do not think that the code is very efficient even though it works. I was just wondering if anyone had any pointers for speeding it up.

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

    /*definition of the structure to be used, it is initialized as
    an array containing 14 members.13 are to hold the provided info
    and the 14th is to effect the structure bubbleSort*/
    struct info {
    int id;
    char name[10];
    int performance;
    float currentSal;
    float raise;
    float percentRaise;
    float newSal;
    } employee[14];

    /*used open the input file and fill the array of structures with the imported
    data*/
    int fillStruct(struct info employ[],FILE *, char *, int);

    /*sorts the structure using a modified bubblesort*/
    void structSort(struct info employ[],const int);

    /*this function opens the outputfile and prints the headers and columns
    all data printing is accomplished thru sub-functions*/
    void openOutFilePrintData(struct info employ[],FILE *,char *,const int);

    /*a subfunction of above...prints the members of the structure array*/
    void printFillStructData(struct info employ[],FILE *,const int);

    /*another sub function...calculates and prints the summary data8/
    void printSummaryData(struct info employ[],FILE *, const int);

    int main()
    {
    FILE *ifPtr; /*file pointer for reading from source file*/
    FILE *ofPtr; /*file pointer for writing to output file*/

    char *employPtr; /*pointer used to pass the array employee to various functions*/
    char *infile = "F:\\cis211\\inputFile.txt"; /*location of input file*/
    char *outfile = "F:\\cis211\\outputFile.txt"; /*location of output file*/

    int count=0; /*variable used for counting loops etc...is passed to the
    first function as a standard integer and has its value set
    in fillStruct.From that point on it is passed as a const int*/

    /*this funtion call opens and fills the structure array, it is passed the
    structure array,a file ptr,a char ptr containing the address of the input
    file, and the int count.After all work is performed it closes the input
    file.*/
    count = fillStruct(employee, ifPtr, infile, count);

    /*this function call sorts employee.it is passed the structure array, and
    count(as a const integer)*/
    structSort(employee, count);

    /*this function call opens the output file, prints headers, and calls 2
    other functions to print the structure array data, and to calculate and
    print the summary fields.*/
    openOutFilePrintData(employee,ofPtr,outfile,count) ;

    return(0);

    }

    /*this function first assigns the char * to the FILE *(while opening the input
    file), it then checks to insure the file was actually opened. Then it scans
    the file using fscanf.The condition of the while loop is == 4. This works
    because fscanf keeps a tally of how many pieces of data were read from the
    input file.*/
    int fillStruct(struct info employ[],FILE *inFilePtr, char *inFileLocation, int ctr)
    {
    if((inFilePtr=fopen(inFileLocation,"r"))!=NULL) /*opens source file for reading and*/
    { /*checks to make sure it was opened*/
    /*if the if is true then this portion of code begins reading the values
    into the array of structures(employee), the storing of values in the
    structure is done in the while loop itself*/
    while((fscanf(inFilePtr,"%d %s %d %f",&employ[ctr].id,employ[ctr].name,
    &employ[ctr].performance,&employ[ctr].currentSal)) ==4){
    /*this portion checks the performance code and assigns a different
    raise percentage based on their poerformance. It also calculates
    employee[ctr]'s raise and new salary*/
    switch(employ[ctr].performance){
    case 1:
    employ[ctr].percentRaise = .045;
    employ[ctr].raise = employ[ctr].currentSal * employ[ctr].percentRaise;
    employ[ctr].newSal = employ[ctr].currentSal + employ[ctr].raise;
    break;
    case 2:
    employ[ctr].percentRaise = .06;
    employ[ctr].raise = employ[ctr].currentSal * employ[ctr].percentRaise;
    employ[ctr].newSal = employ[ctr].currentSal + employ[ctr].raise;
    break;
    case 3:
    employ[ctr].percentRaise = .078;
    employ[ctr].raise = employ[ctr].currentSal * employ[ctr].percentRaise;
    employ[ctr].newSal = employ[ctr].currentSal + employ[ctr].raise;
    break;
    case 4:
    employ[ctr].percentRaise = .09;
    employ[ctr].raise = employ[ctr].currentSal * employ[ctr].percentRaise;
    employ[ctr].newSal = employ[ctr].currentSal + employ[ctr].raise;
    break;
    }
    /*increments the variable count.this variable is used to terminate
    most loops*/
    ctr++;

    }
    }
    /*if the input file fails to open then the following code is executed*/
    else {
    printf("Error opening file to read\n");
    exit(1);
    }

    /*returns ctr, which is used as a terminating condition for the majority of
    loops throughout the program*/
    return ctr;
    }

    /*this function sorts the structure array based on their name member. it uses
    the function strcmp to perform this. if the first argument of strcmp is
    greater than its' second, the function returns a value greater than 1.*/
    void structSort(struct info employ[], const int ctr)
    {
    //variables used for controlling the sort loop
    int pass,x;

    for(pass=1;pass<=ctr-1; pass++)
    for(x=0;x<=ctr-2;x++){
    /*if the current name is of greater value(strcmp) than the next
    value, it switchs their places, making use of an extra empty structure
    included in the array*/
    if(strcmp(employ[x].name, employ[x+1].name) > 0)
    {
    employ[13] = employ[x];
    employ[x] = employ[x+1];
    employ[x+1] = employ[13];
    }
    }
    }

    /*this function opens the output file, prints the headers and calls 2 other
    functions. it also checks to see if the output file was opened*/
    void openOutFilePrintData(struct info employ[],FILE *outFilePtr,
    char *outFileLocation,const int ctr)
    {

    if((outFilePtr = fopen(outFileLocation,"w"))!=NULL) /*opens the file for writing and checks*/
    { /*to make sure it was opened*/


    /*the fprintfs print the column headers and table separators*/
    fprintf(outFilePtr,"\t\t\t\t%s\n","Increase");
    fprintf(outFilePtr,"\t\t\t%s\t%s\t%s\n","Current", "--------------","New");
    fprintf(outFilePtr,"%s\t%s\t%s\t%s\t%s\t%s\t%s\n", "ID","Name","PR","Salary",
    "$","%","Salary");
    fprintf(outFilePtr,"%s","========================= ===============================\n");

    /*this function prints(to the outputfile) the structure array and all its
    members(minus the extra member used to switch values*/
    printFillStructData(employ,outFilePtr,ctr);

    /*prints the table separator before the summary data*/
    fprintf(outFilePtr,"%s","========================= ===============================\n");

    /*calculates and prints the summary data*/
    printSummaryData(employ,outFilePtr,ctr);
    }

    /*if the output file had failed to be opened this error message is executed*/
    else
    {
    fprintf(stderr,"cannot open %s.\n",outFileLocation);
    getchar();
    exit(0);
    }

    }


    /*this function prints the contents of the structure array, minus the extra
    structure*/
    void printFillStructData(struct info emp[],FILE *ofPtr, const int ct)
    {
    int i;

    /*this loops through the various records*/
    for(i=0;i<ct;i++)
    {
    fprintf(ofPtr,"%d\t%s\t%d\t%.0f\t%.0f\t%.2f\t%.0f\ n",emp[i].id,emp[i].name,
    emp[i].performance,emp[i].currentSal,emp[i].raise,
    emp[i].percentRaise * 100, emp[i].newSal);
    }
    }

    /*this function calculates and prints the summary data*/
    void printSummaryData(struct info emp[],FILE *ofPtr,const int ct)
    {
    /*variables used in the calculation of the summary and summary data*/
    int i;
    float sumCurrentSal; /*used to hold the total of current salaries*/
    float sumRaise=0; /*holds the sum of the raises*/
    float avgPercentRaise=0;/*holds the sum of raise percentages and is then / by 2*/
    float sumNewSal=0; /*sum of the new salaries, after the raises*/

    /*this loops through the structures and performs various calculations*/
    for(i=0;i<ct;i++)
    {
    sumCurrentSal += (int)emp[i].currentSal;
    sumRaise +=(int)emp[i].raise;
    avgPercentRaise += emp[i].percentRaise;
    sumNewSal += (int)emp[i].newSal;
    }

    /*used to print out the calculated fields*/
    fprintf(ofPtr,"\t%s\t%.0f\t%.0f\t%.2f\t%.0f\n","Su mmary:",sumCurrentSal,sumRaise,
    (avgPercentRaise / ct) * 100, sumNewSal);
    }

    BTW-I am using Borland 5.0 and this project is using sequential access files not random access. And it is done a few weeks ahead of time. Any help would be appreciated

    Norm

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,485
    > /*definition of the structure to be used, it is initialized as
    an array containing 14 members.13 are to hold the provided info
    and the 14th is to effect the structure bubbleSort*/
    Well this is bad (awful actually)

    Instead of messing with the last element of the array, declare a temp variable inside your sort function to take the place of employ[13]

    The only performance drag is that bubble sort - which you should replace with the qsort in stdlib.h
    The file handling is the only other expensive bit, but there's not much you can do about that

  3. #3
    Registered User
    Join Date
    Feb 2002
    Posts
    1
    thanks salem,
    I am going to have to replace that extra array element and take a look at some documentation to find out what exactly qsort is and its syntax.


    thanks again

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Issue with program that's calling a function and has a loop
    By tigerfansince84 in forum C++ Programming
    Replies: 9
    Last Post: 11-12-2008, 12:38 PM
  2. Need help with a program, theres something in it for you
    By engstudent363 in forum C Programming
    Replies: 1
    Last Post: 02-29-2008, 12:41 PM
  3. Replies: 4
    Last Post: 02-21-2008, 09:39 AM
  4. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM

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