So i'm trying to make a recursive program for a class im taking,but so far i can't seem to get it to run i've tried for 2 days now and just can't seem to get around the error's i'm getting. If you guy's and gal's could please take a look and see if you could help it would be greatly appreciated.Thank You
Code:
#define n 5
enum Estados {Ninguno, Incluido, Excluido}
struct elemento
{
Estados estado;
};
typedef elemento TipoDato;
int Pesos[n],OabjetivoAalcanzar;
void Inicializa()
{
Pesos[0]=7; Pesos=[1]=5; Pesos[2]=4; Pesos[3]=4; Pesos[4]=1;
OabjetivoAalcanzar=10;
}
Void MochilaExacta(int OabjetivoAalcanzar,int i,int *Solucion)
{
int Encontrado,Posibilidad;
/*Finishes with success if siObjetivo=0 or if sio Objetivo<0 or i=n */
if((OabjetivoAalcanzar<=0)||(i==n))
if(OabjetivoAalcanzar==0)
*Solucion=1;
else;/*dont do anything because Objetivo<0 or i=n */
else
{
Posibilidad=0;
Encontrado=0;
do
{
Posibilidad++;
switch(Posibilidad)
{
case 1:/*Inclusion*/
MochilaExacta(OabjetivoAalcanzar-Pesos[i],i+1,&Encontrado);
if(Encontrado)
printf("%d",Pesos[i]); /*Its been included and has a solution*/
break;
case 2:/*Exclusion*/
MochilaExacta(OabjetivoAalcanzar,i+1,&Encontrado);
/*Even if Encontrado is true,there is nothing left to do*/
}
}
while(!((Posibilidad==2)||Encontrado));
*Solucion=Encontrado;
}
}
void MochilaExacta_N_R(int OabjetivoAalcanzar)
{
int i,Solucion;
Pila *P;
lemento e,el;
i=0;
Solucion=0;
e.estado=Ninguno;
VaciaP(&P);
AnadeP(&P,e);/*Assigns the initial value to the pile P considering Pesos[0]*/
while(!EsVaciaP(P))
{
e=PrimeroP(P);
BorrarP(&P);
if(Solucion)/*If you already have a solution then write it*/
{
if(e.estado==Incluido)
printf("%d\n",Pesos[i]);/*Only written if something is included*/
i--;
}
else
if(((OabjetivoAalcanzar<=0)&&(e.estado==Ninguno))||(i==n))
{
if(OabjetivoAalcanzar==0)
Solucion=1;/*A solution is always obtainable*/
i--;/*the i hasnt been tested,You go back on the recursive*/
}
else/*There is no decision consider the state of the candidate*/
switch(e.estado)
{
case Ninguno: /*1st it includes an advance in the recursive*/
OabjetivoAalcanzar-=Pesos[i];
i++;
el.estado=Incluido;
AnadeP(&P,el);
el.estado=Ninguno;
AnadeP(&P,el);
break;
case Incluido: /*Now the advance is excluded*/
/*i-- is backed up;on option else of what*/
OabjetivoAalcanzar+=Pesos[i];
/*since it was removed it has to be added*/
i++;
el.estado=Excluido;
AnadeP(&P,el);
el.estado=Ninguno;AnadeP(&P,el);
break;
case Excluido:
/*the current election didnt give any results ending recursivity*/
i--;
}
}
}
void main (void)
{
int Solucion;
Inicializa();
MochilaExacta(OabjetivoAalcanzar,0,&Solucion);
if(Solucion)
printf("Solution given for Object=%d\n",OabjetivoAalcanzar);
Solucion=0;
OabjetivoAalcanzar=10;
MochilaExacta_N_R(OabjetivoAalcanzar);
if(Solucion)
printf("A Solution was found");
}