Question 2 (20 MARKS) The Nusa UniCity is a convenience college university that
ID: 3874201 • Letter: Q
Question
Question 2 (20 MARKS) The Nusa UniCity is a convenience college university that has three building of hostel (block A, B and C) located in Nusajaya. At the end of each year, the management wants to know the total bill of these hostels from January to December. They have decided to use a computer program to help them in analyzing the h been appointed to develop the program using C language. The requirements of the program are as follow ostel's bill. You, as a freelance programmer have Input . The program should read in bill data from a text file . The format of the input file is as follows: Three columns are for block A, B and C bills. While twelve rows data is for January to December bill Figure 1 below shows an example of input file named "bil12016.txt" containing bill data for the year 2016. 2840 3030 2530 2760 3220 3140 3080 3110 2960 2890 2940 2700 4500 4740 4620 4560 4720 4800 4600 4530 4420 4260 4360 4200 3760 3630 3720 3850 3940 4070 3860 3800 3770 3680 3600 3550 Figure 1: An example of input file, "bil12016.txt" Output: . The program should print out a report into a text file named "report2016.txt". . The report should include o o o The total bill for January to December The total, max and min bill for each hostel. The grand total of bill, over all hostel bills throughout the year Figure 2 shows an example report file for the sales data of the year 2015Explanation / Answer
Given below is the code for the question.
Please don't forget to rate the answer if it was helpful. Thank you
#include <stdio.h>
#include <stdlib.h>
void calculateTotal(int blockA[], int blockB[], int blockC[], int total[]);
int min(int arr[]);
int max(int arr[]);
int sum(int arr[]);
void readFile(char *fname, int blockA[], int blockB[], int blockC[]);
void printReport(char *fname, int blockA[], int blockB[], int blockC[], int total[]);
int main()
{
char inputFile[] = "bill2016.txt";
char outputFile[] = "report2016.txt";
int blockA[12], blockB[12], blockC[12], total[12];
readFile(inputFile, blockA, blockB, blockC);
calculateTotal(blockA, blockB, blockC, total);
printReport(outputFile, blockA, blockB, blockC, total);
}
void calculateTotal(int blockA[], int blockB[], int blockC[], int total[])
{
int i;
for(i = 0; i < 12; i++)
total[i] = blockA[i] + blockB[i] + blockC[i];
}
int min(int arr[])
{
int minimum = arr[0];
int i;
for(i = 1; i < 12; i++)
{
if(arr[i] < minimum)
minimum = arr[i];
}
return minimum;
}
int max(int arr[])
{
int maximum = arr[0];
int i;
for(i = 1; i < 12; i++)
{
if(arr[i] > maximum)
maximum = arr[i];
}
return maximum;
}
int sum(int arr[])
{
int i;
int total = 0;
for(i = 0; i < 12; i++)
{
total += arr[i];
}
return total;
}
void readFile(char *fname, int blockA[], int blockB[], int blockC[])
{
FILE *fp = fopen(fname, "r");
int i;
if(fp == NULL)
{
printf("Error: Could not open file %s for reading ", fname);
exit(1);
}
for(i = 0; i < 12; i++)
{
fscanf(fp, "%d %d %d", &blockA[i], &blockB[i], &blockC[i]);
}
fclose(fp);
}
void printReport(char *fname, int blockA[], int blockB[], int blockC[], int total[])
{
FILE *fp = fopen(fname, "w");
int i;
if(fp == NULL)
{
printf("Error: Could not open file %s for writing ", fname);
exit(1);
}
fprintf(fp, " TOTAL ELECTRICITY BILL YEAR 2016 ");
fprintf(fp, " HOSTEL BLOCK ");
fprintf(fp, "%-10s %-10s %-10s %-10s %-10s ", "MONTH", "A", "B", "C", "TOTAL");
fprintf(fp, "%-10s %-10s %-10s %-10s %-10s ", "-----", "-", "-", "-", "-----");
for(i = 0 ; i < 12; i++)
fprintf(fp, "%4d %10d %10d %10d %10d ", i+1, blockA[i], blockB[i], blockC[i], total[i] );
fprintf(fp, " %5s %10d %10d %10d %10d ", "TOTAL", sum(blockA), sum(blockB), sum(blockC), sum(total));
fprintf(fp, "%5s %10d %10d %10d %10d ", "MAX", max(blockA), max(blockB), max(blockC), max(total));
fprintf(fp, "%5s %10d %10d %10d %10d ", "MIN", min(blockA), min(blockB), min(blockC), min(total));
fclose(fp);
printf("Report generated in file %s ", fname);
}
input file: bill2016.txt
2840 4500 3760
3030 4740 3630
2530 4620 3720
2760 4560 3850
3220 4720 3940
3140 4800 4070
3080 4600 3860
3110 4530 3800
2960 4420 3770
2890 4260 3680
2940 4360 3600
2700 4200 3550
output file: report2016.txt
TOTAL ELECTRICITY BILL YEAR 2016
HOSTEL BLOCK
MONTH A B C TOTAL
----- - - - -----
1 2840 4500 3760 11100
2 3030 4740 3630 11400
3 2530 4620 3720 10870
4 2760 4560 3850 11170
5 3220 4720 3940 11880
6 3140 4800 4070 12010
7 3080 4600 3860 11540
8 3110 4530 3800 11440
9 2960 4420 3770 11150
10 2890 4260 3680 10830
11 2940 4360 3600 10900
12 2700 4200 3550 10450
TOTAL 35200 54310 45230 134740
MAX 3220 4800 4070 12010
MIN 2530 4200 3550 10450
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.