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
|
// Monthly Sales Analysis
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;
// Function Prototypes
void inputData(double[]);
int lowMonth(double[]);
int highMonth(double[]);
double averageSales(double[]);
const char * monthArray[] = {"January", "February", "March", "April",
"May", "June", "July", "August",
"September", "October", "November", "December"};
const int NUM_MONTHS = 12;
int main()
{
double sales[NUM_MONTHS];
int low, high;
double average;
// Input the monthly sales data
inputData(sales);
// Find the month with the highest sales
high = highMonth(sales);
// Find the month with the lowest sales
low = lowMonth(sales);
// Calculate the average monthly sales
average = averageSales(sales);
// Display results
cout << "The highest sales were in " << monthArray[high] << " with $" << setprecision(2) << fixed << sales[high] << endl;
cout << "The lowest sales were in " << monthArray[low] << " with $" << setprecision(2) << fixed << sales[low] << endl;
cout << "The average monthly sales were $" << setprecision(2) << fixed << average << endl;
cin.get();
cin.get();
cin.clear();
cin.ignore(255, '\n');
cin.get();
return 0;
}
// This functions requests the monthly sales data from the user
void inputData( double sales[])
{
for (int i = 0; i < NUM_MONTHS; i++)
{
cout << "Please enter the sales in dollars for " << monthArray[i] << " ";
cin >> sales[NUM_MONTHS];
}
return;
}
// This function determines which month had the highest sales and
// returns the number for that month, 0 = January, 1 = February, etc.
int highMonth(double sales[])
{
int highest;
highest = sales[0];
for (int i = 0; i < NUM_MONTHS; i++)
{
if (sales[i] > highest)
highest = sales[i];
}
return highest;
}
// This function determines which month had the lowest sales and
// returns the number for that month, 0 = January, 1 = February, etc.
int lowMonth(double sales[])
{
int lowest;
lowest = sales[0];
for (int i = 0; i < NUM_MONTHS; i++)
{
if (sales[i] < lowest)
lowest = sales[i];
}
return lowest;
}
// This function computes the average monthly sales
double averageSales(double sales[])
{
int sum = 0;
double average = 0.0;
for (int i = 0; i < NUM_MONTHS; i++)
{
sum += sales[i];
}
return average = sum / NUM_MONTHS;
}
|