I need to modify a program I\'ve written for a C Language course. The original a
ID: 3572268 • Letter: I
Question
I need to modify a program I've written for a C Language course. The original assignment was to write a program to get initial inventory information from the user for a beer distibutorship, process weeky sales, and then display the final inventory. The original program read data from keyboard input. However, i need to modify it to recieve input from a file (beerinput.txt) and print the answers to another file (beeroutput.txt). Chegg wont allow me to attach a non-image file, so I will paste the code from my original program.
#include <stdio.h>
int
main (void)
{
const int WEEK_DAYS = 5;
const int TOTAL_BRANDS = 4;
int amount[WEEK_DAYS][TOTAL_BRANDS];
int finalAmount[TOTAL_BRANDS];
printf("Beer Brand IDs ");
printf("1. Piels ");
printf("2. Coors ");
printf("3. Bud ");
printf("4. Iron City ");
printf(" Weekly Amount (+ for purchased or - for sold) ");
int day;
for(day = 0; day < WEEK_DAYS; day++)
{
printf(" Day #%d: ", (day + 1));
int id;
for(id = 0; id < TOTAL_BRANDS; id++)
{
printf("Enter an amount for ID #%d: $", (id + 1));
scanf("%d", &amount[day][id]);
}
}
int id;
for(id = 0; id < TOTAL_BRANDS; id++)
{
int sum = 0;
int day;
for(day = 0; day < WEEK_DAYS; day++)
sum += amount[day][id];
finalAmount[id] = sum;
}
printf(" Final inventry: ");
printf("%-6s%9s%9s%9s%9s ", "Day", "ID1", "ID2", "ID3", "ID4");
for(day = 0; day < WEEK_DAYS; day++)
{
printf("%-6d", (day + 1));
int id;
for(id = 0; id < TOTAL_BRANDS; id++)
printf("%9d", amount[day][id]);
printf(" ");
}
printf("%6s", "Total:");
for(id = 0; id < TOTAL_BRANDS; id++)
printf("%9d", finalAmount[id]);
printf(" ");
return 0;
}
Explanation / Answer
beerinput.txt
20
12
3
2
33
55
6
3
11
5
44
3
66
22
3
5
4
5
6
33
you have to create a file name beerinput and give the path in the below program.
Program:
#include <stdio.h>
int
main (void)
{
int value;
float x[100];
const int WEEK_DAYS = 5;
const int TOTAL_BRANDS = 4;
int amount[WEEK_DAYS][TOTAL_BRANDS];
int finalAmount[TOTAL_BRANDS];
printf("Beer Brand IDs ");
printf("1. Piels ");
printf("2. Coors ");
printf("3. Bud ");
printf("4. Iron City ");
FILE *fin = fopen("C:\Users\Mittu\Desktop\cheggdes\05-12-16\beerinput.txt", "r");
FILE *fout = fopen("C:\Users\Mittu\Desktop\cheggdes\05-12-16\beeroutput.txt", "w");
printf(" Weekly Amount (+ for purchased or - for sold) ");
int day=0;
while(!feof(fin) && day < WEEK_DAYS) {
//fscanf(fin, "%f", &value)
//x[day] = value;
printf(" Day #%d: ", (day + 1));
int id;
for(id = 0; id < TOTAL_BRANDS; id++)
{
printf("Enter an amount for ID #%d: $", (id + 1));
fscanf(fin,"%d", &amount[day][id]);
printf("%d ",amount[day][id]);
}
day++;
}
//for(day = 0; day < WEEK_DAYS; day++)
//{
// printf(" Day #%d: ", (day + 1));
// int id;
// for(id = 0; id < TOTAL_BRANDS; id++)
// {
// printf("Enter an amount for ID #%d: $", (id + 1));
// scanf("%d", &amount[day][id]);
// }
//}
int id;
for(id = 0; id < TOTAL_BRANDS; id++)
{
int sum = 0;
int day;
for(day = 0; day < WEEK_DAYS; day++)
sum += amount[day][id];
finalAmount[id] = sum;
}
printf(" Final inventry: ");
fprintf(fout," Final inventry: ");
printf("%-6s%9s%9s%9s%9s ", "Day", "ID1", "ID2", "ID3", "ID4");
fprintf(fout,"%-6s%9s%9s%9s%9s ", "Day", "ID1", "ID2", "ID3", "ID4");
for(day = 0; day < WEEK_DAYS; day++)
{
fprintf(fout,"%-6d", (day + 1));
printf("%-6d", (day + 1));
int id;
for(id = 0; id < TOTAL_BRANDS; id++)
{
printf("%9d", amount[day][id]);
fprintf(fout, "%9d", amount[day][id]);
}
printf(" ");
fprintf(fout," ");
}
printf("%6s", "Total:");
fprintf(fout,"%6s", "Total:");
for(id = 0; id < TOTAL_BRANDS; id++){
printf("%9d", finalAmount[id]);
fprintf(fout,"%9d", finalAmount[id]);
}
printf(" ");
fprintf(fout, " ");
fclose(fin);
fclose(fout);
return 0;
}