// This is based on Exercise 7.22, page 402

// This program uses a two-dimensional array to solve the following problem.
// A company has four salespeople (1 to 4) who sell five different products
// (1 to 5). Once a day, each salesperson passes in a slip for each different
// type of product sold. Each slip contains the following:
//    a) The salesperson number (1 to 4)
//    b) The product number (1 to 5)
//    c) The total dollar value of that product sold that day
// Thus, each salesperson passes in between 0 and 5 slips per day. Assume that
// the information from all of the slips for last month is available. This
// program reads all this information for last month's sales and summarizes 
// the total sales by salesperson by product. All totals are stored in a 2D
// array called sales. After reading in all the information for last month,
// the program prints the results in tabular format with each of the columns
// representing a particular salesperson and each of the rows representing a
// particular product. The program cross totals each row to get the total
// sales of each product last month; the program cross totals each column to
// get the total sales by salesperson for last month. The tabular output 
// includes these cross totals to the right of the totaled rows and to the 
// bottom of the totaled columns.

#include <iostream> 
#include <iomanip>

using namespace std;

// Global constants
   const int PEOPLE = 5;
   const int PRODUCTS = 6;

// Prototypes
void readSlips ( double arr[][PRODUCTS] );

int main()
{
   double sales[ PEOPLE ][ PRODUCTS ] = { 0.0 };
   
   // INSERT CODE: Call function readSlips() here ...

   cout << "\nThe total sales for each salesperson are displayed at the "
      << "end of each row,\n" << "and the total sales for each product "
      << "are displayed at the bottom of each column.\n " << setw( 12 ) 
      << 1 << setw( 12 ) << 2 << setw( 12 ) << 3 << setw( 12 ) << 4 
      << setw( 12 ) << 5 << setw( 13 ) << "Total\n" << fixed << showpoint;

   // Note: totalSales = total of all the sales for a  product (row totals)
   //       productSales = array of totals per salesperson (col totals)

   double totalSales; 
   double productSales[ PRODUCTS ] = { 0.0 };

   // Display salespeople and sales
   for ( int i = 1; i < PEOPLE; i++ ) 
   {
      totalSales = 0.0;
      cout << i;
      
      // add total sales, and display individual sales
      for ( int j = 1; j < PRODUCTS; j++ ) 
      {
         // INSERT CODE: Update the value of totalSales


         cout << setw( 12 ) << setprecision( 2 ) << sales[ i ][ j ];

         // INSERT CODE: Update the value of productSales[j]


      } // end inner for

      cout << setw( 12 ) << setprecision( 2 ) << totalSales << '\n';
   } // end outer for
   
   cout << "\nTotal" << setw( 8 ) << setprecision( 2 ) 
      << productSales[ 1 ];

   // display total product sales
   for ( int j = 2; j < PRODUCTS; j++ )
      cout << setw( 12 ) << setprecision( 2 ) << productSales[ j ];

   cout << endl;
   return 0; // indicates successful termination
} // end main


// Function to read in the total sales per salesperson per product
// from last month
void readSlips ( double arr[][PRODUCTS] )
{
   int salesPerson; // the salesperson's number (1 to 4; -1 quits)
   int product;     // the product number (1 to 5)
   double total;    // the total dollar value sold last month

   // Enter sales slips from last month
   cout << "Enter the salesperson (1 - 4), product number (1 - 5), and "
      << "total sales.\nEnter -1 for the salesperson to end input.\n";
   cin >> salesPerson;
   
   // continue receiving input for each salesperson until -1 is entered
   while ( salesPerson != -1 ) 
   {
      // INSERT CODE: Read in product number and total sale for that 
      //              product, then update the array arr[] ...


      cin >> salesPerson;
   } // end while
}


