# Help me figure out why zero is always coming out?

• It was based on the algorithm to deal with the backpack from Sejwick's book, but the program works wrong. Somehow, zero always comes back. In his book, the code is very poor: the appointments of variables are said to be far from the beginning, not even the variables, but the introduction of such numbers as M. The code is very unpleasant, and it's hard to get into a pretty procedure. Help me at least figure out why zero is always coming out.

``````#include <iostream>
#include <cstring>
#include <ctime>
typedef struct{
int size;
int val;
} Item;
const int n = 10;   // Число предметов
const int m = 50;   // Емкость рюкзака
const int size = 50;
Item items[n];
int max_known[size];
Item item_known[size];
int knap(int cap){
int space;
int max;
int maxi;
int t;
for(int i = 0; i &lt; size; i++)
std::cout &lt;&lt; max_known[i] &lt;&lt; " ";
std::cout &lt;&lt; std::endl;

if(max_known[cap] != -1) return max_known[cap];
for(int i = 0, max = 0; i &lt; n; i++)
if((space = cap - items[i].size) &gt;= 0)
if((t = knap(space) + items[i].val) &gt; max){
max = t;
maxi = i;
}
max_known[cap] = max;
item_known[cap] = items[maxi];
return max;

}
int main(){
std::memset(max_known, -1, size * sizeof(int));
// Создаем рандомные вещи
std::srand(time(NULL));
for(int i = 0; i &lt; n; i++){
Item it;
it.size = 1 + std::rand() % 15;
it.val = 1 + std::rand() % 701;
items[i] = it;
}

std::cout &lt;&lt; knap(m) &lt;&lt; std::endl;

int pause;
std::cin &gt;&gt; pause;

}
``````

• You're coming from the mass element. `max_known`which, in general, is not, and it's definitely not--1.

To get rid of the masses and `memset` Use it. `std::vector<int>`♪ Well, it's cool not to initiate something under the conditions of:

2

2

2

2

2

2

2

2

2

2

2

2

2

2

2