The below code behaves as if I have initialized variable res to 0, if I uncomment the lines above the first time I used it.
int t, mi,cur,n,res;
scanf("%d",&t);
while(t-- && scanf("%d",&n) == 1)
{
for (int i = 1; i <= n; ++i)
{
scanf("%d",&h[i]);
}
for (int i = 1; i <= n; ++i)
{
scanf("%d",&k[i]);
}
for (int i = 0; i < 1001; ++i)
dp[0][i] = INF;
for (int i = 0; i < 501; ++i)
dp[i][0] = 0;
for (int i = 1; i <= n; ++i)
{
for (int j = 1; j <= 2*n; ++j)
{
dp[i][j] = dp[i-1][j];
if(j >= k[i-1])
dp[i][j] = min(dp[i-1][j],1 + dp[i][j-k[i-1]]);
}
}
// The lines below.
//for (int i = 0; i <= n; ++i)
//{
// for (int j = 0; j <= 2*n; ++j)
// {
// printf("%d ", dp[i][j]);
// }
// printf("\n");
//}
for (int i = 1; i <= n; ++i)
{
res += dp[n][2*h[i]];
}
printf("%d\n", res);
When I print the value of res with the lines commented, it gives me garbage value, but when I uncomment it, it gives me the expected value. I don't know the advanced concepts of the language. Why this happens?