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
|
#include <iostream>
#include <fstream>
#include <iomanip>
#include <cstdlib>
#include <string>
using namespace std;
//function prototypes
void displayHeader();
void readIntoSales(ifstream &fp, float Numsales, float sales[], float SIZE);
float computeAverage(float sales[], float SIZE, float Numsales);
void booleanAssignments(float sales[], float target[], bool meet[], int SIZE);
int compareAboveArray(bool meet[], float SIZE);
int compareBelowArray(bool meet[], float SIZE);
void displayResults(float &totalAverage, int &above, int &below, int &dept, int \
standard);
//the main function
int main()
{
//constant to keep all the arrays the same size
const int SIZE = 12;
string performance;
int above = 0, below = 0, dept = 0, standardValues = 0;
int compareTheResults = 0, standard = 0;
float avg = 0, Numsales, totalAverage;
float target[] = {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[SIZE] = {};
bool meet[SIZE] = {};
ifstream fp;
displayHeader();
fp.open("sales.dat");
if(!fp)
{
cout <<"Error opening the file!" <<endl;
}
int dept = 0;
while(fp.eof()==false)
{
//reads input into the sales array
readIntoSales(fp, Numsales, sales, SIZE);
//finds the average of the first dept
totalAverage = computeAverage(sales, SIZE, Numsales);
//compares the standard amount for each month with the sales and stores the boolean variable in the third array
booleanAssignments(sales, target, meet, SIZE);
//finds how many months are above the standard month values
above = compareAboveArray(meet, SIZE);
//finds how many months are below the standard month values
below = compareBelowArray(meet, SIZE);
//displays the results
displayResults(totalAverage, above, below, dept, 4);
dept++;
}
return 0;
}
//displays a header i na neat format
void displayHeader()
{
cout<<left;
cout<< "Store Statistics"<<endl;
cout<<" "<<endl;
cout<<"Dept"<<setw(10)<<"Average"<<setw(10)<<"Above"<<setw(10)<<"Below"<<setw(\
10)<<"Performance" <<endl;
cout<<"----------------------------------------------------------------" <<end\
l;
}
//reads input from the sales.dat file
void readIntoSales(ifstream &fp, float Numsales, float sales[], float SIZE)
{
fp >>Numsales;
for (int i = 0; i < SIZE; i++)
Numsales = sales[i];
}
//finds the average
float computeAverage(float sales[], float SIZE, float Numsales)
{
float sum = 0, salesAverage;
for(int i = 0; i < SIZE; i++)
{
sum += Numsales;
}
salesAverage = (sum/SIZE);
return salesAverage;
}
//assigns boolean variables to the third array
void booleanAssignments(float sales[], float target[], bool meet[], int SIZE)
{
for(int i = 0; i < SIZE; i++)
{
meet[i];
sales[i];
target[i];
if(sales[i] <= target[i])
meet[i] = false;
else
meet[i] = true;
}
}
//uses the bool array and finds how many months are above the standard
int compareAboveArray(bool meet[], int SIZE)
{
int above = 0;
for (int i = 0; i < SIZE; i++)
{
if(meet[i] == false)
above++;
}
return above;
}
//uses the bool array and finds how many months are below the standard
int compareBelowArray(bool meet[], int SIZE)
{
int below = 0;
for (int i = 0; i < SIZE; i++)
{
if(meet[i] == true)
below++;
}
return below;
}
//displays results
void displayResults(float &totalAverage, int &above, int &below, int &dept, int \
standard)
{
string performance;
if( below > standard)
performance = "unsatisfied";
else
performance = "satisfies";
dept += 1;
cout<<dept<<setw(10) <<totalAverage<<setw(10) <<above<<setw(10)<<below<<setw(1\
0) <<performance <<endl;
}
|