In: Computer Science
The U-Mart Supermarket is a convenience store company that has five store located in different location at UTM Skudai. At the end of each year, the management of the UTM wants to know the performance of the company. They have decided to use a computer program to help them in analyzing the company performance based on the total sales of each quarter in all stores. Develop a C program to perform this task using array and function based one following requirements.
Input:
The program should read in sales data from a text file.
The format of the input file for each line/record is as
follows:
first number is for the store branch code,
second number is the sales of the first quarter ,
third number is the sales of the second quarter,
fourth number is the sales of the o third quarter,
fifth number is the sales of the fourth quarter.
The following is an example of input file named “sales2015.txt”
containing sales data for the year 2019 for five stores.
11 940.80 490.10 960.60 670.50
12 710.90 570.30 170.60 310.60
13 360.70 160.30 300.60 870.60
14 480.50 490.40 910.70 720.55
15 340.30 320.30 140.60 570.70
Arrays:
The program should use array(s) to store the sales
data
Functions:
The program should have the following functions:
float highest_Sale(). This function should accept arrays
(representing sales) as one of its parameters. It should determine
the indices of row and column of a cell in the array
which has the highest sales.
void grandTotal_each_store(). This function should return the grand
total of sales for each stores throughout the year. It should
accept array (representing sales) as its parameters.
void Total_each_quarter(). This function should accept arrays and
the index of a column in the array (representing a quarter) as its
parameters. The function should return the total sales for the each
quarter.
Output:
The program should print out a report into a text file named
“report.txt”.
The report should include:
- The grand total of sales, over all stores sales, throughout the
year.
- The highest sales of all quarter. Print the store code and the highest sales.
- The total sales for each quarter of all store.
The following diagram is example of output file based on the
input given.
EXPECTED OUTPUT
Grand total of sales over all stores: RM xxxxx.xx
The highest quarter sales:
Store Code: 11
Quarter : 3
Sales: RM 960.60
Total sales by quarter:
Quarter Sales
----------- --------------
Quarter 1 RM 4474.10
Quarter 2 RM xxxx.xx
Quarter 3 RM xxxx.xx
Quarter 4 RM xxxx.xx
#include<stdio.h>
#define MAX 100
int read_detail (float details[][5])
{
FILE *f1;
f1= fopen("sales2015.txt","r");
if(f1==NULL)
{
printf("Error!");
}
int i=0;
while(!feof(f1))
{
fscanf(f1,"%f",&details[i][0]);
fscanf(f1,"%f",&details[i][1]);
fscanf(f1,"%f",&details[i][2]);
fscanf(f1,"%f",&details[i][3]);
fscanf(f1,"%f",&details[i][4]);
printf("%f %f %f %f
%f\n",details[i][0],details[i][1],details[i][2],details[i][3],details[i][4]);
i++;
}
return i;
}
float highest_Sale(float details[][5],int size,int *row, int
*column)
{
float max = details[0][1];
*row = 0;
*column = 1;
int i;
for(i=0;i<size;i++)
{
int j;
for(j=1;j<5;j++)
{
if(details[i][j]>max)
{
max =details[i][j];
*row = i;
*column = j;
}
}
}
return max;
}
void Total_each_quarter(float details[][5],float tot[],int
size,int column)
{
int i;
tot[column]=0;
for(i=0;i<size;i++)
{
tot[column]+=details[i][column+1];
}
}
float tot_sale(float tot[],int size)
{
float total = 0;
int i;
for(i=0;i<size;i++)
{
total+= tot[i];
}
return total;
}
void Output(float details[][5],float tot[],int size,int row,int
col)
{
FILE *f1;
f1= fopen("report.txt","w");
fprintf(f1,"Grand total of sales over all stores: RM
%.2f\n", tot_sale(tot,size));
fprintf(f1,"The highest quarter sales:\n");
fprintf(f1,"Store Code: %.0f\n",details[0][row]
);
fprintf(f1,"Quarter : %d\n",col);
fprintf(f1,"Sales: RM %.2f\n",details[row][col]);
fprintf(f1,"Total sales by quarter:\n");
fprintf(f1,"Quarter Sales\n");
fprintf(f1,"----------- --------------\n");
int i;
for(i=0;i<4;i++)
{
fprintf(f1,"Quarter %d RM
%.2f\n",i+1,tot[i]);
}
fclose(f1);
}
int main()
{
float details[MAX][5],tot[MAX];
int size,row,column;
size = read_detail(details);
highest_Sale(details,size,&row,&column);
int i;
for(i=0;i<4;i++)
{
Total_each_quarter(details,tot,size,i);
}
Output(details,tot,size,row,column);
}
//Output