Write a program that lets the user enter four quarterly sales figures for six di
ID: 3620296 • Letter: W
Question
Write a program that lets the user enter four quarterly sales figures for six divisions of a company. The figures should be stored in a two-dimensional array. Once the figures are entered,the program should display the following data for each quarter:- A list of the sales figures by division
- Each division's increase or decrease from the previous quarter( This will not be displayed for the first quarter.)
- The total sales for the quarter
- The company's increase or decrease from the previous quarter (This will not be displayed for the first quarter.)
- The average sales for all divisions that quarter
- The division with the highest sales for that quarter
The program should be modular, with functions that calulate the statistics above.
Input Validation : Do not accept negative numbers for sales figures.
Explanation / Answer
The two-dimensional array will have the form sales[period][division] to represent the sales in a particular period from a particular division. We use the generic term period instead of the specific term, quarter, to allow the code to be used for other reporting periods with minimal adjustments. In the code below, we have made up names for the six divisions to show how we can use arrays to generalize the code. If we had a different set of divisions, we would only have to change the array initialization statement for the division names. Similarly changing the names of the reporting periods would require only a change to the definition of the period name array. Of course, changing the number of reporting periods or the number of divisions would require changes to the symbolic constants that specify the numbers of periods and divisions.
Notice that when we pass a whole array to a function, we can change elements of the array. We can do this because whole arrays are passed by reference.
Notice that the use of a common input file is maintained by passing the program variable for the file name to the function as a reference to the type ofstream.
#include
#include
using namespace std;
// Use global data for array sizes and array names
const int MAX_DIVISIONS = 6,
MAX_PERIODS = 4;
string divisionName[] = {“Pacific”, “Mountain”, “North Central”,
“South Central”, “Northeast”, “Southeast” };
string periodName[] = {“First Quarter”, “Second Quarter”,
“Third Quarter”, “Fourth Quarter” };
// Function prototypes shown below
void getInput( double sales[MAX_PERIODS][ MAX_DIVISIONS] );
void printSalesTable( double sales[MAX_PERIODS][ MAX_DIVISIONS],
double periodTotal[], ofstream& outFile );
void printPercentIncrease( double sales[MAX_PERIODS][ MAX_DIVISIONS],
double periodTotal[], ofstream& outFile );
void printTopDivision( double sales[MAX_PERIODS][ MAX_DIVISIONS],
ofstream& outFile );
int main()
{
double sales[MAX_PERIODS][ MAX_DIVISIONS];
double periodTotal[MAX_PERIODS];
getInput( sales );
ofstream outFile;
printSalesTable( sales, periodTotal, outFile );
printPercentIncrease( sales, periodTotal, outFile );
printTopDivision( sales, outFile );
return EXIT_SUCCESS;
}
void getInput( double sales[MAX_PERIODS][ MAX_DIVISIONS] )
{
// Get input data in this routine. Prompt user for keyboard
// input, checking for negative sales figures
cout << “Enter sales data for each division for each reporting “
<< “ period. ”
for ( int division = 0; division < MAX_DIVISIONS; division++ )
{
cout << “Enter sales data for the “ << divisionName[division]
<< “ Division ”
for ( int period = 0; period < MAX_PERIODS; period++ )
{
cout << periodName[period] << “ Sales: “;
cin >> sales[period][division];
// Ensure that sales data are not negative
do while ( sales[period][division] < 0 )
{
cout << “ ERROR – you cannot have negative sales! ”
cout << “Renter “ << periodName[period] << “ Sales: “;
cin >> sales[period][division];
}
}
}
}
void printSalesTable( double sales[MAX_PERIODS][ MAX_DIVISIONS],
double periodTotal[], ofstream& outFile )
{
// Print sales table showing the sales for each division
// on one row. That row will show the sales for each
// period and the total for all periods.
// Start by printing title then print table header with
//period names “Total”
outFile << setw(50) << “Sales for each division by reporting period ”
<< setw(15) << left << “Division” << right;
for ( int period = 0; period < MAX_PERIODS; period++ )
{
outFile << setw(16) << periodName[period];
}
outFile << setw(16) << “Total Sales ”
// Print sales data for each division here. Compute
// total sales per division in variable total; grandTotal
// is variable to hold sales for all divisions. The
// array periodTotal[period] computes the total for all
// divisions in a given period.
double grandTotal = 0;
for ( int division = 0; division < MAX_DIVISIONS; division++ )
{
outFile << setw(15) << left << divisionName[division] << right
<< fixed << setprecision(2);
double total = 0;
for ( int period = 0; period < MAX_PERIODS; period++ )
{
outFile << sales[period][division];
total += sales[period][division];
periodTotal[period] += sales[period][division];
}
outFile << setw(16) << total << endl;
grandTotal += total;
}
// Print totals and averages for all divisions
outFile << “ ” << setw(15) << left << “Period Totals” << right;
for ( int period = 0; period < MAX_PERIODS; period++ )
{
outFile <<< periodTotal[period];
}
outFile << setw(16) << grandTotal;
}
outFile << “ ” << setw(15) << left << “Period Averages” << right;
for ( int period = 0; period < MAX_PERIODS; period++ )
{
outFile <<< periodTotal[period] / MAX_DIVISIONS;
}
outFile << setw(16) << grandTotal / MAX_DIVISIONS;
}
void printPercentIncrease( double sales[MAX_PERIODS][ MAX_DIVISIONS],
double periodTotal[], ofstream& outFile )
{
// Print sales table showing the sales for each division
// on one row. That row will show the sales for each
// period and the total for all periods.
// Start by printing title then print table header with
//period names “Total”
outFile << setw(50) << “ Percent increase in sales for each division “
<< “by reporting period (excluding first) ”
<< setw(15) << left << “Division” << right;
for ( int period = 1; period < MAX_PERIODS; period++ )
{
outFile << setw(16) << periodName[period];
}
// Print percent increase for each division here. Compute
// total sales per division in variable total; grandTotal
// is variable to hold sales for all divisions. The
// array periodTotal[period] computes the total for all
// divisions in a given period.
for ( int division = 0; division < MAX_DIVISIONS; division++ )
{
outFile << setw(15) << left << divisionName[division] << right
<< fixed << setprecision(2);
for ( int period = 1; period < MAX_PERIODS; period++ )
{
if ( sales[period-1][division] == 0 )
outfile << setw(16) << “Not applicable”;
else
outFile << setw(15) << ( sales[period][division] /
sales[period-1][division] – 1 ) * 100 << “%”;
}
}
// Print percent increase for all divisions
outFile << “ ” << setw(15) << left << “Period Totals” << right;
for ( int period = 1; period < MAX_PERIODS; period++ )
{
if ( total[period] == 0 )
outFile << setw(16) << “Not applicable”;
else
outFile <<< ( total[period] / total[period – 1]
- 1) * 100 << “%”;
}
void printTopDivision( double sales[MAX_PERIODS][ MAX_DIVISIONS],
ofstream& outFile )
{
outFile << “ ” << setw(50)<< “Top divisions by quarter ”;
for ( int period = 0; period < MAX_PERIODS; period++ )
{
int maxIndex = 0;
for( division = 1; division < MAX_DIVISIONS; division++ )
{
if ( sales[period][division] > sales[period][maxIndex] )
maxIndex = division;
}
outFile << setw(20) << right << periodName[period] << “: “
<< divisionName[maxIndex] << endl << endl;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.