You have been asked to write a program to produce a sales report similar to the one below that indicates individual percentage contribution by each employee. The program must allow the user to enter a user id number and sales amount for up to ten employees, but must allow fewer to be entered if desired, stopping input when the user enters the value zero for an employee id number.
Emp.ID $ Sales % Sales ------------------------ 101 3000.00 30 102 1000.00 10 103 4000.00 40 104 2000.00 20 ------------------------ Total: 10000.00 100
Produce a sales report that indicates individual percentage contribution by each employee. The program must allow the user to enter a user id number and sales amount for up to ten employees, but must allow fewer to be entered if desired, stopping input if the user enters the value zero for an employee id number.
Initially, you might think that this task could be accomplished using a simple sentinel controlled loop that re-uses just a few variables similar to those shown in the web page about Accumulation Using a Sentinel Loop. In this problem, the difficulty lies in the calculation of the percentages in the right-most column. It is impossible to calculate any employee's percentage of sales until all the employee sales data has been entered and totaled. This fact would prevent you from being able to output each employee's row until all the employee data was entered. And that fact would require that all data be retained throughout the entire program, preventing the re-use of just a few storage locations in a loop. The challenge here is to find a way to manipulate multiple storage locations (such as the four dollar sales amounts shown above) without having to define and manipulate many separate variable labels. The analysis below shows how to do this using storage arrays.
In the sample output below, the numbers to the left of the sample screen are there only for your reference in other documentation. They are not intended to be part of the output. Bracketed text represents user input, not program output. "<CR>" represents the carriage return and/or line feed sequence necessary to start a new line of output. The three columns of the report at the bottom are each 8 characters wide with alignment and rounding as shown below.
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 |
SALES REPORT PROGRAM<CR> by Susan Student - March 1, 2008<CR> <CR> Employee ID Number (0 when done)? 101<CR> Sales volume for this period? $ 3000.00<CR> <CR> Employee ID Number (0 when done)? 102<CR> Sales volume for this period? $ 1000.00<CR> <CR> Employee ID Number (0 when done)? 103<CR> Sales volume for this period? $ 4000.00<CR> <CR> Employee ID Number (0 when done)? 104<CR> Sales volume for this period? $ 2000.00<CR> <CR> Employee ID Number (0 when done)? 0<CR> <CR> User input complete.<CR> <CR> EMPLOYEE SALES WITH PERCENTAGE BREAKDOWN<CR> <CR> Emp.ID $ Sales % Sales<CR> ------------------------<CR> 101 3000.00 30<CR> 102 1000.00 10<CR> 103 4000.00 40<CR> 104 2000.00 20<CR> ------------------------<CR> Total: 10000.00 100<CR> |
| LABEL | DESCRIPTION | DATA TYPE | VALUE | USAGE | DESTINATION |
|---|---|---|---|---|---|
| MAX | Maximum quantity of employees | Integer | 10 | Tested | --- |
| SV | Loop sentinel value | Integer | 0 | Tested | Screen |
| LABEL | DESCRIPTION | DATA TYPE | SOURCE | USAGE | DESTINATION |
|---|---|---|---|---|---|
| ARRAYS: | |||||
| EID | Employee ID number | Integer | Assigned | --- | Screen |
| S | Dollar sales for each employee |
Floating point | Keyboard | For P and accumulated in T |
Screen |
| P | Percentage of total sales for each employee |
Floating point | Calculated | Accumulated in TP | Screen |
| SCALARS: | |||||
| EIN | Response to EID Question | Integer | Keyboard | Sentinel test & for EID |
--- |
| C | General purpose counter | Integer | Set to 0 | Loop index | --- |
| Q | Quantity of employees | Integer | Set to 0 | Loop index and tested |
--- |
| T | Total sales for all employees | Floating point | Set to 0.0 | Accumulates S & for P |
Screen |
| TP | Total percentage sales | Floating point | Set to 0.0 | Accumulates P | Screen |
Although programs involving repetition structures are often developed using flowcharts, an outline style algorithm is being provided here for the benefit of those students who have not yet learned to use flowcharts well. The outline below shows an alternative way to describe repetitive processes. Pay careful attention to the indentation level of each step in the outline. It will help you to see where to use braces in the C++ source code to block statements so as to nest the structures within the algorithm properly.
As a practice exercise (although not as part of this assignment), you might want to try to draw the flowchart for this algorithm to develop more skill in using them.
Note: in the document below, "<CR>" represents the carriage return and/or line feed sequence necessary to start a new line of output.
A. Start
B. Display the program title and credits on lines 1-2.
C. Initialize Q and T to both be zero.
D. Perform the following steps (to be repeated based on conditions tested after each pass):
D.1. Request and store a value for EIN.
D.1.a. Display a <CR>.
D.1.b. Prompt for EIN without carriage return.
D.1.c. Store keyboard response in EIN and then display a <CR>. (see cin)
D.2. Test the condition: EIN equals SV
D.2.a. If true, then:
D.2.a.1. Display a <CR>
D.2.a.2. Display "User input complete." and <CR>.
D.2.b. Otherwise, do the following:
D.2.b.1. Store (assign) the value of EIN in the number Q element of EID
D.2.b.2. Prompt for the number Q element of S without <CR>.
D.2.b.3. Store keyboard response in the number Q element of S and then display a <CR>.
D.2.b.4. Accumulate the value of the number Q element of S in T.
D.2.b.5. Increment Q by 1.
D.3. Repeat step D while Q is less than MAX and EIN is not SV.
E. Test the condition: Q equals zero
E.1. If true, then beep and display the lines:
E.1.a. No employee data entered.<CR>
E.1.b. Program terminated.<CR>
E.2. Otherwise:
E.2.a. Store zero in TP.
E.2.b. Store a zero in C.
E.2.c. While C is less than Q, perform the following steps
E.2.c.1. Divide the number C element of S by T and multiply the result by 100,
then store the result in the Cth element of P.
E.2.c.2. Accumulate the Cth element of P in TP
E.2.c.3. Increment C by 1
E.2.c.4. Loop back to step E.2.c.
E.2.d. Display the report heading as shown on lines 19-23.
E.2.d.1. Display a <CR>.
E.2.d.2. Display the line "EMPLOYEE SALES WITH PERCENTAGE BREAKDOWN" and <CR>.
E.2.d.3. Display a <CR>.
E.2.d.4. Display the line "Emp.ID $ Sales % Sales" and <CR>.
E.2.d.5. Display the line "------------------------" and <CR>.
E.2.e. Store a zero in C.
E.2.f. While C is less than the Q value do (repeat) the following steps:
E.2.f.1. Display the Cth element of EID, S, and P, followed by a carriage return
using the output format illustrated on lines 24-27 of the sample output.
E.2.f.2. Increment C by 1.
E.2.f.3. Loop back to step E.2.f.
E.2.g. Display the report summary as shown on lines 28-29.
E.2.g.1. Display the line "------------------------" and <CR>.
E.2.g.2. Display the totals T and TP as shown on line 29 and <CR>.
F. End
The Tracing Chart for problems using arrays can be quite large because each element of each array should be given its own column, along with any logical expressions. The desk check for this algorithm has been omitted for the sake of brevity in this assignment. Desk checks for programs that have loops can be quite long and often are shortcut by starting them and going through the first few passes and then jumping to a value of the control variable near the final value of the loop and finishing the test there. This practice has obvious risks, but is widely practiced by many professionals (with varied rates of success).
/***********************************************************
* array_example.cpp - A program to produce a Sales Report *
***********************************************************/
#include <iostream> // Load the Input/Output STREAM Library
using namespace std; // to define context for cin and cout
#include <iomanip> // Load the Input/Output Stream MANIPulator Library
int main ()
{
/* DECLARATIONS */
#define MAX 10
#define SV 0
long int EID [MAX];
double S [MAX];
float P [MAX];
long int EIN;
int C, Q;
double T;
float TP;
/* EXECUTABLE STATEMENTS */
cout << "SALES REPORT PROGRAM\n";
cout << "by Susan Student - October 1, 2011\n";
Q=0; T=0;
do {
cout << "\nEmployee ID Number (" << SV << " when done)? ";
cin >> EIN;
if (EIN==SV) cout << "\nUser input complete.\n";
else {
EID[Q] = EIN;
cout << "Sales volume for this period? $";
cin >> S[Q];
T = T + S[Q];
Q = Q + 1;
}
} while (Q<MAX && EIN!=SV);
if (Q==0) cout << "\aNo employee data entered.\nProgram terminated.\n";
else {
TP = 0;
for (C=0; C<Q; C++) { P[C] = 100*S[C]/T; TP = TP + P[C]; }
cout << "\nEMPLOYEE SALES WITH PERCENTAGE BREAKDOWN\n\n";
cout << "Emp.ID $ Sales % Sales\n";
cout << "------------------------\n";
for (C=0; C<Q; C++)
{
cout << setw(8) << left << EID[C];
cout << setw(8) << right << setprecision(2) << fixed << S[C];
cout << setw(8) << P[C] << endl;
}
cout << "------------------------\n";
cout << "Total: " << setw(8) << T << setw(8) << TP << endl;
}
return 0;
}