In this program, we are going to share a c program to implement the bin packing algorithm. If you are a beginner and want to start learning the C programming, then keep your close attention in this tutorial as I am going to share a c program to implement the Bin Packing Algorithm with the output.
We have designed this program for beginners for learning purpose. Copy below c program and execute it with c compiler to see the output of the program.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | #include<stdio.h> void binPacking(int *a, int size, int n) { int binCount = 1, i; int s = size; for (i = 0; i < n; i++) { if (s - *(a + i) > 0) { s -= *(a + i); continue; } else { binCount++; s = size; i--; } } printf("Number of bins required: %d", binCount); } int main(int argc, char **argv) { printf("Enter the number of items in Set: "); int n; int a[n], i; int size; scanf("%d", &n); printf("Enter %d items:", n); for (i = 0; i < n; i++) scanf("%d", &a[i]); printf("Enter the bin size: "); scanf("%d", &size); binPacking(a, size, n); return 0; } |
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
If you like FreeWebMentor and you would like to contribute, you can write an article and mail your article to [email protected] Your article will appear on the FreeWebMentor main page and help other developers.