0
#include<stdio.h>
int max(int a,int b)
{
    if(a>b)
    return a;
    return b;
}
void knacksnap(int n,int a[][n+1],int* val,int* weight,int maxweight)
{
    if(n==0 || maxweight==0)
    {
        a[maxweight][n]=0;
        return;
    }
    if(a[maxweight][n]!=-1)
    return;
    if(weight[n-1]>maxweight)
    {
        knacksnap(n-1,a,val,weight,maxweight);
        a[maxweight][n]=a[maxweight][n-1];
    }
    else
    {
        knacksnap(n-1,a,val,weight,maxweight-weight[n-1]);
        knacksnap(n-1,a,val,weight,maxweight);
        a[maxweight][n]=max(val[n-1]+a[maxweight-weight[n-1]][n-1],a[maxweight][n-1]);
    }
}
int main()
{
    int n;
    scanf("%d",&n);
    int val[n],weight[n];
    int i;
    for(i=0;i<n;i++)
    scanf("%d",&val[i]);
    for(i=0;i<n;i++)
    scanf("%d",&weight[i]);
    int maxweight;
    scanf("%d",&maxweight);
    int a[maxweight+1][n+1];
    int j;
    for(i=0;i<maxweight+1;i++)
    {
        for(j=0;j<n+1;j++)
        {
            a[i][j]=-1;
        }
    }
    knacksnap(n,a,val,weight,maxweight);
    printf("\n");
    printf("%d",a[maxweight][n]);
}

It not taking updated values by the knacksnap function. for example the inner knacksnap funtions are not generating correct values. eventhough their values are updated its not taking them. can someone please help

surya
  • 11
  • 1
    [How to debug small programs](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/) – Filburt Jul 07 '21 at 10:15
  • Have you tried running your code line by line in a debugger while monitoring the values of all variables, in order to determine at which point your program stops behaving as intended? If you did not try this, then you probably want to read this: [What is a debugger and how can it help me diagnose problems?](https://stackoverflow.com/q/25385173/12149471) – Andreas Wenzel Jul 07 '21 at 10:17

0 Answers0