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
|
#include <iostream>
#include <fstream>
#include <cstdlib> // ***** ADDED SO AS TO USE system COMMANDS
using namespace std;
const int LOCATIONS = 20; // ***** CHANGE TO int
void readdata(double a[], int size, ifstream& data);
double avg_array(const double a[], double data);
double sum_array(const double a[], double data);
double find_high(const double a[], double data);
double find_low(const double a[], double data);
int main()
{
ifstream in;
char in_file[256];
double avg_sales, high_sales, low_sales, total_sales;
double salesdata[LOCATIONS];
cout << "Enter the input file name:\n";
cin >> in_file;
in.open(in_file);
if(in.fail( ))
{
cout << "Input file opening failed.\n";
exit(1);
}
readdata(salesdata,LOCATIONS, in);
total_sales = sum_array(salesdata, LOCATIONS);
cout << endl << "The total sales are $" << total_sales << endl;
avg_sales = avg_array(salesdata, LOCATIONS);
cout << "The average sales amount is $" << avg_sales <<endl;
high_sales = find_high(salesdata, LOCATIONS);
cout << "The highest sales amount is $" << high_sales << endl;
low_sales = find_low(salesdata, LOCATIONS);
cout << "The lowest sales amount is $" << low_sales << endl;
system("pause");
return 0;
}
double sum_array(const double sales[], double n)
{
double sum = 0;
for(int count = 0; count < n; count++)
sum += sales[count];
return(sum);
}
double avg_array(const double sales[], double n)
{
return((double)sum_array(sales, n)/n);
}
double find_high(const double sales[], double n)
{
double highest;
highest = sales[0];
for(int count = 1; count < n; count++)
if(highest < sales[count])
highest = sales[count];
return(highest);
}
double find_low(const double sales[], double n)
{
double lowest;
lowest = sales[0];
for(int count = 1; count < n; count++)
if(lowest > sales[count])
lowest = sales[count];
return(lowest);
}
void readdata(double a[], int size, ifstream& in_data)
{
int index=0;
while(index<size)
{
in_data>>a[index];
index++;
}
} // ***** ADDED TO END THE FUNCTION
|