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

7.21 Use a two-dimensional array to solve the following problem. A company has f

ID: 3771672 • Letter: 7

Question

7.21

Use 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
b) The product number
c) The total dollar value of that product sold that day
Thus, each salesperson passes in between 0 and 5 sales slips per day. Assume that the information
from all of the slips for last month is available. Write a program that will read all this information
for last month’s sales (one salesperson’s data at a time) and summarize the total sales by salesperson
by product. All totals should be stored in the two-dimensional array sales. After processing all the
information for last month, print the results in tabular format with each of the columns representing
a particular salesperson and each of the rows representing a particular product. Cross total each
row to get the total sales of each product for last month; cross total each column to get the total
sales by salesperson for last month. Your tabular printout should include these cross totals to the
right of the totaled rows and to the bottom of the totaled columns.

Explanation / Answer

Complete Program:

#include <iostream>
#include <iomanip>
#include <string>
using namespace std;

int main()
{
const int SALESPEOPLE = 5;
const int PRODUCTS = 6;

float salesArr[SALESPEOPLE][PRODUCTS] = {0.0};
float sales;  
int sp, pr;

cout << "Enter the salesperson number (1 to 4), the product number (1 to 5),and the day sales, seperated each by a sapce." << endl;
cout << "Enter -1 for the salesperson number to end input." << endl;
cin >> sp;

while(sp != -1)
{
  cin >> pr >> sales;
  salesArr[sp][pr] += sales;
  
  cin >> sp;
}

float salesOfEachProduct[PRODUCTS] = {0.0};
float totalSales;

cout << setw(13) << 1 << setw(13) << 2 << setw(13) << 3 << setw(13) << 4 << setw(13) << 5 << setw(14) << "Total" << endl;
cout << fixed << showpoint;

for (int i = 1; i < SALESPEOPLE; i++)
{
  totalSales = 0.0;
  cout << i;

  for(int j = 1; j < PRODUCTS; j++)
  {
   totalSales += salesArr[i][j];
   cout << setw(13) << setprecision(2) << salesArr[i][j];
   salesOfEachProduct[j] += salesArr[i][j];
  }

  cout << setw(13) << setprecision(2) << totalSales << endl;
}

cout << endl << "Total" << setw(9) << setprecision(2) << salesOfEachProduct[1];

for(int j = 2; j < PRODUCTS; j++)
{
  cout << setw(13) << setprecision(2) << salesOfEachProduct[j];
}
cout << endl << endl;

system("pause");
return 0;
}

Sample Output:

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote