CSE 1002 PPS Bonus
Total time : 600 mins
Challenges : 5
Question 1
Given a point in three-dimensional space with x-coordinate, y-coordinate, z-coordinates, Provide a function increment with one, two and three parameters to transform the values of all the three coordinates. When the function with one parameter is called increment all the three , by same value . When the function with two arguments is called multiply each coordinates by the sum of the two parameters. when the function with three arguments is called, increment x-coordinate by the value of first parameter, y-coordinate by the value of the second parameter and z-coordinate by the value of the third parameter.
Input format:
Enter the coordinates of the point
X-coordinate
Y-coordinate
Z-coordinate
Enter the increment value for function with one parameter
Enter the values for the function with two parameters
Enter the increment values for function with three parameters
Output format:
Display the coordinates of the point after calling function with one parameter
Display the coordinates of the point after calling function with two parameters
Display the coordinates of the point after calling function with three parameters
Solution
#include< iostream > using namespace std; class dim { int x,y,z; public : void get() { cin>>x>>y>>z; } void increment(int val) { x+=val; y+=val; z+=val; } void increment(int val1,int val2) { x*=val1+val2; y*=val1+val2; z*=val1+val2; } void increment(int val1,int val2,int val3) { x+=val1; y+=val2; z+=val3; } 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();
}
Input
INPUT : x y and z coordiante
Output
OUTPUT : print() cout<<x<<y<<z;
Processing
increment() overloading
Pseudocode
BEGIN Read input increament() x+=val1 y+=val2 z+=val3 print() cout<<x<<y<<z; END
Question 2
Given a set of names, design an algorithm and write a C++ code to form a sorted list, in the descending order of names that do begin with a vowel.
Hint: Use STL for implementation
Input Format:
Number of names, ‘n’
Name 1
Name 2
…
Name n
Output Format:
Names that do begin with a vowel in a descending order.
Solution
#include
#include
#include
#include
#include
using namespace std;
int main() { int num; string name; vector < string > names; cin>>num; for(int i=0;i < num;i++) { cin>>name; names.push_back(name); } sort(names.begin(),names.end()); for(vector < string > :: iterator i=names.end();i!=names.begin();i--) { name=*(i-1); if(name[0]=='A' || name[0]=='E' || name[0]=='I' || name[0]=='O' || name[0]=='U') cout<<name<<"\n"; } return(0); }
Input
INPUT : cin>>name; names.push_back(name);
Output
OUTPUT ; cout<<*i;
Processing
if(*i[0]=='A''E''I''O''U')
Pseudocode
BEGIN Read input if(*i[0]=='A''E''I''O''U') cout<<*i<<"\n"; END
Question 3
Jobs are submitted to an operating system. Operating system does something called job scheduling when a number of jobs are submitted to an operating system at the same time. Some of the jobs have only job id and time required to complete the job. Whereas some other jobs are having priority in addition to job id and time required. Given a set of jobs without priority print the id of the job that requires minimum time to execute and given a set of jobs with priority, print the id and priority of the job which requires minimum time to execute. Design a OOP model to solve the problem.
Input Format
Number of jobs without priority
Id of job1
Time required by job1
…
Id of job-n
Time required by job-n
Number of jobs with priority
Id of job1
Time required by job1
Priority of job1
…
Id of job-n
Time required by job-n
Priority of job-n
Output Format
Id of job with out priority that requires minimum time
Id of job with priority that requires minimum time and its priority.
Solution
#include
#include
#include
using namespace std;
class job
{
int id;
float time;
public:
void get();
//function to return time
float gettime();
int getid();
};
class job_Sch
{
int num;
job j[20];
public:
//function to print job with maximum time
void find_Max();
void get();
};
bool pri=false; int p[20]; void job :: get() { cin>>id>>time; } float job :: gettime() { return(time); } int job :: getid() { return(id); } void job_Sch :: get() { cin>>num; for(int i=0;i < num;i++) { j[i].get(); if(pri) cin>>p[i]; } } void job_Sch :: find_Max() { int max=-1,pos; for(int i=0;i < num;i++) if(max < j[i].gettime()) { pos=i; max=j[i].gettime(); } cout<<j[pos].getid()<<"\n"; if(pri) cout<<p[pos]<<"\n"; } int main() { job_Sch j; j.get(); j.find_Max(); pri=true; j.get(); j.find_Max(); return(0); }
Input
INPUT : get() cin>>id>>time;
Output
OUTPUT : find_Max() cout<<p[pos];
Processing
for(int i=0;i < num;i++) if(max < j[i].gettime()) { pos=i; max=j[i].gettime(); }
Pseudocode
BEGIN Read input for(int i=0;i < num;i++) if(max < j[i].gettime()) { pos=i; max=j[i].gettime(); } cout<<j[pos].getid()<<"\n"; if(pri) cout<<p[pos]<<"\n"; END
Question 4
Jegan organizes a family function and invite some of his friends. Many of his friends come prior to the function. Jegan has arranged for their accommodation in a hotel. The rooms are booked as the guest arrive, so the room numbers are not continous and not in any order. Jegan has the room number of the friend who had arrived first. And he has requested his friends to have a room number of the person who arrives next to him. That is the friend who arrived third will have room number of the friend who arrived fourth. The last guest will not have any room number. Given the details of each guest such as name, his room number and room number of the guest who arrived next, and room number of that Jegan has, design an algorithm and write a C++ code to print the names and room numbers to serve a coffee. For example, if the details in the following table is given
Room Number of Guest
Name of Guest
Room number the guest who arrived next
125
John
210
157
Kannan
125
210
Kumar
-1
315
Anand
157
If Jegan has room number as 315, room number of the friend who arrived first, then the output should be
Anand 315
Kannan 157
John 125
Kumar 210
Hint: Map in STL can be used for representing the input, room number of the guest may be the key and other two details may be stored as value by representing them as a user defined data type.
Input Format
Number of friends
Room number of guest1
Name of guest1
Room number the guest who arrived next to guest1
Room number of guest2
Name of guest2
Room number the guest who arrived next to guest2
….
Room number of guestn
Name of guestn
-1 ( Last guest doesn’t posses any room number)
Room number that Jegan has
Output Format
Name of guest1 and his Room number separated by tab
Name of guest2 and his Room number separated by tab
…
Name of guestn and his Room number separated by tab
Solution
#include
using namespace std;
#include
#include
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();
};
void guest :: get() { cin>>name>>friend_Room_No; } void hotel :: get() { int val; guest g; cin>>num_Of_Guest; for(int i=0;i < num_Of_Guest;i++) { cin>>val; g.get(); stay_Det.insert(pair < int,guest > (val,g)); } cin>>first_Room_No; } void hotel :: serve_Coffee() { int rm=first_Room_No; while(rm!=-1) { cout<<stay_Det[rm].name<<" "<<rm<<"\n"; rm=stay_Det[rm].friend_Room_No; } }
main()
{
hotel h;
h.get();
h.serve_Coffee();
}
Input
INPUT : cin>>name>>friend_room_no;
Output
OUTPUT : cout<<stay_Det[rm].name;
Processing
while(rm!=-1) { cout<<stay_Det[rm].name<<" "<<rm<<"\n"; rm=stay_Det[rm].friend_Room_No; }
Pseudocode
BEGIN Read input while(rm!=-1) { cout<<stay_Det[rm].name<<" "<<rm<<"\n"; rm=stay_Det[rm].friend_Room_No; } END
Question 5
Rahul is fond of travelling and he visits cities and towns in the country whenever possible. All cities in the country are not connected to each other. Given the details of the cities that are connected to each city, source city from where he begins the travel and the destination city of the travel, design an algorithm and write a C++ code to list down the cities in the travel. Rahul must not visit a city more than once. When the destination city name is in the connected cities of the current city, chose it and complete the route. When the destination city name is not in the list of connected cities to current city and there are more than one city from the current city, he sorts the city names and include the first minimum city name that is not yet visited. For example, if the connection between the cities are given as follows, source city as A and destination city as F the list of cities in the travel are A, B, D and F.
City
Connected Cities
A
E, B
B
D
C
E,D
D
F
E
F
Use vectors, maps and algorithms such as sort, find in STL for implementation. Assume that the connection of cities is very simple and there is a path as described in the problem.
Input Format
Number of cities that are connected to other cities, ‘n’
Name of city1
Number of neighbouring cities for city1, ‘m’
First neighbouring city of city1
Second neighbouring city of city1
…
mth neighbouring city of city1
…
Name of cityn
Number of neighbouring cities for cityn, ‘m’
First neighbouring city of cityn
Second neighbouring city of cityn
…
mth neighbouring city of cityn
Output Format
Name of cities in the list
Solution
#include
using namespace std;
#include
#include
#include
#include
class travel
{
int num_Of_Cities;
map<string,vector > city_Connection;
string source;
string destn;
vector route;
public:
//In this function form the map that depicts the
// the connection of cities,key is the name of the
// city and value is a vector of cities that are
// connected to the city
void get();
void find_Route();
void print_Route();
};
void travel :: get() { int num; string val,ct; cin>>num_Of_Cities; for(int i=0;i < num_Of_Cities;i++) { vector < string > city; cin>>val>>num; for(int j=0;j < num;j++) { cin>>ct; city.push_back(ct); } sort(city.begin(),city.end()); city_Connection.insert(pair < string,vector < string > > (val,city)); } cin>>source>>destn; } bool look(vector < string > route,string str) { for(vector < string > :: iterator i=route.begin();i!=route.end();i++) if(str==*i) return(true); return(false); } void travel :: find_Route() { route.push_back(source); while(source!=destn) { string s=source; vector < string > :: iterator i=city_Connection[s].begin(); if(look(city_Connection[s],destn)) { route.push_back(destn); break; } while(look(route,*i)) i++; source=*i; route.push_back(source); } } void travel :: print_Route() { for(vector < string > :: iterator i=route.begin();i!=route.end();i++) cout<<*i<<"\n"; }
int main()
{
travel t;
t.get();
t.find_Route();
t.print_Route();
}
Input
INPUT : cin>>n; city.push_back(n);
Output
OUTPUT : route.print()
Processing
while(source!=destn) { vector < string > :: iterator i=city_Connection.begin(); if(look(city_Connection,destn)) { route.push_back(destn); break; } while(look(route,*i)) i++; source=*i; route.push_back(source); }
Pseudocode
BEGIN Read input while(source!=destn) { vector < string > :: iterator i=city_Connection.begin(); if(look(city_Connection,destn)) { route.push_back(destn); break; } while(look(route,*i)) i++; source=*i; route.push_back(source); } route.print(); END
Very nice coding
Really great
LikeLike
Very nice
LikeLike
Thanks
LikeLike
Thank you so much for all the codes throughout the semester bro
LikeLike
Thanks for helping out throghout semester!!
LikeLike