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

/* 2170_3_2.cc Problem description: Assume that the input values contain a sales

ID: 3548300 • Letter: #

Question

/* 2170_3_2.cc
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 <iostream>
#include <string>
#include <iomanip>

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<NumOfSalesman; index++ )
    {
        cin >> 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<NumOfSalesman; index ++ )
    {   // output each row
        cout << 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

1.

int sum=0;
for(int i=0;i<NumOfSalesman;i++)
{
    sum=0;
    for(int j=0;j<4;j++)
    {
        sum+=salesreport[i][j];
    }
    salesmanTotal[i]=sum; //total sales of each salesman
    salesmanAvg[i]=sum/4; //avg sales of each salesman
}


2.
for(int i=0;i<4;i++)
{
    sum=0;
    for(int j=0;j<NumOfSalesman;j++)
    {
        sum+=salesreport[j][i];
    }
    quarterTotal[i]=sum; //total sales of each quarter
}