Part (1): In this program, you are going to write a program to simulate the customer flows in the supermarket. The following is the function prototype which is given:   int act(int type, int NumCustomers);   This function will do the following jobs: if act is called with type=1, print out “a custom enters the supermarket”, NumCustomers++, return NumCustomers; if act is called with type=-1, that means a custom left the supermarket, if there are still customers in the supermarket, do: NumCustomers--; otherwise, print out “No Customers”; return NumCustomers   The following is the given main function, please complete the program.   #include using namespace std;   //functions prototype     int main() {    int NumCustomers=30;    NumCustomers = act(1, NumCustomers);    cout<< NumCustomers < using namespace std;   //declare global variable NumCustomers here:   //function prototype   int main() {    act(1);    cout<< NumCustomers < using namespace std; //function prototype     int main() {    act(1);    cout<< act(0) <

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

Part (1):

In this program, you are going to write a program to simulate the customer flows in the supermarket. The following is the function prototype which is given:

 

int act(int type, int NumCustomers);

 

This function will do the following jobs:

if act is called with type=1, print out “a custom enters the supermarket”, NumCustomers++, return NumCustomers;

if act is called with type=-1, that means a custom left the supermarket, if there are still customers in the supermarket, do: NumCustomers--; otherwise, print out “No Customers”; return NumCustomers

 

The following is the given main function, please complete the program.

 

#include<iostream>

using namespace std;

 

//functions prototype

 

 

int main()

{

   int NumCustomers=30;

   NumCustomers = act(1, NumCustomers);

   cout<< NumCustomers <<endl;

 

   NumCustomers = act(1, NumCustomers);

   NumCustomers = act(1, NumCustomers);

   cout<< NumCustomers <<endl;

 

   NumCustomers = act(-1, NumCustomers);

   NumCustomers = act(-1, NumCustomers);

   NumCustomers = act(1, NumCustomers);

   cout<< NumCustomers <<endl;

 

   return 0;

}

 

//Complete the functions here:

 

Part (2)

Modify your function prototype to the following:

void act(int type, int& num);

 

  • This function will do the same jobs as Part (1) except that no value will be returned since it is a void function.
  • Change your main function properly to implement the same task.

 

Part (3)

In your program of Part (2), modify your function prototype to the following:

void act(int type, int num);

keep the main function same as Part (2), then run your program again, is there any difference in your output? Why does this happen?

 

Part (4) Now you will implement this program by using global variable. The steps are as follows:

  1. Declare a global variable NumCustomers.
  2. Modify your function act, and only keep one parameter which is type as follows.

void act(int type);

  1. Modify your main function as follows, add global variable NumCustomers, and then run the program and verify your solution.

 

#include<iostream>

using namespace std;

 

//declare global variable NumCustomers here:

 

//function prototype

 

int main()

{

   act(1);

   cout<< NumCustomers <<endl;

 

   act(1);

   act(1);

   cout<< NumCustomers <<endl;

 

   act(-1);

   act(-1);

   act(1);

   cout<< NumCustomers <<endl;

 

   return 0;

}

 

//Complete function definition here

void act(int type)

{

//if act is called with type=1, that means a custom entered the supermarket, NumCustomers++;

 

//if act is called with type=-1, that means a custom left the supermarket, if there are still customers in the supermarket, do: NumCustomers--; otherwise, print out “No Customers”;

 

}

 

Part (5) Now Modify your program without using any global variable: you will implement this program by using static variable. The steps are as follows:

  1. Modify your function act as follows.

 

int act(int type)

{

            //declare NumCustomers as a static variable

//if act is called with type=1, that means a custom entered the supermarket, NumCustomers++;

 

//if act is called with type=-1, that means a custom left the supermarket, if there are still customers in the supermarket, do: NumCustomers--; otherwise, print out “No Customers”;

 

// return NumCustomers

 

}

  1. Modify your main function as follows, and then run the program and verify your solution.

 

#include<iostream>

using namespace std;

//function prototype

 

 

int main()

{

   act(1);

   cout<< act(0) <<endl;

 

   act(1);

   act(1);

   cout<< act(0) <<endl;

 

   act(-1);

   act(-1);

   act(1);

   cout<< act(0) <<endl;

 

   return 0;

}

 

//function definition

 

//please answer in c++

Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 6 steps with 11 images

Blurred answer
Knowledge Booster
Reference Types in Function
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education