Question

In: Computer Science

In C++, please. Thank you! Perhaps you remember the popular TV show, Deal or No Deal,...

In C++, please. Thank you!

Perhaps you remember the popular TV show, Deal or No Deal, with Howie Mandel as the host, that ran several years ago. In honor of the show returning in its original form (and renewed for a fifth season in 2019!) this lab is called Stack or No Stack.

As we studied in Chapter 1, imagine again a bag data structure into which we can insert and remove elements. This bag has two operations, defined as:

1 x Insert an element (with value x) into the bag
2 x Take an element from the bag (the value x)

This time the bag is somewhat mysterious. Given a sequence of inserts and removals, the bag may operate like a LIFO stack, or it may not.

Your program must guess whether or not the bag is operating as a stack given a series of operations and the corresponding return values.

Program Input

The input to your program will be a series of test cases in a file. Each test case begins with a line containing a single integer n (1 < n < 100). Following the operations defined in the above table, each of of the next n lines is either a type-1 command followed by an integer x (which inserts the value x) or a type-2 command followed by an integer x which means the command retrieves the value x. The value of x is always a positive integer not larger than 100. There will be no ambiguous test cases. In other words, you will be able to fully determine the output based on the input alone. The input is terminated by an end-of-file (EOF).  

Your code may use anything in the standard C++ library -- including the STL stack container. This is, of course, optional.

Program Output

For each test case, output one of the following:

stack
not stack

which will indicate whether or not the bag is determined to be operating as a stack, given the series of operations.

What to Submit

Please submit a .cpp file which contains your solution to the problem. Your program should take its input from a file. You will want to write some test driver code but you are not required to submit it as part of the assignment Do so only if it is convenient.

How to Earn Full Credit

Your code should compile, run, and pass all of the below test cases.

Code that does not compile or pass at least the below test cases will have points deducted. Your professor has held back a few extra cases that your program must also pass to receive full credit. Your program should output only 'stack' or 'not stack' once per line, with one line for each test case.

Sample Input

Create a file with the following lines and use it as input into your program. Please use at least these as your test cases before submitting your code in Canvas.

4
1 2
1 1
2 1
2 2
6
1 5
1 10
1 12
2 10
2 5
2 12
2
1 8
2 8

Sample Output

stack
not stack
stack

Solutions

Expert Solution

here is a code for the problem

I am reading the input from file

#include<bits/stdc++.h>
using namespace std;
int main(){
ifstream fin("file");//reads input from the file
string ch;
vector<string>input; // to store values of input that is type of 1
vector<string>output; //to stroe values of output that is type if 2
while(fin){ //reading file data
getline(fin, ch); // getting data from fin and writing it to ch line bt line
cout<<ch<<endl;
if(ch.size()==1){ // when it size of input line 4 in first input test case
if(output.size()==0) // if output file is empty ie for first case
continue;
reverse(output.begin(),output.end()); // reversing output , because stack is LIFO ie last in first out so we will check if they are same or not
  
if(input==output){
cout<<"stack"<<endl;
}
else{
cout<<"not stack"<<endl;
}
input.clear();// clearing input and output for next input
output.clear();
}
else{
if(ch[0]=='1'){ //checking type if type 1 or type 2
string data = "";
for(int i = 2;i<ch.size();i++){
data+=ch[i];
}
input.push_back(data); // stroing data for input
}
else {
string data = "";
for(int i = 2;i<ch.size();i++){
data+=ch[i];
}
output.push_back(data); //storing data for output
  
}
}
  
  
}
  
//its for the last case when file end so that we can check the output
if(output.size()!=0){
output.pop_back(); //i am removing last element for output because it is reading it two time in every case ans this
//is the cornor case we have to check.. it might be because of comiler of what . you must check it once if it working for you or not
// but its working for me
reverse(output.begin(),output.end());
if(input==output){
cout<<"stack"<<endl;
}
else{
cout<<"not stack"<<endl;
}
}
}

output-file

file image I used to read input

if any doubt. comment below.


Related Solutions

