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;
}
}
}