Code:
#include <stdio.h>
#define INPUT_FILE_GET_NAMES "names.txt"
#define INPUT_FILE_GET_SALES "sales.txt"
#define MAX_ROWS_NAMES 25
#define MAX_COLS_NAMES 20
#define MAX_ROWS_SALES 25
#define MAX_COLS_SALES 6
#define PRICE_OF_PRODUCT 1985.95
//Function Declarations
int getNames(char namesArray[][MAX_COLS_NAMES]);
int getSales(int salesArray [][MAX_COLS_SALES], int a);
void calcSales(int size, int numWeeks, int salesArrays[][MAX_COLS_SALES], double total[][MAX_COLS_SALES], double sum[MAX_ROWS_SALES]);
double calcHiLowTotal(int size, int numWeeks, double total[][MAX_COLS_SALES],
double total_sales[MAX_ROWS_SALES], double low[MAX_ROWS_SALES],
double high[MAX_ROWS_SALES]);
void outputSalesDataToScreen(char namesArray[][MAX_COLS_NAMES], int numNames, int numWeeks, int salesArray[][MAX_COLS_SALES]);
void outputSalesDataToFile(char namesArray[][MAX_COLS_NAMES], int salesArray[][MAX_COLS_SALES], double total[][MAX_COLS_SALES], double total_sales[],
double low[],double high[], double sum[], int numNames, int numWeeks);
int main()
{
//Local Declarations
char namesArray[MAX_ROWS_NAMES][MAX_COLS_NAMES] = {0};
int salesArray[MAX_ROWS_SALES][MAX_COLS_SALES] = {0};
double total[MAX_ROWS_SALES][MAX_COLS_SALES] = {0};
double sum[MAX_ROWS_SALES] = {0};
double total_sales[MAX_ROWS_SALES] = {0};
double low[MAX_ROWS_SALES] = {0};
double high[MAX_ROWS_SALES] = {0};
int i = 0;
int j = 0;
int row;
int col;
int numNames=0;
int numWeeks=0;
//Statements
// printf("*** Salespersons *** \n");
numNames=getNames (namesArray);
numWeeks=getSales (salesArray, numNames);
calcSales(numNames, numWeeks, salesArray, total, sum);
// printf("\n\n***Person total*** \n");
calcHiLowTotal (numNames, numWeeks, total, total_sales, low, high);
outputSalesDataToScreen (namesArray, numNames, numWeeks, salesArray);
outputSalesDataToFile(namesArray, salesArray, total, total_sales, low, high, sum, numNames, numWeeks);
}
int getNames(char namesArray[][MAX_COLS_NAMES])
{
//Statements
FILE *fp;
int i, j, c;
int a;
//open the sequential access file
fp = fopen(INPUT_FILE_GET_NAMES,"r");
if(fp == NULL)
{
printf("Error, can't open file!!!\n");
exit(101);
}
fscanf(fp, "%d", &a);
if (a <= MAX_ROWS_NAMES)
{
for(i=0;i<a;i++)
{
for(j = 0; j <= MAX_COLS_NAMES; j++)
{
fscanf(fp, "%c", &namesArray[i][j]);
// printf("%c", namesArray[i][j]);
}
// printf("\n");
}
}
else
printf(" ***Warning: data file is too large *** \n");
fclose(fp);
return a;
}
int getSales(int salesArray[][MAX_COLS_SALES], int a)
{
//Statements
FILE *fl;
int i=0, j=0, c, b;
//open the sequential access file
fl = fopen(INPUT_FILE_GET_SALES,"r");
if(fl == NULL)
{
printf("Error, can't open file!!!\n");
exit(101);
}
fscanf(fl, "%d", &b);
if (b < MAX_COLS_SALES)
{
for(i=0;i < a ; i++)
{
for(j = 0; j < b; j++)
{
fscanf(fl, "%d", &salesArray[i][j]);
// printf("%d ",salesArray[i][j]);
}
// printf("\n\n");
}
}
else
printf(" ***Warning: data file is too large *** \n");
fclose(fl);
return b;
}
void calcSales(int size, int numWeeks, int salesArray[][MAX_COLS_SALES], double total[][MAX_COLS_SALES], double sum[MAX_ROWS_SALES])
{
int row=0;
int col=0;
for (row=0;row <size; row++)
{
for (col=0; col<numWeeks; col++)
{
total[row][col] = salesArray[row][col] * PRICE_OF_PRODUCT;
sum[row] +=total[row][col];
}
// printf("\n");
}
// printf("\n\n");
for (row=0;row <size; row++)
{
for (col=0; col<numWeeks; col++)
{
// printf("%8.2lf ", total[row][col]);
}
// printf("*----TOTAL SALES---*");
// printf("%f ", sum[row]);
// printf("\n\n");
}
return;
}
double calcHiLowTotal(int size, int numWeeks, double total[][MAX_COLS_SALES], double total_sales[], double low[], double high[])
{
int row=0;
int col=0;
double comp;
//prints the total for each week's sales
for (col = 0; col < numWeeks; col++)
{
for (row = 0; row < size; row++)
{
total_sales[col] += total[row][col];
}
// printf("%f ", total_sales[col]);
}
// printf("\n\n\n ");
//printf("****Lowest sale for each week ****\n");
//prints the lowest for each week's sales
for (col = 0; col <numWeeks; col++)
{
low[col] = total[0][col];
for (row = 0; row < size; row++)
{
if(low[col] > total[row][col])
low[col] = total[row][col];
}
//printf("%f ", low[col]);
}
//printf(" \n\n ");
//printf("****Highest sale for each week**** \n");
//prints the highest for each week's sales
for (col = 0; col < numWeeks; col++)
{
high[col] = total[0][col];
for (row = 0; row < size; row++)
{
if(high[col] < total[row][col])
high[col] = total[row][col];
}
//printf("%f ", high[col]);
}
//prints company total
for(col = 0; col < numWeeks; col++)
{
comp += total_sales[col];
}
// printf("\n");
// printf("%d", comp);
return comp;
}
void outputSalesDataToScreen(char namesArray[][MAX_COLS_NAMES], int numNames, int numWeeks, int salesArray[][MAX_COLS_SALES])
{
//Local Declarations
int i, j, k;
//Statements
printf(" \n Homework 2: Two Dimensional Arrays\n\n");
printf( " *** Sales Table *** \n\n\n");
printf("================== ======== ======== ======== ========\n");
printf(" Sales Person Week 1 Week 2 Week 3 Week 4\n");
printf("================== ======== ======== ======== ========\n");
for (i=0; i<numNames; i++)
{
for (j=0; j<MAX_COLS_NAMES; j++)
{
printf("%c", namesArray[i][j]);
}
printf("\n ");
for (k=0; k<numWeeks; k++)
{
printf(" %2d", salesArray[i][k]);
}
}
printf(" \n\n\n *** The output is in SALESRES.TXT *** \n\n");
return;
}
void outputSalesDataToFile(char namesArray[][MAX_COLS_NAMES], int salesArray[][MAX_COLS_SALES], double total[][MAX_COLS_SALES], double total_sales[],
double low[],double high[], double sum[], int numNames, int numWeeks)
{
//Local Declarations
FILE* salesOutput;
int i, j, k, l;
double comp;
//Statements
if(!(salesOutput = fopen("SALESRES.TXT", "w")))
{
printf("can not open the 'SALESERS.TXT' file.\n");
exit(101);
}
fprintf(salesOutput, "===========================\n");
fprintf(salesOutput, " ABC Manufacturing Company\n");
fprintf(salesOutput, "===========================\n\n\n");
fprintf(salesOutput, "*** Dollar Values Table *** \n\n");
fprintf(salesOutput, "==================== ========== ========== ========== ========== =============\n");
fprintf(salesOutput, " Sales Person Week 1 Week 2 Week 3 Week 4 Total/Person\n");
fprintf(salesOutput, "==================== ========== ========== ========== ========== =============\n");
for(i = 0; i < numNames; i++)
{
for(j =0; j < MAX_COLS_NAMES; j++)
{
fprintf(salesOutput, "%c", namesArray[i][j]);
}
for(k =0; k < numWeeks; k++)
{
fprintf(salesOutput, " %8.2lf", salesArray[i][k] * PRICE_OF_PRODUCT);
}
fprintf(salesOutput, " %8.2lf", sum[i]);
}
fprintf(salesOutput, "\n==================== ========== ========== ========== ==========");
fprintf(salesOutput, "\n Total / Week: ");
for (i = 0; i < numWeeks; i++)
{
comp += total_sales[i];
for (j = 0; j < numNames; j++)
{
total_sales[i] += total[i][j];
}
printf("%f ", total_sales[i]);
}
fprintf(salesOutput, " %9.2lf", total_sales[i]);
fprintf(salesOutput, "\n LOW : ");
for(l =0; l < numWeeks; l++)
fprintf(salesOutput, " %9.2lf", low[l]);
fprintf(salesOutput, "\n HIGH: ");
for(l =0; l < numWeeks; l++)
fprintf(salesOutput, " %9.2lf", high[l]);
fprintf(salesOutput, "\n\n *** Total Sales for company: ");
fprintf(salesOutput, "%10.2lf ***\n\n", comp);
fprintf(salesOutput, "=============\n");
fprintf(salesOutput, "End of Report\n");
fprintf(salesOutput, "=============\n");
fclose(salesOutput);
return;
}
the output i get