An Introduction To Programming Through C++
IIT Bombay.
Week 7- Programming Assignment 2:
In 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.
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.
Sample Test Cases
Input Output
Test Case 1
a 5
a 7
a 15
d 5
a 3
c
3
7
15
Input Output
Test Case 1
a 5
a 7
a 15
d 5
a 3
c
3
7
15
WRITE/COPY THESE CODE DIRECTLY IN NPTEL TERMINAL AND COMPILE AND RUN THEN SUBMIT.
PROGRAM:#include <iostream>
using namespace std;
int main(){
bool present[100];
for(int i=0; i< 100; ++i){
present[i]=false;
}
char event;
const int MAX_EVENTS = 500;
int rno;
for(int i = 0; i < MAX_EVENTS; ++i){
cin>>event;
cin>>rno;
if(event == 'c'){
break;
}else if(event == 'a'){
present[rno] = true;
}else if(event == 'd'){
present[rno] = false;
}
}
for(int i=0; i< 100; ++i){
if(present[i]== true){
cout<< i << "\n";
}
}
return 0;
}
<<CLICK HERE FOR WEEK 7:PROGRAMMING -01>>https://www.procustoms.org/2020/03/week7-programming-assignment-1-solution.html?m=1
<<CLICK HERE FOR WEEK 7: PROGRAMMING-03>> FOLLOW OUR WEBSITE FROM THE BUTTON PROVIDED TO THE BOTTOM OF PAGE TO GET SOON ANSWERS OF PROGRAMS.
Disclaimer: Here you can find all nptel assignment solutions related to CS stream.These may help you for your assignment.The answers are only for verification.You cannot copy these directly from the post(code of conduct of NPTEL).These is not 100% correct solutions.
No comments:
Post a Comment
If you have any problems related to solutions or any concept please let me know.