fork download
  1. // Timothy Price CS1A Ch 7 challenge 7
  2. /*******************************************************************************
  3.  * Calculate quarterly sales
  4.  *
  5.  * This program receives a number of sets of values and calculates displayable
  6.  * sales statistics.
  7.  *
  8.  * Inputs:
  9.  * Sales per division per quarter
  10.  *
  11.  * Outputs:
  12.  * List of sales per division
  13.  * Quarterly division comparrison
  14.  * Total quarterly sales
  15.  * Quarterly company comparrison
  16.  * Average company sales per quarter
  17.  * Division with highest sales per quarter
  18. *******************************************************************************/
  19.  
  20. #include <iostream>
  21. #include <iomanip>
  22. #include <cmath>
  23.  
  24. using namespace std;
  25.  
  26. // Constants for number of divisions and quarters
  27. const int DIVISIONS = 6;
  28. const int QUARTERS = 4;
  29.  
  30. // Function Prototypes
  31. void getSales(double s[DIVISIONS][QUARTERS]);
  32. double qTotal(double s[DIVISIONS][QUARTERS], int q);
  33. double qAvg(double s[DIVISIONS][QUARTERS], int q);
  34. int bestDiv(double s[DIVISIONS][QUARTERS], int q);
  35. void showReport(double s[DIVISIONS][QUARTERS]);
  36.  
  37. int main() {
  38. double sales[DIVISIONS][QUARTERS];
  39.  
  40. cout << "===== Quarterly Sales Tracker =====" << endl << endl;
  41.  
  42. // Get and validate sales input
  43. getSales(sales);
  44.  
  45. // Display the structured report
  46. showReport(sales);
  47.  
  48. return 0;
  49. }
  50.  
  51. // Function to get sales figures with input validation
  52. void getSales(double s[DIVISIONS][QUARTERS]) {
  53. for (int i = 0; i < DIVISIONS; ++i) {
  54. cout << "Enter sales for Division " << (i + 1) << endl;
  55. for (int j = 0; j < QUARTERS; ++j) {
  56. do {
  57. cout << " Quarter " << (j + 1) << ": $";
  58. cin >> s[i][j];
  59. cout << s[i][j] << endl;
  60.  
  61. if (s[i][j] < 0) {
  62. cout << "Sales cannot be negative." << endl;
  63. }
  64. } while (s[i][j] < 0);
  65. }
  66. cout << endl;
  67. }
  68. }
  69.  
  70. // Function to calculate total sales for a quarter
  71. double qTotal(double s[DIVISIONS][QUARTERS], int q) {
  72. double total = 0.0;
  73. for (int i = 0; i < DIVISIONS; ++i) {
  74. total += s[i][q];
  75. }
  76. return total;
  77. }
  78.  
  79. // Function to calculate average sales for a quarter
  80. double qAvg(double s[DIVISIONS][QUARTERS], int q) {
  81. return qTotal(s, q) / DIVISIONS;
  82. }
  83.  
  84. // Function to find the division with the highest sales for a quarter
  85. int bestDiv(double s[DIVISIONS][QUARTERS], int q) {
  86. int top = 0;
  87. for (int i = 1; i < DIVISIONS; ++i) {
  88. if (s[i][q] > s[top][q]) {
  89. top = i;
  90. }
  91. }
  92. return top;
  93. }
  94.  
  95. // Function to display the complete report
  96. void showReport(double s[DIVISIONS][QUARTERS]) {
  97. cout << fixed << setprecision(2);
  98.  
  99. cout << endl << "-----------------------------" << endl;
  100. cout << " SALES REPORT " << endl;
  101. cout << "-----------------------------" << endl;
  102.  
  103. double prevTotal = 0.0;
  104.  
  105. for (int q = 0; q < QUARTERS; ++q) {
  106. cout << endl << "------------------------------" << endl;
  107. cout << "QUARTER " << (q + 1) << endl;
  108. cout << "------------------------------" << endl;
  109.  
  110. // Division list and changes
  111. cout << "Division Sales: " << endl;
  112. for (int i = 0; i < DIVISIONS; ++i) {
  113. cout << " Div " << (i + 1) << ": $" << s[i][q];
  114.  
  115. if (q > 0) {
  116. double diff = s[i][q] - s[i][q - 1];
  117. cout << " (";
  118. if (diff >= 0) {
  119. cout << "+";
  120. }
  121. cout << "$" << diff << ")";
  122. }
  123. cout << endl;
  124. }
  125.  
  126. // Quarter total and company change
  127. double curTotal = qTotal(s, q);
  128. cout << endl << "Total Sales: $" << curTotal << endl;
  129.  
  130. if (q > 0) {
  131. double compDiff = curTotal - prevTotal;
  132. cout << "Company Change: ";
  133. if (compDiff >= 0) {
  134. cout << "+";
  135. }
  136. cout << "$" << compDiff << endl;
  137. }
  138.  
  139. // Quarter average and top division
  140. cout << "Average Sales: $" << qAvg(s, q) << endl;
  141.  
  142. int top = bestDiv(s, q);
  143. cout << "Top Division: Div " << (top + 1) << " ($" << s[top][q] << ") ";
  144. cout << endl;
  145.  
  146. prevTotal = curTotal;
  147. }
  148. }
