# Knapsack Problem

• 11-12-2006
Fork
Knapsack Problem
I want to work out the max value of items that can be placed in a bag under a weight constraint C. I have the values and weights of the items stored in two arrays and i want to make another array which will hold 0 or 1 corresponding to whether the item is included or not (call it A[n]). How can I get A[n] to increase like a binary counter so that it checks every possible combination of items??
• 11-12-2006
Salem
I'm sure putting "knapsack problem" into a search engine will tell you all you need to know.
• 11-12-2006
Fork
Believe me I've tried, just can't seem to get my head around this problem for some reason
• 11-12-2006
manutd
```#include <stdio.h> void zeroarr (int * array, size_t size) {   int i;   for (i = 0; i < size; ++i) {       array[i] = 0;   }   return; } void printarr (int * array, size_t size) {   int i;   for (i = 0; i < size; ++i) {       printf ("%d", array[i]);   }   printf ("\n");   return; } // Return 1 if overflow // This is what you want. int incrarr (int * arr, size_t size) {   int i;   for (i = 0; i < size; ++i) {       if (arr[i] == 1) {         // 1 encountered.  Set to 0 and increment next value         arr[i] = 0;       }       else {         // 0 encountered.  Set to 1, and stop incrementing         arr[i] = 1;         break;       }   }   // Detect overflow   return i == size; } int main (void) {   int a[8];   zeroarr(a, 8);     for (;;) {       printarr(a, 8);       if (incrarr(a, 8))         break;   } }```