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

PLEASE WRITE IN C++ Write a program that reads sales data from a file. The progr

ID: 3805562 • Letter: P

Question

PLEASE WRITE IN C++

Write a 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)

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
   

10.      Extra Credit (15 points) – Write two additional functions:

a.       Write a function that sorts the arrays sales data in ascending order.  You may not use selection sort.   Bubble sort example.

b.      Write a function that displays all the elements of the array in a nicely formatted output.

Explanation / Answer

SalesData.CPP:

_____________

#include<iostream.h>

#include<fstream.h>

#include<conio.h>

#include<string.h>

const int MAX_CAPACITY = 20;

int number_locations = 0;

void sort_array(double[]);

void print_elements(double[]);

int main()

{

ifstream infile,salefile;

char name[30],ch = '';

int i;

double sales[MAX_CAPACITY];

clrscr();

infile.open("locations.txt");

infile>>number_locations;

do

{

cout<<" Enter file name that contains sales data:";

cin>>name;

salefile.open(name);

for(i=0;i<number_locations;i++)

salefile>>sales[i];

double total = 0,high_sale = 0,low_sale = 999999.999;

for(i=0;i<number_locations;i++){

total += sales[i];

if(sales[i] > high_sale)

high_sale = sales[i];

if(sales[i]<low_sale)

low_sale = sales[i];

}

double avgsale = total / number_locations;

cout<<" The total sales are $"<<total;

cout<<" The average sales amount is $"<<avgsale;

cout<<" The highest sales amount is $"<<high_sale;

cout<<" The lowest sales amount is $"<<low_sale;

print_elements(sales);

sort_array(sales);

cout<<" If you want to repeat the above process, type 'r' otherwise type any key:";

cin>>ch;

}while(ch=='r');

infile.close();

salefile.close();

getch();

return 0;

}

void sort_array(double s[]){

int i,j;

double temp = 0;

for(i=0;i<number_locations;i++)

for(j=i+1;j<number_locations;j++)

if(s[i]>s[j]){

temp = s[i];

s[i] = s[j];

s[j] = temp;

}

cout<<" Sorted sales data:"<<endl;

for(i=0;i<number_locations;i++)

cout<<"sale"<< i+1<<" $"<<s[i]<<endl;

}

void print_elements(double s[]){

cout<<" Sales data:"<<endl;

for(int i=0;i<number_locations;i++)

cout<<"sale"<< i+1<<": $"<<s[i]<<endl;

}

locations.txt :

___________

5

sales.txt :

________

127648.92 234567.54 75367.89 32887.98 87943.76

Sample Input and Output :

_______________________

Enter file name that contains sales data:sales.txt

The total sales are $558416.09

The average sales amount is $111683.218

The highest sales amount is $234567.98

The lowest sales amount is $32887.98

Sales data:

Sale1 : $127648.92

Sale2 : $234567.54

Sale3 : $75367.89

Sale4 : $32887.98

Sale5 : $87943.76

Sorted sales data:

Sale1 : $32887.98

Sale2 : $75367.89

Sale3 : $87943.76

Sale4 : $127648.92

Sale5 : $234567.54

If you want to repeat the above process, type 'r' otherwise type any key: p

/*It will be terminated */

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote