Unformatted text preview:

“South Central”, “Northeast”, “Southeast” };Jacaranda (Engineering) 3333 Mail Code Phone: 818.677.6448E-mail: [email protected] 8348 Fax: 818.677.7062College of Engineering and Computer ScienceComputer Science DepartmentComputer Science 106 Computing in Engineering and ScienceSpring 2006 Class number: 11672 Instructor: Larry CarettoEngineering Building Room 2303 Mail Code Phone: 818.677.6448E-mail: [email protected] 8348 Fax: 818.677.7062May 16 homework solutions Comp 106, L. S. Caretto, Spring 2006 Page 3Homework Solutions –May 16, 2006Page 474, Program 5 – Write a program that lets the user enter four quarterly salesfigures for six divisions of a company. The figures should be stored in a two-dimensional array. Once the figures are entered, the program should display the following information for each quarter.- A list of the sales figures by division- Each division’s increase of decrease from the previous quarter. (This will not be displayed for the first quarter.- The total sales for the quarter.- The company’s increase of decrease from the previous quarter. (This will not be displayed for the first quarter.- The average sales for all divisions that quarter.- The division with the highest sales for that quarter.The program should be modular with functions that calculate these statistics. Input validation: do not accept negative numbers for sales figures.The two-dimensional array will have the form sales[period][division] to represent the sales in aparticular period from a particular division. We use the generic term period instead of the specificterm, quarter, to allow the code to be used for other reporting periods with minimal adjustments. In the code below, we have made up names for the six divisions to show how we can use arrays to generalize the code. If we had a different set of divisions, we would only have to change the array initialization statement for the division names. Similarly changing the names of the reporting periods would require only a change to the definition of the period name array. Of course, changing the number of reporting periods or the number of divisions would require changes to the symbolic constants that specify the numbers of periods and divisions.Notice that when we pass a whole array to a function, we can change elements of the array. We can do this because whole arrays are passed by reference.Notice that the use of a common input file is maintained by passing the program variable for the file name to the function as a reference to the type ofstream.#include <iostream>#include <iomanip>using namespace std;// Use global data for array sizes and array names const int MAX_DIVISIONS = 6, MAX_PERIODS = 4; string divisionName[] = {“Pacific”, “Mountain”, “North Central”, “South Central”, “Northeast”, “Southeast” }; string periodName[] = {“First Quarter”, “Second Quarter”, “Third Quarter”, “Fourth Quarter” };// Function prototypes shown belowvoid getInput( double sales[MAX_PERIODS][ MAX_DIVISIONS] );void printSalesTable( double sales[MAX_PERIODS][ MAX_DIVISIONS], double periodTotal[], ofstream& outFile );void printPercentIncrease( double sales[MAX_PERIODS][ MAX_DIVISIONS], double periodTotal[], ofstream& outFile );May 16 homework solutions Comp 106, L. S. Caretto, Spring 2006 Page 4void printTopDivision( double sales[MAX_PERIODS][ MAX_DIVISIONS], ofstream& outFile );int main(){ double sales[MAX_PERIODS][ MAX_DIVISIONS]; double periodTotal[MAX_PERIODS]; getInput( sales ); ofstream outFile; printSalesTable( sales, periodTotal, outFile ); printPercentIncrease( sales, periodTotal, outFile ); printTopDivision( sales, outFile ); return EXIT_SUCCESS;}void getInput( double sales[MAX_PERIODS][ MAX_DIVISIONS] ){// Get input data in this routine. Prompt user for keyboard// input, checking for negative sales figures cout << “Enter sales data for each division for each reporting “ << “ period.\n” for ( int division = 0; division < MAX_DIVISIONS; division++ ) { cout << “Enter sales data for the “ << divisionName[division] << “ Division\n” for ( int period = 0; period < MAX_PERIODS; period++ ) { cout << periodName[period] << “ Sales: “; cin >> sales[period][division];// Ensure that sales data are not negative do while ( sales[period][division] < 0 ) { cout << “\nERROR – you cannot have negative sales!\n” cout << “Renter “ << periodName[period] << “ Sales: “; cin >> sales[period][division]; } } }}void printSalesTable( double sales[MAX_PERIODS][ MAX_DIVISIONS], double periodTotal[], ofstream& outFile ){// Print sales table showing the sales for each division// on one row. That row will show the sales for each// period and the total for all periods. // Start by printing title then print table header with //period names “Total” outFile << setw(50) << “Sales for each division by reporting period\n\n” << setw(15) << left << “Division” << right; for ( int period = 0; period < MAX_PERIODS; period++ ) { outFile << setw(16) << periodName[period];May 16 homework solutions Comp 106, L. S. Caretto, Spring 2006 Page 5 } outFile << setw(16) << “Total Sales\n\n”// Print sales data for each division here. Compute // total sales per division in variable total; grandTotal // is variable to hold sales for all divisions. The // array periodTotal[period] computes the total for all // divisions in a given period. double grandTotal = 0; for ( int division = 0; division < MAX_DIVISIONS; division++ ) { outFile << setw(15) << left << divisionName[division] << right << fixed << setprecision(2); double total = 0; for ( int period = 0; period < MAX_PERIODS; period++ ) { outFile << sales[period][division]; total += sales[period][division]; periodTotal[period] += sales[period][division]; } outFile << setw(16) << total << endl; grandTotal += total; }// Print totals and averages for all divisions outFile << “\n\n” << setw(15) << left << “Period Totals” << right; for ( int period = 0;


View Full Document

CSUN COMP 106 - Homework Solutions

Download Homework Solutions
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Homework Solutions and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Homework Solutions 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?