1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158
|
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
const int COLS = 5;
const int ROWS = 5;
void showArray(int[][COLS], string[]);
double getTotal(int[][COLS]);
double getAverage(int[][COLS]);
double getRowTotal(int[][COLS], int);
double getColumnTotal(int[][COLS], int);
double getHighest(int[][COLS], int&, int&);
double getLowest(int[][COLS], int&, int&);
int main()
{
string division[ROWS] = { "North", "South", "East", "West", "Quarter Total" };
string quarter[COLS] = { "Quarter 1", "Quarter 2", "Quarter 3", "Quarter 4", "Division Total" };
double total = 0;
double average = 0;
double colTotal = 0;
double highest = 0;
double lowest = 0;
int div, qtr;
int sales[ROWS][COLS];
for (div = 0; div < ROWS - 1; div++)
{
for (qtr = 0; qtr < COLS - 1; qtr++)
{
cout << "What is the sales amount for " << division[div] << " division, "
<< quarter[qtr] << "?";
cin >> sales[div][qtr];
}
cout << endl;
}
for (qtr = 0; qtr < COLS; qtr++)
{
cout << "\t" << setw(15) << quarter[qtr];
}
showArray(sales, division);
total = getTotal(sales);
average = getAverage(sales);
colTotal = getColumnTotal(sales, colTotal);
highest = getHighest(sales, div, qtr);
lowest = getLowest(sales, div, qtr);
cout << fixed << showpoint << setprecision(2);
cout << "The total sales for the company is $" << total << endl;
cout << "The average sales for each division's quarterly sales is $" << average << endl;
cout << "The highest sales quarter for the company is $" << highest << endl;
cout << "The lowest sales quarter for the company is $" << lowest << endl;
return 0;
}
void showArray(int sales[ROWS][COLS], string division[ROWS])
{
int div, qtr;
double rowTotal = 0;
rowTotal = getRowTotal(sales, rowTotal);
for (div = 0; div < ROWS - 1; div++)
{
cout << "\n\n";
cout << division[div] << "\t";
for (qtr = 0; qtr < COLS - 1; qtr++)
{
cout << setw(15) << sales[div][qtr] << " ";
}
cout << setw(15) << rowTotal;
}
cout << "\n\n";
cout << division[4];
}
double getTotal(int sales[ROWS][COLS])
{
double total = 0;
int div, qtr;
for (div = 0; div < ROWS - 1; div++)
{
for (qtr = 0; qtr < COLS - 1; qtr++)
{
total += sales[div][qtr];
}
}
return total;
}
double getAverage(int sales[ROWS][COLS])
{
double total = 0;
double average = 0;
int div, qtr;
for (div = 0; div < ROWS - 1; div++)
{
for (qtr = 0; qtr < COLS - 1; qtr++)
{
total += sales[div][qtr];
average = total / ((div +1) * (qtr +1));
}
}
return average;
}
double getRowTotal(int sales[ROWS][COLS], int rowTotal)
{
for (int div = 0; div < ROWS - 1; div++)
{
rowTotal = 0;
for (int qtr = 0; qtr < COLS - 1; qtr++)
rowTotal += sales[div][qtr];
}
return rowTotal;
}
double getColumnTotal(int sales[ROWS][COLS], int colTotal)
{
for (int qtr = 0; qtr < COLS - 1; qtr++)
{
colTotal = 0;
for (int div = 0; div < ROWS - 1; div++)
colTotal += sales[div][qtr];
cout << "\t" << setw(7) << colTotal << setw(6);
}
cout << endl;
return 0;
}
double getHighest(int sales[ROWS][COLS], int &div, int &qtr)
{
int highest = sales[0][0];
for (int div = 0; div < ROWS - 1; div++)
{
for (int qtr = 0; qtr < COLS - 1; qtr++)
{
if (sales[div][qtr] > highest)
highest = sales[div][qtr];
}
}
return highest;
}
double getLowest(int sales[ROWS][COLS], int &div, int &qtr)
{
int lowest = sales[0][0];
for (int div = 0; div < ROWS - 1; div++)
{
for (int qtr = 0; qtr < COLS - 1; qtr++)
{
if (sales[div][qtr] < lowest)
lowest = sales[div][qtr];
}
}
return lowest;
}
|