Saturday 18 November 2017

C++ Program to Implement the Bin Packing Algorithm


Code:

#include  iostream

using namespace std;

void binPacking(int *a, int size, int n)
{
    int binCount = 1;
    int s = size;
    for (int i = 0; i < n; i++)
    {
        if (s - *(a + i) > 0)
        {
            s -= *(a + i);
            continue;
        }
        else
        {
            binCount++;
            s = size;
            i--;
        }
    }

    cout << "Number of bins required: " << binCount;
}

int main(int argc, char **argv)
{
    cout << "BIN - PACKING Algorithm\n";
    cout << "Enter the number of items in Set: ";
    int n;
    cin >> n;
    cout << "Enter " << n << " items:";
    int a[n];
    for (int i = 0; i < n; i++)
        cin >> a[i];
    cout << "Enter the bin size: ";
    int size;
    cin >> size;
    binPacking(a, size, n);
}


Output:

BIN - PACKING Algorithm
Enter the number of items in Set: 5
Enter 5 items:12 23 34 45 56
Enter the bin size: 70
Number of bins required: 3


More C++ programs:














100+ Best Home Decoration Ideas For Christmas Day 2019 To Make Home Beautiful

Best gifts for Christmas Day | Greeting cards for Christmas Day | Gift your children a new gift on Christmas day This Christmas d...