Help please Memoized rod cutting in C while showing the length it is cut
-
r[1000]; *r = (int *) malloc ((n+ 1) * sizeof(int)); but if I use int *r then also it doesn't passes properly .Should I use structure? The problem should take input like division-3 input of price-1,5,8 and give rod cutting whereas there are three iteration which should be one r=0; and the maximum price for this division should be 8 but the result comes 1677218
#include
#include
#include
#include
int sum=0;int max(int a, int b)
{
return(a>b)?a:b;
}
int cut_rod_aux(int price[], int n, int r[])
{ int i;
if(r[n]>=0)
{
return r[n];
printf(" %d\n",r[n]);
}
if (n==0)
{
sum=0;} else { sum =100000; for( i=1; i
-
r[1000]; *r = (int *) malloc ((n+ 1) * sizeof(int)); but if I use int *r then also it doesn't passes properly .Should I use structure? The problem should take input like division-3 input of price-1,5,8 and give rod cutting whereas there are three iteration which should be one r=0; and the maximum price for this division should be 8 but the result comes 1677218
#include
#include
#include
#include
int sum=0;int max(int a, int b)
{
return(a>b)?a:b;
}
int cut_rod_aux(int price[], int n, int r[])
{ int i;
if(r[n]>=0)
{
return r[n];
printf(" %d\n",r[n]);
}
if (n==0)
{
sum=0;} else { sum =100000; for( i=1; i
I am assuming your cut and paste failed because as that appears to me that would not compile the r line has no type Anyhow your malloc is correct the problem is with r 1.) You can't and don't need the array specifier. 2.) You definitely don't dereference the malloc to it ... "*r" means dereference Anyhow any quick check of creating a dynamic array would give you the correct syntax
int* r;
r = (int *) malloc ((n+ 1) * sizeof(int));where x is the index position in the array. It's your responsibility to release the memory when you are donefree(r);
In vino veritas
-
r[1000]; *r = (int *) malloc ((n+ 1) * sizeof(int)); but if I use int *r then also it doesn't passes properly .Should I use structure? The problem should take input like division-3 input of price-1,5,8 and give rod cutting whereas there are three iteration which should be one r=0; and the maximum price for this division should be 8 but the result comes 1677218
#include
#include
#include
#include
int sum=0;int max(int a, int b)
{
return(a>b)?a:b;
}
int cut_rod_aux(int price[], int n, int r[])
{ int i;
if(r[n]>=0)
{
return r[n];
printf(" %d\n",r[n]);
}
if (n==0)
{
sum=0;} else { sum =100000; for( i=1; i
-
I am assuming your cut and paste failed because as that appears to me that would not compile the r line has no type Anyhow your malloc is correct the problem is with r 1.) You can't and don't need the array specifier. 2.) You definitely don't dereference the malloc to it ... "*r" means dereference Anyhow any quick check of creating a dynamic array would give you the correct syntax
int* r;
r = (int *) malloc ((n+ 1) * sizeof(int));where x is the index position in the array. It's your responsibility to release the memory when you are donefree(r);
In vino veritas