Computer Science, asked by manikpathria123, 11 months ago

n this exercise you will write a program that keeps track of who is in a certain classroom. At the start, the classroom is empty.Then for some time, students keep arriving and departing until the door finally closes.These events are given to the program as input consisting of lines of the following form
a x : This says student with roll number x arrived into the class room.
d x : This says student with roll number x departed from the class room.
c : This says the door closed.
After this you should print the roll numbers of the students who are in the class room (in increasing order), one per line and the program should stop.Arrivals and departures can happen in any order; and you can assume that a student departs only if she was present and arrives only if she was not present earlier. Assume further that the roll numbers are in the range 0 through 99. You should use a bool array present of size 100, and use the invariant: present[i] should be true if and only if student with roll number i is present in the class room.

Answers

Answered by yashchaudhary9529
0

Answer:

At the start, the classroom is empty.Then for some time, students keep arriving and departing until the door finally closes.

Answered by dreamrob
1

Program in C++:

#include<iostream>

using namespace std;

int main()

{

char ch;

bool present[100] = {false};

int x;

do

{

 cout<<"\n1. For arrived, press a\n2. For departed, press d\n3. For door closed, press c\nEnter your choice : ";

 cin>>ch;

 if(ch == 'a')

 {

  cout<<"Enter a roll number (0-99): ";

  cin>>x;

  if(present[x] == false)

  {

   present[x] = true;

  }

 }

 else if(ch == 'd')

 {

  cout<<"Enter a roll number (0-99): ";

  cin>>x;

  if(present[x] == true)

  {

   present[x] = false;

  }

 }

 else if(ch == 'c')

 {

  cout<<"Room closed\n";

  cout<<"\nStudents present : \n";

  for(int i = 0; i < 100; i++)

  {

   if(present[i])

   {

    cout<<i<<endl;

   }

  }

 }

 else

 {

  cout<<"Invalid choice\n";

 }  

}

while(ch != 'c');

return 0;

}

Output:

1. For arrived, press a

2. For departed, press d

3. For door closed, press c

Enter your choice : a

Enter a roll number (0-99): 5

1. For arrived, press a

2. For departed, press d

3. For door closed, press c

Enter your choice : a

Enter a roll number (0-99): 7

1. For arrived, press a

2. For departed, press d

3. For door closed, press c

Enter your choice : a

Enter a roll number (0-99): 15

1. For arrived, press a

2. For departed, press d

3. For door closed, press c

Enter your choice : d

Enter a roll number (0-99): 5

1. For arrived, press a

2. For departed, press d

3. For door closed, press c

Enter your choice : a

Enter a roll number (0-99): 3

1. For arrived, press a

2. For departed, press d

3. For door closed, press c

Enter your choice : c

Room closed

Students present :

3

7

15

Similar questions