// Timothy Price CS1A Ch 7 challenge 7
/*******************************************************************************
* Calculate quarterly sales
*
* This program receives a number of sets of values and calculates displayable
* sales statistics.
*
* Inputs:
* Sales per division per quarter
*
* Outputs:
* List of sales per division
* Quarterly division comparrison
* Total quarterly sales
* Quarterly company comparrison
* Average company sales per quarter
* Division with highest sales per quarter
*******************************************************************************/
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;
// Constants for number of divisions and quarters
const int DIVISIONS = 6;
const int QUARTERS = 4;
// Function Prototypes
void getSales(double s[DIVISIONS][QUARTERS]);
double qTotal(double s[DIVISIONS][QUARTERS], int q);
double qAvg(double s[DIVISIONS][QUARTERS], int q);
int bestDiv(double s[DIVISIONS][QUARTERS], int q);
void showReport(double s[DIVISIONS][QUARTERS]);
int main() {
double sales[DIVISIONS][QUARTERS];
cout << "===== Quarterly Sales Tracker =====" << endl << endl;
// Get and validate sales input
getSales(sales);
// Display the structured report
showReport(sales);
return 0;
}
// Function to get sales figures with input validation
void getSales(double s[DIVISIONS][QUARTERS]) {
for (int i = 0; i < DIVISIONS; ++i) {
cout << "Enter sales for Division " << (i + 1) << endl;
for (int j = 0; j < QUARTERS; ++j) {
do {
cout << " Quarter " << (j + 1) << ": $";
cin >> s[i][j];
cout << s[i][j] << endl;
if (s[i][j] < 0) {
cout << "Sales cannot be negative." << endl;
}
} while (s[i][j] < 0);
}
cout << endl;
}
}
// Function to calculate total sales for a quarter
double qTotal(double s[DIVISIONS][QUARTERS], int q) {
double total = 0.0;
for (int i = 0; i < DIVISIONS; ++i) {
total += s[i][q];
}
return total;
}
// Function to calculate average sales for a quarter
double qAvg(double s[DIVISIONS][QUARTERS], int q) {
return qTotal(s, q) / DIVISIONS;
}
// Function to find the division with the highest sales for a quarter
int bestDiv(double s[DIVISIONS][QUARTERS], int q) {
int top = 0;
for (int i = 1; i < DIVISIONS; ++i) {
if (s[i][q] > s[top][q]) {
top = i;
}
}
return top;
}
// Function to display the complete report
void showReport(double s[DIVISIONS][QUARTERS]) {
cout << fixed << setprecision(2);
cout << endl << "-----------------------------" << endl;
cout << " SALES REPORT " << endl;
cout << "-----------------------------" << endl;
double prevTotal = 0.0;
for (int q = 0; q < QUARTERS; ++q) {
cout << endl << "------------------------------" << endl;
cout << "QUARTER " << (q + 1) << endl;
cout << "------------------------------" << endl;
// Division list and changes
cout << "Division Sales: " << endl;
for (int i = 0; i < DIVISIONS; ++i) {
cout << " Div " << (i + 1) << ": $" << s[i][q];
if (q > 0) {
double diff = s[i][q] - s[i][q - 1];
cout << " (";
if (diff >= 0) {
cout << "+";
}
cout << "$" << diff << ")";
}
cout << endl;
}
// Quarter total and company change
double curTotal = qTotal(s, q);
cout << endl << "Total Sales: $" << curTotal << endl;
if (q > 0) {
double compDiff = curTotal - prevTotal;
cout << "Company Change: ";
if (compDiff >= 0) {
cout << "+";
}
cout << "$" << compDiff << endl;
}
// Quarter average and top division
cout << "Average Sales: $" << qAvg(s, q) << endl;
int top = bestDiv(s, q);
cout << "Top Division: Div " << (top + 1) << " ($" << s[top][q] << ") ";
cout << endl;
prevTotal = curTotal;
}
}