I wrote a recursive program in C.It works. Now I modifed it for C++ on Unix. I have been trying for ages.
I just can't get it to work. The problem is in the Print function.
Is there a mistake in the way Print is called recursively?
Strategy* Print(const int resalloc[],int nusers,Strategy *head)
{
int i;
Strategy *curr;
if(head == NULL)
{
head = (Strategy*)malloc(sizeof(Strategy));
head->next = NULL;
for(i = 0; i < nusers; i++)
{
head->com = resalloc;
}
printf("head == null\n");
}
else
{
/**************the next stmt is giving me trouble*********/
head->next = Print(resalloc,nusers,head->next);
}
printf("\n");
return(head);
}
Strategy *RecNum(int puser[],int nusers,int resalloc[],int icurr,Strategy *head)
{
int i,j;
int last, temp;
temp = icurr + 1;
if(temp == nusers)
last = 1;
else
last = 0;
printf("nusers %d icurr %d \n",nusers,icurr);
for(i = 0; i <= puser[icurr]; i++)
{
printf("puser[icurr] %d\n",puser[icurr]);
resalloc[icurr] = i;
if(last)
{
printf("in\n");
head = Print(resalloc,nusers,head);
}
else
{
head = RecNum(puser,nusers,resalloc,icurr+1,head);
}
}
return(head);
}
void generateStrategy(ResourceCenter *&r, Crisis *&c, int &cri_num, int &res_num)
{
Strategy *set;
Strategy *current;
int i;
int num;
int *p, *res;
int count = 0;
p = (int*)malloc(sizeof(int)*res_num);
res = (int*)malloc(sizeof(int)*res_num);
for(int i = 0; i < res_num; i++)
{
res = r.res_left;
}
for(int j = 0; j < cri_num; j++)
{
set = RecNum(res,res_num,p,0,current);
c[j].first = set;
}
}
rsshetty.
It's always in the details.
I just can't get it to work. The problem is in the Print function.
Is there a mistake in the way Print is called recursively?
Strategy* Print(const int resalloc[],int nusers,Strategy *head)
{
int i;
Strategy *curr;
if(head == NULL)
{
head = (Strategy*)malloc(sizeof(Strategy));
head->next = NULL;
for(i = 0; i < nusers; i++)
{
head->com = resalloc;
}
printf("head == null\n");
}
else
{
/**************the next stmt is giving me trouble*********/
head->next = Print(resalloc,nusers,head->next);
}
printf("\n");
return(head);
}
Strategy *RecNum(int puser[],int nusers,int resalloc[],int icurr,Strategy *head)
{
int i,j;
int last, temp;
temp = icurr + 1;
if(temp == nusers)
last = 1;
else
last = 0;
printf("nusers %d icurr %d \n",nusers,icurr);
for(i = 0; i <= puser[icurr]; i++)
{
printf("puser[icurr] %d\n",puser[icurr]);
resalloc[icurr] = i;
if(last)
{
printf("in\n");
head = Print(resalloc,nusers,head);
}
else
{
head = RecNum(puser,nusers,resalloc,icurr+1,head);
}
}
return(head);
}
void generateStrategy(ResourceCenter *&r, Crisis *&c, int &cri_num, int &res_num)
{
Strategy *set;
Strategy *current;
int i;
int num;
int *p, *res;
int count = 0;
p = (int*)malloc(sizeof(int)*res_num);
res = (int*)malloc(sizeof(int)*res_num);
for(int i = 0; i < res_num; i++)
{
res = r.res_left;
}
for(int j = 0; j < cri_num; j++)
{
set = RecNum(res,res_num,p,0,current);
c[j].first = set;
}
}
rsshetty.
It's always in the details.