In: Computer Science
Suppose we are writing a C++ program for the sales department of a small company. The department has 4 sales person and is selling 5 products. The program will allow the input of each sale made (sales person, product, sales amount). The program will also allow a user to see the sales made by a particular sales person or the sales on a particular product.
Requirement of the Program:
1. Using a 2-D array as a data structure for storing the sales information
2. Containing a menu that allows a user to
1. Enter a sale
2. Check the sales made by a sales person
3. Check the sales on a product
4. Exit the program
Here is a sample run of the program:
Answer : Given data
CODE
#include<iostream>
using namespace std;
//function to enter sales
void Enter_sale(int sales[5][4])
{
//variables
int product,person,sale_amount;
cout<<"1. Product 1 \n2. Product 2 \n3. Product
3 \n4. Product 4 \n5. Product 5 \n";
//prompt for product
cout<<"Enter Product: ";
cin>>product;
cout<<"1. Sales Person 1 \n2. Sales Person 2
\n3. Sales Person 3 \n4. Sales Person 4 \n";
//prompt for sales
cout<<"Enter Sales Person: ";
cin>>person;
//prompt for sales amount
cout<<"Enter Sales Amount: ";
cin>>sale_amount;
//adding to previous total sales amount
sales[product-1][person-1]=sales[product-1][person-1]+sale_amount;
}
//function to check sales by a person
void Check_salesperson(int sales[5][4])
{
int person,total=0;
cout<<"1. Sales Person 1 \n2. Sales Person 2
\n3. Sales Person 3 \n4. Sales Person 4 \n";
//prompt for person
cout<<"Enter Sales Person: ";
cin>>person;
if(person>=1 && person<=4)
{
//printing details of sales by a
person
cout<<"Details of Sales
\n";
for(int i=0;i<5;i++)
{
cout<<"Product "<<i+1<<":
"<<sales[i][person-1]<<"\n";
total=total+sales[i][person-1];
}
//printing total sales by a
person
cout<<"Total:
"<<total<<"\n";
}
else
{
cout<<"Error!\n";
}
}
//function to check sales by a product
void Check_product(int sales[5][4])
{
int product,total=0;
cout<<"1. Product 1 \n2. Product 2 \n3. Product
3 \n4. Product 4 \n5. Product 5 \n";
//prompt for product
cout<<"Enter Product: ";
cin>>product;
if(product>=1 && product<=5)
{
//printing details of sales on a
product
cout<<"Details of Sales
\n";
for(int i=0;i<4;i++)
{
cout<<"Sales Person "<<i+1<<":
"<<sales[product-1][i]<<"\n";
total=total+sales[product-1][i];
}
//printing total sales on a
product
cout<<"Total:
"<<total<<"\n";
}
else
{
cout<<"Error!\n";
}
}
int main()
{
int choice;
int sales[5][4] =
{{0,0,0,0},{0,0,0,0},{0,0,0,0},{0,0,0,0},{0,0,0,0}};
while(true)
{
//Menu
cout<<"1. Enter Sales\n2.
Check Sales by a Person\n3. Check Sales on a Product\n4.
Exit\n";
//choice
cin>>choice;
switch(choice)
{
//if enter
sales
case 1:
Enter_sale(sales);
break;
//if check sales
by a person
case 2:
Check_salesperson(sales);
break;
//if check sales
on a product
case 3:
Check_product(sales);
break;
//if exit
case 4:
exit(0);
//if wrong
choice
default:
cout<<"Error!\n";
break;
}
}
}
OUTPUT
CODE SCREEN SHOT
____________THE END_____________