Answer to Question #254176 in C for Hazzy

Question #254176

Exercise 3 : Practice to manipulate 2D arrays

Use a double-subscripted 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:

a) The salesperson number

b) The product number

c) The total dollar value of that product sold that day

Assume that the information from all the slips for a day is available. Write a program that will read all this information for the day and store in a double-subscripted array sales. For each product find and display the total sales.



1
Expert's answer
2021-10-20T12:46:58-0400
#include <iostream>
using ​namespace std;


int main()
{
    const int salespeoples{ 4 };
    const int products{ 5 };
    double sales[salespeoples][products]{ 0 };
    int salespeopl;
    int prod;
    float sale;
    // Getting data per day
    for (int i = 0; i < salespeoples * products; i++) 
    {
        cout << "Enter the seller number 1 ... 4 ";
        cin >> salespeopl;
        cout << "Enter product number 1 ... 5 ";
        cin >> prod;
        cout << "Enter the sale amount ";
        cin >> sale;
        // We take into account the fact the numbering of
        // arrays starts from zero
        sales[salespeopl - 1][prod - 1] += sale;
    }
    cout << "Total sale:" << "\n";
    for (int i = 0; i < products; i++) 
    {
        sale = 0;
        for (int j = 0; j < salespeoples; j++) 
        {
            sale += sales[j][i];
        }
        cout <<"Product " << i + 1 << " sales amount: "<<sale << "\n";
    }
    return 0;

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS