Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

QUESTION 2 120 MARKS The Maju Jaya Mart is a convenience store company that has

ID: 3874203 • Letter: Q

Question

QUESTION 2 120 MARKS The Maju Jaya Mart is a convenience store company that has five store located in Skudai At the end of each year, the management of the company wants to know the performance of their company. They have decided to use a computer program to help them in analyzing the company's sales. You, as a freelance programmer have been appointed to develop the program using C language. The requirements of the program are as follow: Input . Th e program should read in sales data from a text file The format of the input file is as follows: The first column is for the store branch code, second column is the sales of the first quarter, third column is for second quarter, and so forth. Figure 2 shows an example of input file named "sales2015.txt" containing sales data for the year 2015 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. Print the store code and the highest sales. The total sales for each quarter o o o Figure 3 shows an example report file for the sales data of the year 2015 Arrays . The program should use array(s) to store the sales data. Functions The program should have the following function: grandTotal(). This function should return the grand total of sales over all stores throughout the year. It should accept array (representing sales) as its parameters. quarterTotal). 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 specified quarter highestSale(). 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. .

Explanation / Answer

#include<stdio.h>
int grandTotal(int sales[5][5]);
void highestSale(int sales[5][5]);
int quarterTotal(int sales[5][5], int quarterIndex);
FILE *out_file;
int main(){
FILE *in_file;
in_file = fopen("sales.txt", "r");
out_file = fopen("report.txt","w");
int sales[5][5] ;
if (in_file == NULL)
{
printf("Can't open file for reading. ");
}
else
{
for(int i=0;i < 5 ; i++){
for(int j=0; j < 5 ; j++){
fscanf(in_file, "%d", &sales[i][j]);
}
}
fprintf(out_file,"Grand Total of sales over all stores: RM %d ",grandTotal(sales));
highestSale(sales);
fprintf(out_file," Total Sales by quarter : ");
fprintf(out_file, "Quarter Sales ");
fprintf(out_file, "-------- ------- ");
for(int i=1 ; i < 5 ; i++){
fprintf(out_file,"Quarter %d RM %d ",i,quarterTotal(sales,i));
}
fclose(in_file);
}
return 0;
}
int grandTotal(int sales[5][5]){
int grandTotal = 0;
for(int i=0 ; i < 5 ; i++){
for(int j=1 ; j< 5 ;j++){
grandTotal+=sales[i][j];
}
}
return grandTotal;
}
void highestSale(int sales[5][5]){
int highestSale = sales[0][1];
int storeId = sales[0][0];
int quarterId = 1;
for(int i = 0 ; i < 5 ; i++){
for(int j = 1 ; j < 5 ;j++){
if(sales[i][j] > highestSale){
highestSale = sales[i][j];
storeId = sales[i][0];
quarterId = j;
}
}
}
fprintf(out_file, " The highest quarter sales : ");
fprintf(out_file, "---------------------------- ");
fprintf(out_file,"Store Code : %d ",storeId);
fprintf(out_file,"Quarter : %d ",quarterId);
fprintf(out_file,"Sales : RM %d ",highestSale);
}
int quarterTotal(int sales[5][5], int quarterIndex){
int quarterTotal = 0;
for(int i=0 ; i < 5 ; i++){
quarterTotal += sales[i][quarterIndex];
}
return quarterTotal;
}