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
|
/*This program asks for input from the user to display the sales for 4 divisions and 4 quarters. This program will
calculate the total for each quarter, and the total for each division displayed as a table. It will then give the total
sales for all quarters and division as well as provide the division with the highest quarter of sales and the division
with the lowest quarter of sales*/
#include<iostream>
#include<iomanip>
#include<string>
using namespace std;
const int COLS = 4;
const int ROWS = 4;
const string DIVISIONS[5] = { "NORTH", "SOUTH", "EAST", "WEST", "QUARTER TOTAL" };
//Prototypes
double getTotal(double[][COLS]);
double getAverage(double[][COLS]);
double getRowTotal(double[][COLS], int);
void showtable(double[][COLS], int);
//double getColumnTotal(double[][COLS], int);
//double getHighest(double[][COLS], int&, int&);
//double getLowest(double[][COLS], int&, int&);
int main()
{
double sales[ROWS][COLS];
double totalSales,
average;
int div, qtr;
int n = 5;
// User input to populate table.
for (div = 0; div < 4; div++)
{
for (qtr = 0; qtr < 4; qtr++)
{
cout << DIVISIONS[div]<< endl;
cout << "Quarter " << qtr + 1 << endl;
cin >> sales[div][qtr];
}
}
// Adds up all sales for quarters and divisions.
totalSales = getTotal(sales);
cout << totalSales << endl;
// Gets average for company
average = getAverage(sales);
cout << average << endl;
// Returns row total for each quarter.
getRowTotal(sales,ROWS);
showtable(sales, ROWS);
return 0;
}
// Returns total of table.
double getTotal(double sales[ROWS][COLS])
{
double totalSales = 0;
for (int row = 0; row < 4; row++)
{
for (int col = 0; col < 4; col++)
{
totalSales += sales[row][col];
}
}
return totalSales;
}
// Returns the average.
double getAverage(double sales[ROWS][COLS])
{
double totalSales = 0;
double average;
for (int row = 0; row < 4; row++)
{
for (int col = 0; col < 4; col++)
{
totalSales += sales[row][col];
}
}
average = totalSales / (ROWS * COLS);
return average;
}
//Adds up the rows.
double getRowTotal(double sales[ROWS][COLS], int n)
{
double total = 0;
for (int row = 0; row < ROWS; row++)
{
total = 0;
for (int col = 0; col < COLS; col++)
{
total += sales[n][col];
}
}
return total;
}
// Displays table.
void showtable(double sales[][COLS], int rows)
{
for (int x = 0; x < rows; x++)
{
for (int y = 0; y < COLS; y++)
{
cout << setw(4) << sales[x][y] << " ";
}
cout << endl;
}
}
|