This is a C Program to implement First Fit Decreasing for one dimensional objects and M bins. In simple terms this is bin packing algorithm for first fit technique.

Here is source code of the C Program to Implement First Fit Decreasing for 1-D Objects and M Bins. The C program is successfully compiled and run on a Linux system. The program output is also shown below.

`#include <string.h>`

`#include <stdio.h>`

`#include <stdlib.h>`

void binPacking(int *a, int size, int n) {

int binCount = 0, i, j;

int binValues[n];

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

binValues[i] = size;

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

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

if (binValues[j] - a[i] >= 0) {

binValues[j] -= a[i];

break;

`}`

`}`

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

if (binValues[i] != size)

`binCount++;`

printf(

"Number of bins required using first fit decreasing algorithm is: %d",

binCount);

`}`

int* sort(int *sequence, int n) {

`// Bubble Sort descending order`

int i, j;

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

for (j = 0; j < n - 1; j++)

if (sequence[j] < sequence[j + 1]) {

sequence[j] = sequence[j] + sequence[j + 1];

sequence[j + 1] = sequence[j] - sequence[j + 1];

sequence[j] = sequence[j] - sequence[j + 1];

`}`

return sequence;

`}`

int main(int argc, char **argv) {

printf("BIN - PACKING Algorithm 1D Objects(First Fit Decreasing)");

printf("Enter the number of items in Set: ");

int n, i;

scanf("%d", &n);

printf("Enter %d items: ", n);

int a[n];

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

scanf("%d", &a[i]);

printf("Enter the bin size: ");

int size;

scanf("%d", &size);

int *sequence = sort(a, n);

binPacking(sequence, size, n);

return 0;

`}`

Output:

$ gcc BinPacking.c $ ./a.out BIN - PACKING Algorithm 1D Objects(First Fit Decreasing) Enter the number of items in Set: 5 Enter 5 items: 12 23 34 45 56 Enter the bin size: 50 Number of bins required using first fit decreasing algorithm is: 3

**Sanfoundry Global Education & Learning Series – 1000 C Programs.**

Here’s the list of Best Reference Books in C Programming, Data Structures and Algorithms.