0% found this document useful (0 votes)
111 views23 pages

Bonus Problems

The code implements a doubly ended queue (deque) using a queue. A deque supports insertion and removal of elements from both the front and back. It inherits the queue class and overloads the push_front, push_back, pop_front, and pop_back methods to allow adding/removing from either end while maintaining queue behavior. Elements are stored in a circular array and front and rear pointers are used to track the first and last elements.

Uploaded by

Muskan Agarwal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
111 views23 pages

Bonus Problems

The code implements a doubly ended queue (deque) using a queue. A deque supports insertion and removal of elements from both the front and back. It inherits the queue class and overloads the push_front, push_back, pop_front, and pop_back methods to allow adding/removing from either end while maintaining queue behavior. Elements are stored in a circular array and front and rear pointers are used to track the first and last elements.

Uploaded by

Muskan Agarwal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 23

QUESTION-1 List of even integers

void mobile::get(){

cin>>num_Tower_Pts;

for(int i=0;i<num_Tower_Pts;i++){

point p;

p.get();

tower_Pts.push_back(p);

mobile_Pt.get();

point mobile::find_Max(){

point p;

list<point>::iterator itr;

int largest=0;

for(itr=tower_Pts.begin();itr!=tower_Pts.end();itr++){

int distance=(*itr).dist(mobile_Pt);

if(largest<distance){

largest=distance;

p=(*itr);

return p;

void point::get(){
cin>>name;

cin>>x;

cin>>y;

void point::print(){

cout<<name;

int point::dist(point p){

if((p.x%2)!=0 ||(p.y)%2!=0 || (this->x)%2!=0 || (this->y)%2!=0){

cout<<"Invalid input";

exit(0);

return (pow((p.x-this->x),2)+pow((p.y-this->y),2));

}
QUESTION-2 JOB with or without priority
void job::get(){

cin>>id;

cin>>time;

int job::getid(){

return id;

float job::gettime(){

return time;

int priority[20];

int a=0;

void job_Sch::get(){

cin>>num;

for(int i=0;i<num;i++){

j[i].get();

if(a==1){
cin>>priority[i];

void job_Sch::find_Max(){

int largest=0;

int largest_id=0;

int prior=0;

for(int i=0;i<num;i++){

if(largest<j[i].gettime()){

largest=j[i].gettime();

largest_id=j[i].getid();

if(a==1){

prior=priority[i];
}

cout<<largest_id<<endl;

if(a==1){

cout<<prior<<endl;

}
}

main(){

job_Sch j;

j.get();

j.find_Max();
a=1;

j.get();

j.find_Max();

}
QUESTION-3 How to serve tea?
#include<iostream>

using namespace std;

#include<string>

#include<map>

struct guest

int room_No;

string name;

int friend_Room_No;

public:

void get();

};

class hotel

int num_Of_Guest;

map<int,guest> stay_Det;

//this room number is with jegan

int first_Room_No;

public:

void get();

//this function should print


//details such as name and room number

// of guest to serve coffee

void serve_Coffee();

};

Your code has Passed Execution!

void guest::get(){

cin>>name;
cin>>friend_Room_No;

void hotel::get(){

cin>>num_Of_Guest;

for(int i=0;i<num_Of_Guest;i++){

guest g;

cin>>g.room_No;

g.get();

stay_Det[g.room_No]=g;

cin>>first_Room_No;

void hotel::serve_Coffee(){
guest g1;

for(int i=0;i<num_Of_Guest;i++){

g1=stay_Det[first_Room_No];

cout<<g1.name<<"\t"<<g1.room_No<<endl;

first_Room_No=g1.friend_Room_No;

main()

hotel h;

h.get();

h.serve_Coffee();

}
QUESTION-4 3D POINTS?
#include<iostream>

using namespace std;

class dim{

public:

int x;

int y;

int z;

void get(){

cin>>x;

cin>>y;

cin>>z;

void increment(int a){

x=x+a;

y=y+a;

z=z+a;

void increment(int a,int b){

x=x*(a+b);

y=y*(a+b);
z=z*(a+b);

void increment(int a,int b,int c){

x=x+a;

y=y+b;

z=z+c;

void print(){

cout<<x<<"\n"<<y<<"\n"<<z<<"\n";

};

main()

dim d1;

d1.get();

int inc;

cin>>inc;

int p1,p2;

cin>>p1>>p2;

int ix,iy,iz;

cin>>ix>>iy>>iz;
d1.increment(inc);

d1.print();

d1.increment(p1,p2);

d1.print();

d1.increment(ix,iy,iz);

d1.print();

}
QUESTION-5 Generic right shift
#include<iostream>

using namespace std;

#include<string>

template<class T>

//shift the set of 'n' elements ele by 'r' positions

void right_Shift(T *ele,int n,int r);

Your code has Passed Execution!

template<class T>

void right_Shift(T *ele,int n,int r){

T *ele1=new T[20];

for(int i=0;i<n;i++){

ele1[i]=ele[i];

r=n-r;

for(int i=0;i<n;i++){

ele[i]=ele1[(r+i)%n];

main()
{

int n, a[20],r;

int ch;

cin>>ch;

string s[10];

if(ch==0)

cin>>n;

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

cin>>a[i];

cin>>r;

right_Shift(a,n,r);

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

cout<<a[i]<<endl;

}
else

cin>>n;

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

cin>>s[i];

cin>>r;

right_Shift(s,n,r);
for(int i=0;i<n;i++)

cout<<s[i]<<endl;

}
QUESTION-6 Check if matrix is sparse?
void mismatchDimension::error_Msg() const{

cout<<"Dimension of matrices do not match"<<endl;

template<class T>

void matrix<T>::get(){

int i,j;

cin>>row>>col;

for(i=0;i<row;i++){

for(j=0;j<col;j++){

cin>>ele[i][j];

template<class T>

bool matrix<T>::check_Sparse(){

int i=0,j,c=0;

int t=row*col;

for(;i<row;i++){

for(j=0;j<col;j++){
if(ele[i][j]==0){

c++;

if(c>t/2)

return true;

else

return false;

template<class T>matrix<T> matrix<T>::add(matrix<T> &a)


{

int i,j;

if(row!=a.row || col!=a.col)

throw mismatchDimension();
else

for(i=0;i<row;i++){

for(j=0;j<col;j++){

a.ele[i][j]+=ele[i][j];

return a;
}

template<class T>

void matrix<T>::print(){

int i=0,j;

for(;i<row;i++){

for(j=0;j<col;j++){

cout<<ele[i][j]<<endl;

}
QUESTION-7 Dobly ended queue?
template<class T> queue<T>::queue(){

front=-1;

rear=-1;

capacity=20;

ele=new T[20];

template<class T>

bool queue<T>::isempty(){

if(front==-1)

return true;

else

return false;

template<class T>

bool queue<T>::isfull(){

if(rear==19)

return true;

else

return false;
}

template<class T>

bool queue<T>::enqueue(T data){

if(isfull())

return false;

else{

ele[++rear]=data;

if(!rear)

front++;

return true;

template<class T>
T queue<T>::dequeue(){

if(isempty()){

ERR_Flag=true;

return ele[0];

else{

int i=front;
if(front+1>rear){

rear=-1;

front=-1;

else{

front++;

return ele[i];

template<class T>

void queue<T>::print(){

if(!isempty()){

for(int i=front;i<=rear;i++){
cout<<ele[i]<<endl;

else

cout<<"Queue is empty"<<endl;

}
template<class T>

queue<T>::~queue(){}

template<class T>

bool deque<T>::push_Back(T data){

return queue<T>::enqueue(data);

template<class T>

bool deque<T>::push_Front(T data){

if(!queue<T>::isfull()){

if(queue<T>::isempty()){

queue<T>::enqueue(data);

else{
for(int i=queue<T>::rear+
+;i>=queue<T>::front;i--){

queue<T>::ele[i+1]=queue<T>::ele[i];

queue<T>::ele[queue<T>::front]=data;

return true;

}
return false;

template<class T> T deque<T>::pop_Front(){

return queue<T>::dequeue();

template<class T>

T deque<T>::pop_Back(){

if(!queue<T>::isempty()){

if((queue<T>::rear)-1>=(queue<T>::front))

return queue<T>::ele[queue<T>::rear--];

else

return queue<T>::ele[0];
}

You might also like