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
|
#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;
//=============================================================================
// Function prototypes
float average_Sales(float, int);
void Standard_comparison(float, float, bool, int);
int above_below(bool, int);
void print_Result(int, double, int, int);
//=============================================================================
// Main function
int main()
{
// Is this the parallel array????
int average;
float standard[12]={23.0, 33.1, 21.0, 23.5, 54.0, 34.3,
35.0, 45.0, 56.3, 45.6, 34.0, 55.0};
float sales[12];
bool meet[12];
ifstream fp;
int id=0;
fp.open("sales.dat");
while(fp>>sales[0])
{
// Assign value from 1-7 to each line of data
id++;
// Enter the values into the array
for (int i=1; i<11; i++)
{
fp>>sales[i];
}
// Get the average
float average;
average = average_Sales(sales, 12);
// Compare sales and standard, populate the meet array
Standard_comparison(sales, standard, meet, 12);
//compute how many months meet the standard
int above;
above = above_below(meet, 12);
//compute how many months are below standard
int below = 12-above;
//display the department stat in a single line
//the performance can be displayed on the fly
print_Result(id, average, above, below);
}
}
//===============================================================================
// Calculates the annual average for a particular department
float average_Sales(float sales[], int size)
{
float sum;
for (int i=0; i<12; i++)
{
sum+=sales[i];
}
float average;
average = sum/12;
}
//================================================================================
// Compare sales figures with standard array and store stats in new (bool) array
void Standard_comparison(float sales[], float standard[], bool meet[], int size)
{
for (int i=0; i<12; i++)
{
if (sales[i]>standard[i])
meet[i]=true;
else
meet[i]=false;
}
}
//==================================================================================
// Determine whether the monthly sales were above or below average
int above_below(bool meet[], int size)
{
int counter =0;
for (int i=0; i<12; i++)
{
if (meet[i]= true)
counter ++;
}
}
//===================================================================================
void print_Result(int id, float average, int above, int below)
{
cout << setprecision(2) << fixed;
cout << "Store Statistics\n";
cout << "Dept " << "Average " << "Above " << "Below " << "Performance" << endl;
cout << "=======================================================================\n";
for (int i=0; i<7; i++)
{
cout << id++ << average << above << below << endl;
}
}
| |