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
|
#include <iostream>
#include <iomanip>
#include <fstream>
using namespace std;
void displayHeader();
void readIntoSales(ifstream &fp, float Numsales, float sales[], float SIZE);
float computeAverage(float sales[], float SIZE, float &Numsales);
float compareAboveArrays(float targetNumber[], float sales[], bool meet[], float\
SIZE);
float compareBelowArrays(float targetNumber[], float sales[], bool meet[], float\
SIZE);
void displayResults(float totalAverage, float comparingInfoAbove, float comparin\
gInfoBelow);
int main()
{
float Numsales, salesInfo, totalAverage, results, comparingInfoAbove, comparin\
gInfoBelow;
const int SIZE = 12;
float targetNumber[] = {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;
fp.open("sales.dat");
if(!fp)
{
cout <<"Error opening the file!" <<endl;
}
displayHeader();
while(fp.eof()==false)
{
readIntoSales(fp, Numsales, sales, SIZE);
totalAverage = computeAverage(sales, SIZE, Numsales);
comparingInfoAbove = compareAboveArrays(targetNumber, sales, meet, SIZE);
}
displayResults(totalAverage, comparingInfoAbove,comparingInfoBelow);
}
void displayHeader()
{
cout<< "Store Statistics"<<endl;
cout<<" "<<endl;
cout<<" Dept " << " Average " << " Above " << " Below " << " \
Performance " <<endl;
cout<<"----------------------------------------------------------------" <<end\
l;
}
void readIntoSales(ifstream &fp, float Numsales, float sales[], float SIZE)
{
fp >>Numsales;
for (int i = 0; i < Numsales; i++)
sales[i] = Numsales;
}
float computeAverage(float sales[], float SIZE, float &Numsales)
{
float sum = 0;
for(int i = 0; i <SIZE; i++)
{
sum += sales[i];
}
return sum/11;
}
float compareAboveArrays(float targetNumber[], float sales[], bool meet[],float \
SIZE)
{
int counter = 0;
for (int i = 0; i < SIZE; i++)
{
if(sales[i] > targetNumber[i])
{
counter++;
}
}
return counter;
}
float compareBelowArrays(float targetNumber[], float sales[], bool meet[], float\
SIZE)
{
int counter = 0;
for (int i = 0; i < SIZE; i++)
{
if(sales[i] < targetNumber[i])
{
counter++;
}
}
return counter;
}
void displayResults(float totalAverage, float comparingInfoAbove, float comparin\
gInfoBelow)
{
cout <<totalAverage <<endl;
cout <<comparingInfoAbove <<endl;
cout <<comparingInfoBelow <<endl;
}
|