Question
IN C++ please!!! Bin Packing problem description: Given an array of numbers, goal is to group them into minimum possible # of bins, but each
IN C++ please!!!
Bin Packing problem description: Given an array of numbers, goal is to group them into minimum possible # of bins, but each bins items should not go above fixed bin capacity of 100. Let us assume that array contents are already in sorted order.
However, in this lab, we are ONLY validating a possible solution - so, we need to get an array of numbers in one line, another set of numbers in the next line to get the bin assignments for each number & check whether it is a valid solution (we are NOT checking whether it is the best solution though - that is lot harder - come to office hours if you want to discuss about it!). Here are a few sample inputs & outputs:
2 5 11 32 45 78 95 99 1 2 3 3 3 2 1 0 Valid
First line has the input numbers, Second line has the bin assignments. 2 goes to bin 1, 5 goes to bin 2, 11 goes to bin 3, So this solution uses 4 bins with the following totals in each bin: 99, 97, 83, 88. So the solution is valid!
2 5 11 32 45 78 95 99 0 0 0 0 0 1 2 4 Bin 3 not used
Bin index 4 is used & bin index 3 has been skipped - that is not valid.
2 5 11 32 45 78 95 99 0 0 0 0 0 1 1 2 Bin 1 total goes above 100
Total of 78 and 95 goes above 100. So, it is invalid.
2 5 11 32 45 78 95 99 0 0 11 0 0 1 2 3 Bin 11 out of range
Bin 11 is out of range, since there are only 8 items in the input! There is absolutely no need to use more than 8 bins.
2 5 32 45 78 95 99 111 0 0 0 0 0 1 2 3 Input item 111 out of range
Here is the code! Please help me with full detail in the validate() function and I will be much grateful! I'll definitely learn from this! Thank you!
#include
#include
#include
using namespace std;
//let us assume we won't go over 100 numbers
const int MAXITEMS = 100;
int items[MAXITEMS], binAssignments[MAXITEMS];
int numItems=0; //stores actual # of items
const int BINCAPACITY = 100;
void validate() {
// WRITE YOUR CODE HERE
}
int main() {
string line;
int numAssignments = 0;
//get all the items first.
getline(cin, line); //get one line of input
istringstream istr(line); //convert it to inputstringstream
//extract the numbers from the stream into the array
while (istr >> items[numItems])
numItems++;
//get bin assignments now.
getline(cin, line);
istringstream istr2(line);
while (istr2 >> binAssignments[numAssignments])
numAssignments++;
if (numItems != numAssignments) {
cout << "# of items and # of bin assignments mismatch.";
exit(1);
}
validate();
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started