Ciao! Devo convertire la seguente funzione ricorsiva in una iterativa in C provando col seguente metodo. Il risultato è un array di 5 elementi (cl); la funzione iterativa mi dà gli stessi risultati di quella ricorsiva tranne per qualche valore dell'intero e (ad es. per e = 7). Qualcuno ha un'idea di dove sto sbagliando?
float ric_func(struct matrice* ch_d, struct matrice* ch_s, struct matrice* th, struct matrice* feat, struct matrice* val, int n, struct matrice* xt, struct matrice* cl, int ncl, struct matrice* count, int e, int el) {
cl->rig = 1; cl->col = ncl;
cl->N = (float*)malloc(sizeof(float) * cl->rig * cl->col);
if (th->N[n + count->M[e]] != -2) {
if (xt->N[feat->M[n + count->M[e]] + el * xt->col] < th->N[n + count->M[e]]) {
if (ch_s->M[n + count->M[e]] != -1) {
for (int j = 0; j < ncl; j++) {
cl->N[j] = val->N[ch_s->M[n + count->M[e]] * ncl + j];
}
ric_func(ch_d, ch_s, th, feat, val, ch_s->M[n + count->M[e]], xt, cl, ncl, count, e, el);
}
}
else {
if (ch_d->M[n + count->M[e]] != -1) {
for (int j = 0; j < ncl; j++) {
cl->N[j] = val->N[ch_d->M[n + count->M[e]] * ncl + j];
}
ric_func(ch_d, ch_s, th, feat, val, ch_d->M[n + count->M[e]], xt, cl, ncl, count, e, el);
}
}
}
else {
for (int j = 0; j < ncl; j++) {
cl->N[j] = val->N[(n + count->M[e]) * ncl + j];
}
}
return 0;
}
float* iter_func(int* ch_d, int* ch_s, float* th, int* feat, float* val, float* xt, float* cl, int ncl, int* count, int e, int el, int nb) {
int n = 0;
while (th[n]!= -2) {
if (xt[feat[n] + el * nb] < th[n]) {
if (ch_s[n] != -1) {
for (int j = 0; j < ncl; j++) {
cl[j] = val[ch_s[n] * ncl + j];
}
n = ch_s[n] + count[e];
}
}
else {
if (ch_d[n] != -1) {
for (int j = 0; j < ncl; j++) {
cl[j] = val[ch_d[n] * ncl + j];
}
n = ch_d[n] + count[e];
}
}
}
for (int j = 0; j < ncl; j++) {
cl[j] = val[n * ncl + j];
}
return cl;
}