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

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 <

NumCustomers = act(1, NumCustomers);

NumCustomers = act(1, NumCustomers);

cout<< NumCustomers <

NumCustomers = act(-1, NumCustomers);

NumCustomers = act(-1, NumCustomers);

NumCustomers = act(1, NumCustomers);

cout<< NumCustomers <

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 globle variable. The steps are as follows:

Declare a global variable NumCustomers.

Modify your function act, and only keep one parameter which is type as follows.

void act(int type);

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

#include

using namespace std;

//declare global variable NumCustomers here:

//function prototype

int main()

{

act(1);

cout<< NumCustomers <

act(1);

act(1);

cout<< NumCustomers <

act(-1);

act(-1);

act(1);

cout<< NumCustomers <

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:

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

}

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

#include

using namespace std;

//function prototype

int main()

{

act(1);

cout<< act(0) <

act(1);

act(1);

cout<< act(0) <

act(-1);

act(-1);

act(1);

cout<< act(0) <

return 0;

}

//function definition

Problem 2: struct data type

Define a struct data type RainCity which has three elements: city, rainfall and restriction.

Declare a variable which is type of RainCity, named city.

Prompt user to enter the name of the city and rainfall for this city and save them in city. For example: Ottowa 3

Your program should call a function that will determine whether there are restrictions on water use and save it in the restriction element of city. The function will return the character y if there was less than 4 inches of rain, and n otherwise.

Your program should output the data in the following manner:

name of city rainfall restrictions in place?

Ottowa 3 yes

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!