Please solve A, B, and C. Please use excel. Please show work. Thank you. A. Use...
Please solve A, B, and C. Please use excel. Please show work. Thank you. A. Use the stocks of Apple, SAP, IBM, Oracle, and Amazon Download the historical data of weekly stock prices and S&P 500 index prices from year 2017-2019 on the website of yahoo finance and save it on an excel file. B. Use a different sheet to save the market adjusted prices of Apple, SAP, IBM, Oracle, and Amazon t and the index. For each stock, compute...
A study looked at if TV show content influenced the ability of watchers to remember brand...
A study looked at if TV show content influenced the ability of watchers to remember brand names of items in commercials. Researchers randomly assigned volunteers to watch one of three programs, each containing the same nine commercials. After the show ended, the volunteers were asked to remember the brands of products that were advertised. Below shows how many of the nine commercials the volunteers remembered correctly: Program | Size | Mean | SD Violent | 108 | 2.08 | 1.87...
On the popular TV show LOST, the passengers on an airplane experience rapid decompression (i.e. going...
On the popular TV show LOST, the passengers on an airplane experience rapid decompression (i.e. going from high pressure to low pressure) when the plane breaks in half. The plane then rapidly descends (crashes), putting the people through a rapid change from low pressure to high pressure. Explain in detail how going rapidly from high pressure to low pressure and then from low pressure to high pressure would kill most people before they impacted the ground.
Can you please show me the work on how you get your answer please (thank you...
Can you please show me the work on how you get your answer please (thank you in advance kings and queens of chegg) Problem 1: The following selected information is provided about a manufacturing company: Raw material purchases800,000 Direct labor 415,000 Overhead applied730,000 Actual overhead745,000 Selling and administrative salaries500,000 Other selling and administrative expenses185,000 Sales revenue5,000,000 Inventory data: January 1 December 31Raw material 75,000 100,000Work in process 105,000 140,000Finished goods 120,000 125,000Calculate the cost of goods sold. Assume that under/over...
Please answer all, thank you. If cats become a more popular pet in the United States...
Please answer all, thank you. If cats become a more popular pet in the United States than they are now, what can we expect to happen to the market for cat food workers? wage decreases MP increases     MRP decreases MRP increases Which of the following could be a perfectly competitive market? the market for patented pharmaceuticals             the market for restaurants with permits to sell alcohol    the market for tradable stocks   the market for licensed electricians Suppose the price of HD...
PLEASE show you work so it is easier to undersand, thank you!! There are two parts...
PLEASE show you work so it is easier to undersand, thank you!! There are two parts to this problem a) & b) A company made the following expenditures in connection with the construction of a new building: Architect’s fees $12,000 Cash paid for land and unusable building on the land 300,000 Removal of old building 18,000 Salvage from sale of old building materials -4,000 Construction survey 1,500 Legal fees for title search 3,000 Excavation for basement construction 25,000 Machinery purchased...
Please if you could do each problem, and show steps for excel! Thank you! You will...
Please if you could do each problem, and show steps for excel! Thank you! You will graduate in a few years and start working and it’s never too early to start planning for your retirement and other financial events. Let’s fast forward to the beginning of your career. Here’s some assumptions to help you get started. Your starting annual salary will be $60,000. You plan to work for 43 years before retiring. You expect your salary to grow at an...
PLEASE SHOW WORKING IF POSSIBLE AS WELL. THANK YOU. 1) Suppose there is a market for...
PLEASE SHOW WORKING IF POSSIBLE AS WELL. THANK YOU. 1) Suppose there is a market for electronic bikes that is currently at equilibrium. If the consumers of electronic bikes had an increase in income which resulted in a rightward parallel shift of the demand for electronic bikes, then at any given price, the price elasticity of demand will have: A) increased in absolute terms. B) decreased in absolute terms. C) remained unchanged. D) increased, decreased or stayed the same. It...
Please show work thank you! In a random sample of 519 judges, it was found that...
Please show work thank you! In a random sample of 519 judges, it was found that 285 were introverts. a) Let p represent the proportion of all judges who are introverts. Find a point estimate for p. b) Find a 99% confidence interval for p. Give a brief interpretation of the meaning of the confidence interval you have found. c) Do you think the confidence np > 5 and nq > 5 are satisfied in this problem? Explain why this...
Please show the work for how to get the answer. Thank you in advance. Assume that...
Please show the work for how to get the answer. Thank you in advance. Assume that all of the mass of a bicycle wheel is concentrated at its rim. Such a wheel of mass 1.2 kg and radius 30 cm starts from rest at the top of a hill 100 m long and inclined at 20� to the horizontal. What will be the speed of the wheel at the bottom of the hill if it rolls without slipping? (Answer: 15...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT