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.
#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;
Comments
Leave a comment