Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Objective: Write a C++ program that reads sales data from a file. The program ou

ID: 3764291 • Letter: O

Question

Objective:

Write a C++ program that reads sales data from a file.  The program outputs the totals sales, average sales, lowest and highest sales.   

Outcomes:

1.      Read data from a file into an array.

2.      Process data in a partially filled array via functions.

3.      Pass an array as a parameter to a function.

4.      Use for and while loops when processing an array.

Specifications:

1.      Use an array to store the sales data.  Sales data should be stored as a double.

2.      The company has at most 20 locations. (Max capacity of the array)

3.      The input file contains a sales report that list the number of stores reporting data and the sales data.

4.      Sample Input file format: (samplefile1 and samplefile2)

samplefile1:

samplefile2:

3

27648.92 234.83 23458.32  

The '3' means that this file contains data for three locations. The numbers represent the sales for each location.  

5.      The main function should be modular so the main tasks must be handled by functions. Note: this is only an outline of the function requirements. You will need to decide on function names, parameter lists and return types on your own.

6.      The tasks are:  

a.      Ask the user for the file name that contains the sales data. Fill the array with the sales data from a file.

b.      Total the sales data.

c.       Average the sales data.

d.      Determine the highest sales in the report.

e.      Determine the lowest sales from the report

7.      The main function should output the data to the user.

8.      Add code to the main function to allow the user to rerun the program.   

9.      Sample output:

Enter file name: sales.dat

The total sales are $755260.00

The average sales amount is $ 75526.00

The highest sales amount is $124569.00

The lowest sales amount  is $ 35678.00

Explanation / Answer

#include <iostream>
#include <fstream>

const int LOCATIONS = 20;

void readData(double a[], int size, double& data);
double avgArray(const double a[], double data);
double sumArray(const double a[], double data);
double findHigh(const double a[], double data);
double findLow(const double a[], double data);

int main()
{
ifstream in;
char ans, in_file[256];
double data;
double avgSales, highSales, lowSales, totalSales, salesData[LOCATIONS];
cout << "Enter the input file name: ";
cin >> in_file;
in.open(in_file);

if(in.fail( ))
{
cout << "Input file opening failed. ";
exit(1);
}

totalSales = sumArray(salesData, data);
cout << endl << "The total sales are $" << totalSales << endl;
avgSales = avgArray(salesData, data);
cout << "The average sales amount is $" << avgSales <<endl;
highSales = findHigh(salesData, data);
cout << "The highest sales amount is $" << highSales << endl;
lowSales = findLow(salesData, data);
cout << "The lowest sales amount is $" << lowSales << endl;
return 0;
}

double sumArray(const double sales[], double n)
{
double sum = 0;
for(int count = 0; count < n; count++)
sum += sales[count];
return(sum);
}

double avgArray(const double sales[], double n)
{
return((double)sumArray(sales, n)/n);
}

double findHigh(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 findLow(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(int a[], int size, double& data)
{
}