In an exercise, I need to compare a certain number of distances and say which one or which are the smallest and print them out. For this, I have the following function:
void Permuta(FILE *saida, Cidade *C, int *sequencia, int inicio, int termino, int totalViagens, float **distanciasAux)
{
int i, j;
totalViagens = TotalViagens(termino);
if(inicio == termino)
{
for(i = 0; i < termino; i++)
fprintf(saida, "%d\t", sequencia[i]+1);
fprintf(saida, "= %f\n", Distancia(C, termino, sequencia));
*distanciasAux = (float *) malloc(totalViagens*sizeof(float));
float *distancias = *distanciasAux;
distancias[i] = Distancia(C, termino, sequencia);
printf("\n%f\n", distancias[i]);
}
else
{
for(j = inicio; j < termino; j++)
{
Troca((sequencia+inicio), (sequencia+j));
Permuta(saida, C, sequencia, inicio+1, termino, totalViagens, distanciasAux);
Troca((sequencia+inicio), (sequencia+j));
}
}
}
In this function, I do the dynamic allocation as follows, as can be seen above
*distanciasAux = (float *) malloc(totalViagens*sizeof(float));
float *distancias = *distanciasAux;
distancias[i] = Distancia(C, termino, sequencia);
printf("\n%f\n", distancias[i]);
In printing these results, I have the correct distance values. However, to compare these values, I need to do in another function. This other function can be seen below:
void CriaSequencia(FILE *saida, Cidade *C, int *sequencia, int numeroCidade, int totalViagens)
{
int i;
totalViagens = TotalViagens(numeroCidade);
float *distancias;
for(i = 0; i < numeroCidade; i++)
{
sequencia[i] = i;
}
Permuta(saida, C, sequencia, 0, numeroCidade, TotalViagens(numeroCidade), &distancias);
for(i = 0; i < totalViagens; i++)
printf("\n%f\n", distancias[i]);
}
I did not start the comparison process because I wanted to make sure the past values are correct. However, when I ask to print values out of function, all I get are null values (0.000000).
I would like to know why the correct values are not printing and what might be causing this problem.