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 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247
|
/*
Write a program that calculates information about sales during a year.This program
will have an array that will hold the sales totals for 4 divisions(North, South,
East and West, stored in an array as strings for the output) and for 4 quarters.
This array will also hold the total for each division and for each quarter.These
totals will be calculated by the program once the values have been inputted by the
user.So this array will be a 2 - dimensional array with the quarters as the columns
and the divisions as the rows.The last column will hold the totals for the
rows(division totals) and the last row will hold the totals for the
columns(quarter totals).
The program must use / create the following functions :
- int getTotal(int[][COLS]) // gets the total of the complete array except for the
totals in the last column and last row.
- int getAverage(int[][COLS]) // return the average of all the values in the array
except for the column and row with the totals in them.
- int getRowTotal(int[][COLS], int) // returns the total for row that is specified
by the second parameter to the function.
- int getColumnTotal(int[][COLS], int) // returns the total for a column that is
specified by the second parameter to the function.
- int getHighest(int[][COLS], int&, int&) //returns the highest value in the array.
Last two parameters are the index values of where the highest value is located in the
2D array, these can then be used to display which division and quarter.
- int getLowest(int[][COLS], int&, int&) //returns the lowest value in the array.
Last two parameters are the index values of where the highest value is located in
the 2D array; these can then be used to display which division and quarter.
The program should output a table in the same format as below which will then be
followed by the division and quarter with the highest value and the division and
quarter with the lowest value.
Quarter 1 Quarter 2 Quarter 3 Quarter 4 Division Total
North
South
East
West
Quarter Total
*/
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
const int COLS = 5,
ROWS = 5;
int getTotal(int[][COLS]);
int getAverage(int[][COLS]);
int getRowTotal(int[][COLS], int);
int getColumnTotal(int[][COLS], int);
int getHighest(int[][COLS], int&, int&);
int getLowest(int[][COLS], int&, int&);
int Total = 0,
Average,
Highest,
Lowest;
int sales[COLS][ROWS];
int main()
{
const int div_number = 5,
qtr_number = 5;
int RowTotal,
ColumnTotal;
string div[div_number] = { "North", "South", "East", "West", "Quarter Total" };
string qtr[qtr_number] = { "Quarter 1", "Quarter 2", "Quarter 3", "Quarter 4", "Division Total" };
cout << " Please enter the sales information: " << endl;
for (int strDivi = 0; strDivi < COLS; strDivi++)
{
if (strDivi == (COLS - 1))
{
for (int i = 0; i < COLS; i++)
{
RowTotal = getRowTotal(sales, i);
sales[i][strDivi] = RowTotal;
}
break;
}
for (int strQtri = 0; strQtri < ROWS; strQtri++)
{
if (strQtri == (ROWS - 1))
{
for (int i = 0; i < ROWS; i++)
{
ColumnTotal = getColumnTotal(sales, i);
sales[i][strQtri] = ColumnTotal;
}
break;
}
cout << div[strDivi] << " - " << qtr[strQtri] << ": ";
cin >> sales[strDivi][strQtri];
cout << endl;
}
cout << setprecision(2) << fixed << showpoint;
// display table
cout << "\n ";
for (int i = 0; i < div_number; i++) {
if (i < (div_number - 1)) {
cout << setw(12) << qtr[i];
}
else {
cout << setw(15) << qtr[i];
}
}
cout << endl;
for (int row = 0; row < ROWS; row++) {
cout << div[row];
if (row < (ROWS - 1)) {
cout << "\n ";
}
else {
cout << "\n ";
}
for (int col = 0; col < COLS; col++) {
if (col < (COLS - 1)) {
cout << setw(12) << sales[row][col];
}
else {
cout << setw(15) << sales[row][col];
}
}
cout << endl;
}
// call functions and display total, average, lowest, and highest
Total = getTotal(sales);
cout << endl << "Company total sales: " << Total << endl;
Average = getAverage(sales);
cout << "Company average sales: " << Average << endl;
int row, col;
Highest = getHighest(sales, row, col);
cout << div[row] << " Division has the highest sales at " << Highest <<
" in " << qtr[col] << endl;
Lowest = getLowest(sales, row, col);
cout << div[row] << " Division has the lowest sales at " << Lowest <<
" in " << qtr[col] << endl << endl;
// end of program
system("pause");
return 0;
}
}
// functions
int getTotal(int numbers[][COLS])
{
for (int x = 0; x < (ROWS - 1); x++)
{
for (int y = 0; y < (COLS - 1); y++)
{
Total += numbers[x][y];
}
}
return Total;
}
int getAverage(int numbers[][COLS])
{
int index = 0;
double avg_total = 0;
for (int x = 0; x < (ROWS - 1); x++)
{
for (int y = 0; y < (COLS - 1); y++)
{
avg_total += numbers[x][y];
index++;
}
}
Average = avg_total / index;
return Average;
}
int getRowTotal(int numbers[][COLS], int row)
{
int row_total = 0;
for (int col = 0; col < (COLS - 1); col++)
{
row_total += numbers[row][col];
}
return row_total;
}
int getColumnTotal(int numbers[][COLS], int col)
{
int column_total = 0;
for (int row = 0; row < (ROWS - 1); row++)
{
column_total += numbers[row][col];
}
return column_total;
}
int getHighest(int numbers[][COLS], int &row, int &col)
{
int high = numbers[0][0];
for (int r = 0; r < (ROWS - 1); r++) {
for (int c = 0; c < (COLS - 1); c++)
{
if ((numbers[c][r]) > high) {
row = r;
col = c;
high = numbers[row][col];
}
}
}
return high;
}
int getLowest(int numbers[][COLS], int &row, int &col)
{
int low = numbers[0][0];
for (int r = 0; r < (ROWS - 1); r++)
{
for (int c = 0; c < (COLS - 1); c++)
{
if ((numbers[r][c]) <= low)
{
row = r;
col = c;
low = numbers[row][col];
}
}
}
return low;
}
|