Success #stdin #stdout 0.01s 5320KB
stdin
1000
1200
1300
1400
1200
1200
2000
1900
1400
1550
3200
2310
9820
1940
1939
2453
1876
2364
4193
1092
10924
734
6273
12345
stdout
===== Quarterly Sales Tracker =====

Enter sales for Division 1
  Quarter 1: $1000
  Quarter 2: $1200
  Quarter 3: $1300
  Quarter 4: $1400

Enter sales for Division 2
  Quarter 1: $1200
  Quarter 2: $1200
  Quarter 3: $2000
  Quarter 4: $1900

Enter sales for Division 3
  Quarter 1: $1400
  Quarter 2: $1550
  Quarter 3: $3200
  Quarter 4: $2310

Enter sales for Division 4
  Quarter 1: $9820
  Quarter 2: $1940
  Quarter 3: $1939
  Quarter 4: $2453

Enter sales for Division 5
  Quarter 1: $1876
  Quarter 2: $2364
  Quarter 3: $4193
  Quarter 4: $1092

Enter sales for Division 6
  Quarter 1: $10924
  Quarter 2: $734
  Quarter 3: $6273
  Quarter 4: $12345


-----------------------------
          SALES REPORT           
-----------------------------

------------------------------
QUARTER 1
------------------------------
Division Sales: 
  Div 1: $1000.00
  Div 2: $1200.00
  Div 3: $1400.00
  Div 4: $9820.00
  Div 5: $1876.00
  Div 6: $10924.00

Total Sales: $26220.00
Average Sales: $4370.00
Top Division: Div 6 ($10924.00) 

------------------------------
QUARTER 2
------------------------------
Division Sales: 
  Div 1: $1200.00 (+$200.00)
  Div 2: $1200.00 (+$0.00)
  Div 3: $1550.00 (+$150.00)
  Div 4: $1940.00 ($-7880.00)
  Div 5: $2364.00 (+$488.00)
  Div 6: $734.00 ($-10190.00)

Total Sales: $8988.00
Company Change: $-17232.00
Average Sales: $1498.00
Top Division: Div 5 ($2364.00) 

------------------------------
QUARTER 3
------------------------------
Division Sales: 
  Div 1: $1300.00 (+$100.00)
  Div 2: $2000.00 (+$800.00)
  Div 3: $3200.00 (+$1650.00)
  Div 4: $1939.00 ($-1.00)
  Div 5: $4193.00 (+$1829.00)
  Div 6: $6273.00 (+$5539.00)

Total Sales: $18905.00
Company Change: +$9917.00
Average Sales: $3150.83
Top Division: Div 6 ($6273.00) 

------------------------------
QUARTER 4
------------------------------
Division Sales: 
  Div 1: $1400.00 (+$100.00)
  Div 2: $1900.00 ($-100.00)
  Div 3: $2310.00 ($-890.00)
  Div 4: $2453.00 (+$514.00)
  Div 5: $1092.00 ($-3101.00)
  Div 6: $12345.00 (+$6072.00)

Total Sales: $21500.00
Company Change: +$2595.00
Average Sales: $3583.33
Top Division: Div 6 ($12345.00)