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?
&&'s and||'s forifs to put the boolean operator on the start of the next line, so it's clear it's a continuation of the previous statement. I was confusing on seeing thefscanfand thought it was another statement, but it was still part of theif. That's just preference though.