To be completeled in C++: Quarterly Sales Statistics Write a program that lets t
ID: 3684610 • Letter: T
Question
To be completeled in C++:
Quarterly Sales Statistics
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 calculate the statistics above. Input Validation: Do not accept negative numbers for sales figures.
Explanation / Answer
/** C++ code Quarterly Sales Statistics **/
#include <iostream>
#include <iomanip>
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[] );
void printPercentIncrease( double sales[MAX_PERIODS][ MAX_DIVISIONS],
double periodTotal[] );
void printTopDivision( double sales[MAX_PERIODS][ MAX_DIVISIONS]);
int main()
{
double sales[MAX_PERIODS][ MAX_DIVISIONS];
double periodTotal[MAX_PERIODS];
getInput( sales );
printSalesTable( sales, periodTotal );
printPercentIncrease( sales, periodTotal );
printTopDivision( sales );
return 0;
}
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
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[] )
{
// 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"
cout << setw(50) << "Sales for each division by reporting period "
<< setw(15) << left << "Division" << right;
for ( int period = 0; period < MAX_PERIODS; period++ )
{
cout << setw(16) << periodName[period];
}
cout << 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++ )
{
cout << setw(15) << left << divisionName[division] << right
<< fixed << setprecision(2);
double total = 0;
for ( int period = 0; period < MAX_PERIODS; period++ )
{
cout << sales[period][division];
total += sales[period][division];
periodTotal[period] += sales[period][division];
}
cout << setw(16) << total << endl;
grandTotal += total;
}
// Print totals and averages for all divisions
cout << " " << setw(15) << left << "Period Totals" << right;
for ( int period = 0; period < MAX_PERIODS; period++ )
{
cout <<setw(16) << periodTotal[period];
}
cout << setw(16) << grandTotal;
cout << " " << setw(15) << left << "Period Averages" << right;
for ( int period = 0; period < MAX_PERIODS; period++ )
{
cout <<setw(16) << periodTotal[period] / MAX_DIVISIONS;
}
cout << setw(16) << grandTotal / MAX_DIVISIONS;
}
void printPercentIncrease( double sales[MAX_PERIODS][ MAX_DIVISIONS],
double periodTotal[] )
{
// 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"
cout << 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++ )
{
cout << 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++ )
{
cout << setw(15) << left << divisionName[division] << right
<< fixed << setprecision(2);
for ( int period = 1; period < MAX_PERIODS; period++ )
{
if ( sales[period-1][division] == 0 )
cout << setw(16) << "Not applicable";
else
cout << setw(15) << (sales[period][division] / sales[period-1][division]-1)*100 << "%";
}
}
// Print percent increase for all divisions
cout << " " << setw(15) << left << "Period Totals" << right;
for ( int period = 1; period < MAX_PERIODS; period++ )
{
if ( periodTotal[period] == 0 )
cout << setw(16) << "Not applicable";
else
cout << setw(15) << (periodTotal[period]/ periodTotal[period-1]-1)*100 << "%";
}
}
void printTopDivision( double sales[MAX_PERIODS][ MAX_DIVISIONS])
{
cout << " " << setw(50)<< "Top divisions by quarter ";
for ( int period = 0; period < MAX_PERIODS; period++ )
{
int maxIndex = 0;
for( int division = 1; division < MAX_DIVISIONS; division++ )
{
if ( sales[period][division] > sales[period][maxIndex] )
maxIndex = division;
}
cout << 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.