Answer:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
void do_charges(int j, double data[10], double *cph, double *ave_cost);
double round_money(double money);
int main(void)
{
int j=1, n=0, day, month;
double id_buffer=0.0, val_buffer=0.0, data[10], cph = 0.0, ave_cost = 0.0;
char c;
FILE *fp = fopen("usage.txt", "r");
if(fp == NULL ) printf("error\\");
fscanf(fp,"%d %d", &day, &month);
// By using conditional statement
while ( c != EOF )
{
fscanf(fp,"%lf %lf", &id_buffer, &val_buffer);
data[j] = id_buffer;
data[j+4] = val_buffer;
c = getc(fp);
j++;
}
fclose(fp);
fp = fopen("charges.txt", "w");
if(fp == NULL ) printf("error\\");
fprintf(fp,"Charges for %d/%d\\\\\\", day, month);
fprintf(fp,"Customer\t hrs used\t C.P.H.\t\t Ave. Cost\\\\");
printf("Charges for %d/%d\\\\\\", day, month);
printf("Customer\t hrs used\t C.P.H.\t\t Ave. Cost\\\\");
for(j=1;j<4;j++)
{
cph = 0;
do_charges(j, data, &cph, &ave_cost);
fprintf(fp,"%1.0lf\t\t %0.1lf\t\t %0.2lf\t\t %0.2lf\\\\",data[j], data[j+4], cph, ave_cost);
printf("%1.0lf\t\t %0.1lf\t\t %0.2lf\t\t %0.2lf\\\\",data[j], data[j+4], cph, ave_cost);
}
fclose(fp);
return 0;
}
void do_charges(int j, double data[10], double *cph, double *ave_cost)
{
int base_rate = 7.99, partial_rate = 1.99;
double remain=0.0, total_hrs=0.0, money=0.0;
/*
printf("%6.0lf %6.0lf\\", data[0], data[4]);
for(j=1;j<4;j++)
{
printf("%6.0lf %6.2lf\\", data[j], data[j+4]);
}
*/
if(data[j+4] > 10)
{
remain = data[j+4] - 10;
while(remain > 0)
{
*cph = *cph + 1.99;
if(remain/10>0) remain--;
}
*cph = *cph + 7.99;
money = *cph;
*cph = round_money(money);
total_hrs = (remain * *cph);
} else {
*cph = 7.99;
money = *cph;
*cph = round_money(money);
total_hrs = data[j+4] * 7.99;
}
*ave_cost = *cph / data[j+4];
money = *ave_cost;
*ave_cost = round_money(money);
}
double round_money(double money)
{
money = money * 100;
money = round(money);
money = money / 100;
return money;
}
Step-by-step explanation: