The file input.txt is given in the current directory and its structure is as follows: in each line there is the name and surname of the worker, year of employment, and then 12 real numbers that represent the amount of salary during each month in 2015. Assume that there are no spaces in the name and surname and that they contain a maximum of 19 letters. The file contains up to 1000 workers, if there are more than 1000, the first 1000 should be loaded.
Function write should print all workers who were employed during 2010 and whose average salary during all 12 months was higher than 1000. The file format should be: name, surname, and the amount of the average salary, separated by a space. The amount should be rounded to two decimal places, and each worker should be in a separate row.
EXAMPLE of file input:
A a 2010 999.00 999.00 999.00 999.00 999.00 999.00 999.00 999.00 999.00 999.00 999.00 999.00
B b 2012 1001.00 1001.00 1001.00 1001.00 1001.00 1001.00 1001.00 1001.00 1001.00 1001.00 1001.00 1001.00
C c 2010 1001.00 1001.00 1001.00 1001.00 1001.00 1001.00 1001.00 1001.00 1001.00 1001.00 1001.00 1001.00
OUTPUT SHOULD BE:
C c 1001.00
MY OUTPUT:
C c %le
Code:
#include <stdio.h>
struct Worker {
char name[20], surname[20];
int year;
double salary[12];
} arr[1000];
int number_of_workers = 0;
void load() {
FILE *input = fopen("input.txt", "r");
int i=0;
while ((fscanf(input, "%s %s %d %lf %lf %lf %lf %lf %lf %lf %lf %lf %lf %lf %lf",
arr[i].name, arr[i].surname, &arr[i].year, &arr[i].salary[0],
&arr[i].salary[1], &arr[i].salary[2], &arr[i].salary[3],
&arr[i].salary[4],&arr[i].salary[5],&arr[i].salary[6],
&arr[i].salary[7],&arr[i].salary[8],&arr[i].salary[9],
&arr[i].salary[10],&arr[i].salary[11])) == 15 && i < 1000)
{
number_of_workers ;
i ;
}
fclose(input);
number_of_workers = i;
}
double average(int k, int n) {
int i;
double sum = 0;
for (i = 0; i < n; i )
sum = arr[k].salary[i];
return sum / 12;
}
void write() {
int i;
double average_salary;
FILE *output = fopen("output.txt", "w");
for (i = 0; i < number_of_workers; i ) {
average_salary = average(i, 12);
if (arr[i].year == 2010 && average_salary > 1000) {
fprintf(output, "%-20s%-20s%-%le\n", arr[i].name, arr[i].surname,
average_salary);
}
}
fclose(output);
}
int main() {
load();
write();
return 0;
}
Could you help me to instead of %le
my program prints average_salary (1001.00)?? How to fix this?
CodePudding user response:
First of all you need to initialize i to 0. You're declaring it, but never setting it to anything so it's just random data. This means you could be skipping the while
and never reading in data.
void load(struct Worker arr[], int number_of_workers) {
const char in[] = "input.txt";
FILE *input = fopen(in, "r");
---> int i
if (dat == NULL) {
Everything else seems fine to me.
CodePudding user response:
This:
fprintf(output, "%-20s%-20s%-%le\n", arr[i].name, arr[i].surname, average_salary);
was the only thing wrong. Correct would be:
fprintf(output, "%-20s%-20s%%.2f\n", arr[i].name, arr[i].surname, average_salary);