Problem description: Assume that the input values contain a sales report of a co
ID: 3527999 • Letter: P
Question
Problem description: Assume that the input values contain a sales report of a company. Write a program that calculates the average sales and the total sales of each salesman as well as the total sales of each quarter. For example, for the following sales report: Salesman Q1 Q2 Q3 Q4 Willie 11.3 9.1 4.6 2.1 Biff 1.1 0.5 2.7 1.2 Al 3.4 2.5 1.2 2.3 Dave 23.7 19.9 34.3 28.5 Jack 17.4 22.8 51.8 12.5 Bill 143.9 97.8 114.2 88.8 the output of the program should look like the following: Salesman Q1 Q2 Q3 Q4 Total Average Willie 11.3 9.1 4.6 2.1 27.1 6.78 Biff 1.1 0.5 2.7 1.2 5.5 1.38 Al 3.4 2.5 1.2 2.3 9.4 2.35 Dave 23.7 19.9 34.3 28.5 106.4 26.60 Jack 17.4 22.8 51.8 12.5 104.5 26.13 Bill 143.9 97.8 114.2 88.8 444.7 111.18 Total 200.8 152.6 208.8 135.4 */ #include #include #include using namespace std; int main( ) { const int NumOfSalesman = 6; // the number of salesmen string salesman[NumOfSalesman]; // contains the names of salesmen float salesreport[NumOfSalesman][4]; // contains the sales report float salesmanTotal[NumOfSalesman]; // the total sales of salesmen float salesmanAvg[NumOfSalesman]; // the average sales of salesmen float quarterTotal[4]; // the taotal sales of each quarter // read the sales report cin.ignore(256, ' '); // ignore the first line -- the table head for ( int index=0; index> salesman[index]; // read the name of the first salesman // read quarter sales of the salesman specified by the index for ( int quarter=0; quarter<4; quarter++ ) cin >> salesreport[index][quarter]; } //1. calculate the total sales and average sales of each sales man //2. calculate the total sales of each quarter // output the table head cout << setw(10) << left << "Salesman" << setw(10) << right << "Q1" << setw(10) << right << "Q2" << setw(10) << right << "Q3" << setw(10) << right << "Q4" << setw(15) << right << "Total" << setw(10) << right << "Average" << endl << endl; // output statistics of each salesman for ( int index=0; index<< setw(10) << left << salesman[index]; for ( int quarter=0; quarter<4; quarter++ ) cout << setw(10) << setprecision(2) << fixed << right << salesreport[index][quarter]; cout << setw(15) << setprecision(2) << fixed << right << salesmanTotal[index] << setw(10) << setprecision(3) << fixed << right << salesmanAvg[index] << endl; } //output quarter total cout << endl << setw(10) << left << "Total"; for( int quarter=0; quarter<4; quarter++ ) cout << setw(10) << right << setprecision(2) << fixed << quarterTotal[quarter]; cout << endl; return 0; }Explanation / Answer
When we made the Sales book program the first time around, we ended up with a way for a user to enter in a set of Sales and calculate the average. However, our program did not maintain the individual Sales values in instance variables of the class. Thus, repeat calculations require the user to reenter the same Sales. One way to solve this problem would be to store each of our Sales entered in an individual instance of the class. For instance, we could create instance variables Sales1, Sales2, Sales3 etc. to store our Sales, but this would be cumbersome and time-consuming. This looks like a job for an array. In most courses, though, Salesmans take several exams and do several assignment. Instructors are likely to want to analyze Sales across the entire length of the course--both for a single Salesman and for the class as a whole. We will demonstrate a multidimensional array here. **/ public class SalesBook { private String courseName;//the name of the course this SalesBook represents private int[][] Sales; // array of Salesmans Sales //two-argument constructor initializes courseName and Sales array public SalesBook(String name, int[][] SalesArray) { courseName = name;//initialize courseName Sales = SalesArray;//store Sales }//end two-argument SalesBook constructor //method to set the course name public void setCourseName(String name) { courseName = name; //store the course name }//end method setCourseName //method to retrieve the course name public String getCourseName() { return courseName; }//end method getCourseName //display a welcome message to the SalesBook user public void displayMessage() { //getCourseName gets the name of the course System.out.printf("Welcome to the Sales book for %s! ", getCourseName() ); }//end method displayMessage //perform various operations on the data public void processSales() { //output Sales array outputSales(); //call methods getMinimum and getMaximum System.out.printf("Lowest Sales is %d Highest Sales was %d ", getMinimum(), getMaximum()); //call outputBarChart to print Sales distribution chart outputBarChart(); }//end method processSales public int getMinimum() { int lowSales = Sales[0][0];//assume Sales[0] is smallest //loop through rows of Sales array /** in this method, we want to loop through the two-dimensional array Sales. To accomplish this, the outer enhanced for loop will iterate through Sales, assigning successive rows to our parameter, int[] SalesmanSales on successive iteration. The square brackets following the parameter name indicate that SalesmanSales refers to a one-dimentional int array--namely, a row in array Sales containing one Salesman's Sales. To fined the lowest Sales, the inner for loop will compare the elements of the current one dimensional array SalesmanSales to variable variable low Sales. For instance, on the first iteration, the outer for, row 0 of Sales is assigned to parameter SalesmanSales. The inner enhanced for loop will go through SalesmanSales and compares each Sales value with lowSales. If a Sales is less than lowSales, lowSales is set to that Sales. On the second loop, row 1 of Sales is assigned to SalesmanSales, and the elements of this row are compared with variable lowSales. This repeat until all rows of Sales have been traversed. When execution of the nested statement is complete, lowSales contains the lowest Sales in the two-dimensional array. Method getMaximum works similarly to method getMinimum. **/ for(int[] SalesmanSales : Sales) { //loop through columns of current row for(int Sales : SalesmanSales) { //if Sales less than lowSales, assign it to loSales if(Sales highSales) highSales = Sales; }//end inner for }//end for return highSales; //return highest Sales }//end getMaximum public double getAverage(int[] setOfSales) { int total = 0; //initialize total //sum Sales for one Salesman for(int Sales : setOfSales) total += Sales; //return average of Sales return (double) total / setOfSales.length; }//end getAverage public double getTotal(int[] totalOfSales) { int total = 0;//initialize total // int Salary = 200; //initiailize total at 200 and add commission // double Commission = total * 0.09; // double Pay = Salary + Commission; //sum Sales for one Salesman for(int Sales : totalOfSales) total += Sales; //return total of sales of our use return (double) total; // return Pay; }//end getTotal public double getPay(int[] getPay) { int total = 0; for(int Sales : getPay) total += Sales; return (double) total * 0.09 + 200; }//end pay //output bar chart displaying Sales distribution /** To output the overall Sales distribution for a whole semester, the method here uses nested enhanced for loops to create the one-dimensional array frequency based on all the Sales in the two-dimensional array. **/ public void outputBarChart() { System.out.println("Sales distribution: "); //stores frequency of Sales in each range of 10 Sales int[] frequency = new int[11]; //for each Sales, increment the appropriate frequency for(int[] SalesmanSales : Sales ) { for(int Sales : SalesmanSales) ++frequency[Sales/1000]; }// end for //for each Sales frequency, print bar in chart for(int count = 0; countRelated Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.