Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Custom flow simulation: Part (1): In this program, you are going to write a prog

ID: 3677614 • Letter: C

Question

Custom flow simulation:

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 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<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:

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<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

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       

Explanation / Answer

Part(1):

#include<iostream>

using namespace std;

//functions prototype
int act(int type, int NumCustomers);


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:
int act(int type, int NumCustomers){
   if(type == 1){
       cout<<"a custom enters the supermarket"<<endl;
       NumCustomers++;
   }
   else if(type == -1){
       if(NumCustomers > 0){
           cout<<"a custom left the supermarket"<<endl;
           NumCustomers--;
           }else{
               cout<<"No Customers"<<endl;
               }
       }
   return NumCustomers;
   }


/*

Output:

a custom enters the supermarket
31
a custom enters the supermarket
a custom enters the supermarket
33
a custom left the supermarket
a custom left the supermarket
a custom enters the supermarket
32
*/

Part(2):

#include<iostream>

using namespace std;

//functions prototype
void act(int type, int& num);


int main()

{
   int NumCustomers=30;

   act(1, NumCustomers);

   cout<< NumCustomers <<endl;


   act(1, NumCustomers);

   act(1, NumCustomers);

   cout<< NumCustomers <<endl;

    act(-1, NumCustomers);

   act(-1, NumCustomers);

    act(1, NumCustomers);

   cout<< NumCustomers <<endl;

   return 0;

}

//Complete the functions here:
void act(int type, int& num){
   if(type == 1){
       cout<<"a custom enters the supermarket"<<endl;
       num++;
   }
   else if(type == -1){
       if(num > 0){
           cout<<"a custom left the supermarket"<<endl;
           num--;
           }else{
               cout<<"No Customers"<<endl;
               }
       }
   }


/*

Output:

a custom enters the supermarket
31
a custom enters the supermarket
a custom enters the supermarket
33
a custom left the supermarket
a custom left the supermarket
a custom enters the supermarket
32

*/

Part(3):

#include<iostream>

using namespace std;

//functions prototype
void act(int type, int num);


int main()

{
   int NumCustomers=30;

   act(1, NumCustomers);

   cout<< NumCustomers <<endl;


   act(1, NumCustomers);

   act(1, NumCustomers);

   cout<< NumCustomers <<endl;

    act(-1, NumCustomers);

   act(-1, NumCustomers);

    act(1, NumCustomers);

   cout<< NumCustomers <<endl;

   return 0;

}

//Complete the functions here:
void act(int type, int num){
   if(type == 1){
       cout<<"a custom enters the supermarket"<<endl;
       num++;
   }
   else if(type == -1){
       if(num > 0){
           cout<<"a custom left the supermarket"<<endl;
           num--;
           }else{
               cout<<"No Customers"<<endl;
               }
       }
   }


/*

Output:

a custom enters the supermarket
30
a custom enters the supermarket
a custom enters the supermarket
30
a custom left the supermarket
a custom left the supermarket
a custom enters the supermarket
30

*/

We are getting this output because in act(int type, int num), from main function we are passing NumCustomers by value, means any change in num variable inside act function will not reflect in original variable 'NumCustomers', that why we are not seeing any change in variable 'NumCustomers' inside main function. in part(2) we are passing by refrence.

Part(4):

#include<iostream>

using namespace std;

//declare global variable NumCustomers here:
int NumCustomers = 30;

//function prototype
void act(int type);
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(type == 1){
        cout<<"a custom enters the supermarket"<<endl;
        NumCustomers++;
    }
    else if(type == -1){
        if(NumCustomers > 0){
            cout<<"a custom left the supermarket"<<endl;
            NumCustomers--;
            }else{
                cout<<"No Customers"<<endl;
                }
        }

}


/*

Output:

a custom enters the supermarket
31
a custom enters the supermarket
a custom enters the supermarket
33
a custom left the supermarket
a custom left the supermarket
a custom enters the supermarket
32

*/

Part(5):

#include<iostream>

using namespace std;
#include<iostream>

using namespace std;

//functions prototype
int act(int type);

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;

}

int act(int type)

{
   // declaring a static variable
   static int NumCustomers = 30;
if(type == 1){
        cout<<"a custom enters the supermarket"<<endl;
        NumCustomers++;
    }
    else if(type == -1){
        if(NumCustomers > 0){
            cout<<"a custom left the supermarket"<<endl;
            NumCustomers--;
            }else{
                cout<<"No Customers"<<endl;
                }
        }
   return NumCustomers;
}


/*

Output:

a custom enters the supermarket
31
a custom enters the supermarket
a custom enters the supermarket
33
a custom left the supermarket
a custom left the supermarket
a custom enters the supermarket
32

*/

Problem 2:

#include<iostream>

using namespace std;

struct RainCity{
   string city;
   int rainfall;
   char restriction;
   };

char restriction(struct RainCity &city){
   if(city.rainfall < 4){
       city.restriction = 'y';
       }else
           city.restriction = 'n';
   return city.restriction;
   }
int main()

{
   RainCity city;
   cout<<"Enter name of the city: ";
   cin>>city.city;
   cout<<"Enter rainfall for city: ";
   cin>>city.rainfall;
  
   cout<<"Name of City   "<<"rainfall    "<<"restrictions in place"<<endl;
   cout<<"      "<<city.city<<"      "<<city.rainfall<<"        "<<restriction(city)<<endl;
   return 0;

}


/*

Output:

Enter name of the city: Owtowy
Enter rainfall for city: 3
Name of City   rainfall    restrictions in place
      Owtowy      3        y

*/

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote