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
|
#include <iostream>
#include "9.PE4.h"
namespace SALES
{
// copies the lesser of 4 or n items from the array ar
// to the sales member of s and computes and stores the
// average, maximum, and minimum values of the entered items;
// remaining elements of sales, if any, set to 0
void setSales(Sales & s, const double ar[], int n)
{
int temp;
if (n < 4)
temp = n;
else
temp = 4;
//Fills sales array
int sales_count = 0;
int i;
for (i = 0; i < temp; i++)
{
s.sales[i] = ar[i];
sales_count++;
}
temp = 4 - sales_count;
if (temp > 0)
{
for (temp = 4 - temp; temp < 4; temp++)
s.sales[temp] = 0.0;
}
s.average = ave(s, sales_count);
s.max = max(s, sales_count);
s.min = min(s, sales_count);
}
// gathers sales for 4 quarters interactively, stores them
// in the sales member of s and computes and stores the
// average, maximum, and minimum values
void setSales(Sales & s)
{
using std::cout;
using std::cin;
using std::endl;
cout << "Winter: $";
cin >> s.sales[0];
cout << "Spring: $";
cin >> s.sales[1];
cout << "Summer: $";
cin >> s.sales[2];
cout << "Fall: $";
cin >> s.sales[3];
s.average = ave(s, QUARTERS);
s.max = max(s, QUARTERS);
s.min = min(s, QUARTERS);
}
// display all information in structure s
void showSales(const Sales & s)
{
using std::cout;
static int count = 1;
cout << std::endl;
cout << "~~~ Sales Structure # " << count << " ~~~\n";
for (int i = 0; i < QUARTERS; i++)
cout << "Quarter #" << i + 1 << ": " << s.sales[i] << std::endl;
cout << "\nAverage: " << s.average << std::endl;
cout << "Max: " << s.max << std::endl;
cout << "Min: " << s.min << std::endl;
count++;
}
//Maximum computation
double max(Sales &s, int n)
{
double maximum = s.sales[0];
for (int i = 1; i < n; i++)
{
if (s.sales[i] > maximum)
maximum = s.sales[i];
}
return maximum;
}
//Minimum computation
double min(Sales &s, int n)
{
double minimum = s.sales[0];
for (int i = 1; i < n; i++)
{
if (s.sales[i] < minimum)
minimum = s.sales[i];
}
return minimum;
}
//Average computation
double ave(Sales &s, int n)
{
double dtemp = 0;
for (int i = 0; i < n; i++)
dtemp += s.sales[i];
double average = dtemp / n;
return average;
}
}
| |