Bonus Practice Sheet Winter 2018 [17-Mar-2018 to 7-Apr-2018]
Program ID- 1501
There are ‘n’ bags in a corner of a city and they have to be moved to the centre of the city by a solar vehicle. The vehicle starts shunting from morning, the vehicle can carry more load in the mornings than in the evening when sunlight would be dimmer. Given the details of items in the bag, sort the bag in descending so that the vehicle can safely carry it to the centre of the city. Use vector and map in STL. Also use the sort algorithm defined in STL.
Hint: Use total weight of bag as key and object of bag as value, insert total weight of each bag into a vector. Use STL sort algorithm to sort it and print the details of bag in order
Input Format
Number of bags ‘n’
Name of bag1
Number of types of items in bag1, ‘t’
Weight of item1 in bag1
Number of item1 in bag1
Weight of item2 in bag1
Number of item2 in bag1
…
Weight of itemt in bag1
Number of itemt in bag1
…
….
….
Name of bagn
Number of types of items in bagn, ‘t’
Weight of item1 in bagn
Number of item1 in bagn
Weight of item2 in bagn
Number of item2 in bagn
…
Weight of itemt in bagn
Number of itemt in bagn
Output Format
Print name of bags in sorted order.
Sorting must be done in descending order based on the weight of the bag
Please Comment Working if the code worked to you
If you have other working codes please comment the codes enclosing with <pre> and </pre>
Example: <pre> Your Code </pre>
Hint: Use total weight of bag as key and object of bag as value, insert total weight of each bag into a vector. Use STL sort algorithm to sort it and print the details of bag in order
Input Format
Number of bags ‘n’
Name of bag1
Number of types of items in bag1, ‘t’
Weight of item1 in bag1
Number of item1 in bag1
Weight of item2 in bag1
Number of item2 in bag1
…
Weight of itemt in bag1
Number of itemt in bag1
…
….
….
Name of bagn
Number of types of items in bagn, ‘t’
Weight of item1 in bagn
Number of item1 in bagn
Weight of item2 in bagn
Number of item2 in bagn
…
Weight of itemt in bagn
Number of itemt in bagn
Output Format
Print name of bags in sorted order.
Sorting must be done in descending order based on the weight of the bag
Please Comment Working if the code worked to you
If you have other working codes please comment the codes enclosing with <pre> and </pre>
Example: <pre> Your Code </pre>
Code: C++
void bag :: get()
{
cin>>name>>num_Of_Items;
for(int i=0;i < num_Of_Items;i++)
cin>>item_Wt[i]>>item_Count[i];
}
void bag :: print()
{
cout<<name<<"\n";
}
float bag :: compute()
{
float total=0;
for(int i=0;i < num_Of_Items;i++)
total+=item_Count[i]*item_Wt[i];
return(total);
}
void solar :: get()
{
bag temp_bag;
cin>>num_Bags;
for(int i=0;i < num_Bags;i++)
{
temp_bag.get();
m1.insert(pair < float,bag > (temp_bag.compute(),temp_bag));
v.push_back(temp_bag.compute());
}
}
void solar :: sort_Vec()
{
sort(v.begin(),v.end());
reverse(v.begin(),v.end());
}
void solar :: print_In_Order()
{
for(vector < float > :: iterator i=v.begin();i!=v.end();i++)
m1[*i].print();
}
{
cin>>name>>num_Of_Items;
for(int i=0;i < num_Of_Items;i++)
cin>>item_Wt[i]>>item_Count[i];
}
void bag :: print()
{
cout<<name<<"\n";
}
float bag :: compute()
{
float total=0;
for(int i=0;i < num_Of_Items;i++)
total+=item_Count[i]*item_Wt[i];
return(total);
}
void solar :: get()
{
bag temp_bag;
cin>>num_Bags;
for(int i=0;i < num_Bags;i++)
{
temp_bag.get();
m1.insert(pair < float,bag > (temp_bag.compute(),temp_bag));
v.push_back(temp_bag.compute());
}
}
void solar :: sort_Vec()
{
sort(v.begin(),v.end());
reverse(v.begin(),v.end());
}
void solar :: print_In_Order()
{
for(vector < float > :: iterator i=v.begin();i!=v.end();i++)
m1[*i].print();
}
NOTE: The Above Codes are for reference only. It doesn't mean everyone to directly copy/paste those codes.
nice article for beginners.thank you.
ReplyDeleteC++ tutorial
java tutorial