Question

In: Computer Science

Perhaps you remember the popular TV show, Deal or No Deal, with Howie Mandel as the...

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.

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.

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

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

code

#include <iostream>
#include <stack>
#include<fstream>
using namespace std;   

int main ()
{
stack<int> s;   
   int size;
   ifstream inFile;
   bool isStack;
   inFile.open("input.txt");
  
   int num,command;
   while(inFile>>size)
   {
      
       isStack=true;
       for(int i=0;i<size;i++)
       {
           inFile>>command;
           inFile>>num;
           if(command==1)
               s.push(num);
           if(command==2)
           {
               if(s.top()==num)
                   s.pop();
               else
               {
                   isStack=false;
               }
           }
       }
       if(isStack)
           cout<<"stack"<<endl;
       else
           cout<<"not stack"<<endl;
   }
}

If you have any query regarding the code please ask me in the comment i am here for help you. Please do not direct thumbs down just ask if you have any query. And if you like my work then please appreciates with up vote. Thank You.


Related Solutions

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:...
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.
EXCEL ASSIGNMENT You are thinking of advertising Microsoft products on a popular TV music program. You...
EXCEL ASSIGNMENT You are thinking of advertising Microsoft products on a popular TV music program. You will pay 1 price for the first group of ads, but as you buy more ads, the price per ad decreases as shown in the following table... Ad number Price per ad 1-5 $12,000 6-10 $11,000 11-20 $10,000 21 or higher $9,000 For example, if you buy 8 ads, you pay $12,000 for the first 5 ads and $11,000 for each of the next...
Imagine you are on a the game show "Let's Make a Deal" and are given the...
Imagine you are on a the game show "Let's Make a Deal" and are given the opportunity to select one closed door of three, behind one of which there is a prize. The other two doors hide goats or some other such non-prize, or nothing at all. Once you have made your selection, Monty Hall will open one of the remaining doors, revealing that it does not contain the prize. He then asks you if you would like to switch...
Is there a main effect of TV show on aggression?
Results set 2 RH LPBW Insult 7 5 No insult 7 5 DV:  Aggression (noise) level on a 0–10 scale Average of Real Housewives groups = 7 Average of Little People, Big World groups = 5 Is there a main effect of TV show on aggression? (Yes or no? Then, describe. Use this format: “There is [is not] a main effect for TV show, such that_____________________________.”) There is a main effect of TV show on aggression, such that the average...
Anatomy & Physiology on TV: Watch a TV show that deals with Human Anatomy & Physiology....
Anatomy & Physiology on TV: Watch a TV show that deals with Human Anatomy & Physiology. Type a scientifically literate summary of the show. Approved shows: Discovery Channel Human Body Series, Dr. G Medical Examiner, ER, House, CSI, Grey’s Anatomy, Bones, Night Detective, Naked and Afraid…OR something you think is appropriate. Your summary should include the following: What is the name of this show? Why did you choose to watch and summarize this particular episode? Have you seen other episodes...
Deal or No Deal Assignment Have you watched the gameshow Deal or No Deal? We are...
Deal or No Deal Assignment Have you watched the gameshow Deal or No Deal? We are going to design a smaller version of it. Parameters •   We’ll have 25 differing dollar amounts from $1 to $1,000,000. (Let’s use 25, not 26; drop the 1 cent value in the original game values.) Each dollar amount is randomly placed in one of 25 briefcases, and none of the game players know the locations. •   We’ll only implement 1 Round with 4 states:...
Is there a main effect of TV show on aggression? (Yes or no? Then, describe. Use this format: “There is [is not] a main effect for TV show, such that______ are more aggressive than___________.”)
Results set 1 RH LPBW Insult 5 5 No insult 5 5 Hint: The DV is on the y-axis.  One of the IV’s is on the x-axis, the other IV is visualized with the lines (i.e., you would have a line for insult and a line for no insult, or a line for RH and a line for LPBW. DV:  Aggression (noise) level on a 0–10 scale Average of Real Housewives groups = Average of Little People, Big World groups...
Reflect on the advertising you encounter while watching a favorite TV show, reading a magazine or...
Reflect on the advertising you encounter while watching a favorite TV show, reading a magazine or newspaper, or listening to the radio( like i PHONE ADVERTISMENT )    . Choose one or more of the advertisements you encounter and discuss how specifically (if at all) it is geared to the program you are watching. Also explain how well it addresses your particular demographic and how effectively that advertisement speaks to you personally. What could the sponsors do to make this...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT