Calculate all distances

Asked

Viewed 70 times

0

In an exercise, I need to calculate all distances between coordinate points (x,y) which are read from a file, the last point of which coincides with the first. Each point represents a city that is represented by the following structure:

typedef struct
{
    int x;
    int y;
} Cidade;

An example input file can be seen below:

24
(1,10)  (4,4)   (5,1)   (2,0)   (1,10)
(1,10)  (4,4)   (2,0)   (5,1)   (1,10)
(1,10)  (5,1)   (4,4)   (2,0)   (1,10)
(1,10)  (5,1)   (2,0)   (4,4)   (1,10)
(1,10)  (2,0)   (5,1)   (4,4)   (1,10)
(1,10)  (2,0)   (4,4)   (5,1)   (1,10)
(4,4)   (1,10)  (5,1)   (2,0)   (4,4)
(4,4)   (1,10)  (2,0)   (5,1)   (4,4)
(4,4)   (5,1)   (1,10)  (2,0)   (4,4)
(4,4)   (5,1)   (2,0)   (1,10)  (4,4)
(4,4)   (2,0)   (5,1)   (1,10)  (4,4)
(4,4)   (2,0)   (1,10)  (5,1)   (4,4)
(5,1)   (4,4)   (1,10)  (2,0)   (5,1)
(5,1)   (4,4)   (2,0)   (1,10)  (5,1)
(5,1)   (1,10)  (4,4)   (2,0)   (5,1)
(5,1)   (1,10)  (2,0)   (4,4)   (5,1)
(5,1)   (2,0)   (1,10)  (4,4)   (5,1)
(5,1)   (2,0)   (4,4)   (1,10)  (5,1)
(2,0)   (4,4)   (5,1)   (1,10)  (2,0)
(2,0)   (4,4)   (1,10)  (5,1)   (2,0)
(2,0)   (5,1)   (4,4)   (1,10)  (2,0)
(2,0)   (5,1)   (1,10)  (4,4)   (2,0)
(2,0)   (1,10)  (5,1)   (4,4)   (2,0)
(2,0)   (1,10)  (4,4)   (5,1)   (2,0)

I implemented the two functions below to calculate distances:

float Distancia(Cidade cA, Cidade cB)
{
    int distanciaX = pow(cA.x - cB.x, 2);
    int distanciaY = pow(cA.y - cB.y, 2);
    float distancia = sqrt(distanciaX + distanciaY);
    return distancia;
}

float Distancias(Cidade *C, int numeroCidades)
{
    int i;
    float total = 0;
    for(i = 0; i < numeroCidades; i++)
    {
        if(i == numeroCidades - 1)
       {
            total = total + Distancia(C[i], C[0]);
        }
        else
        {
            total = total + Distancia(C[i], C[i+1]);
        }
    }
    return printf("Distancia Total: %f\n\n", total);
}

Finally, I created another function that stores all these calculated distances:

void ArmazenaDistancias(Cidade C[], int numeroCidade)
{
     int i;
     float distancias[TotalViagens(numeroCidade)];
     for(i = 0; i < TotalViagens(numeroCidade); i++)
     {
         distancias[i] = Distancias(C, numeroCidade);
     }
 }

However, when printing the values, all the calculated distances are only the result of the first trip. That is, he calculates all 24 trips, but all with the value of the first. Is there something wrong with the functions? I wonder what can be changed so that each distancias[i] save the value of each trip so that, in the end, I can discover the shortest distance (for each different initial city).

  • Possible duplicate of Doubt in the printing of results

  • Renan, do you want to save the distance from 24 cities to all other 24 cities? 576 possibilities then?

  • Renan, as Jefferson said in the comment above, it’s not quite clear what you want to do. But looking up, it seems to me that you need one to be chained up to make the relationship of the 1st city with all the others; then the 2nd city with the others, and so on.

  • The problem is to calculate all possible routes between n cities. In this example, there are 4 cities, therefore 4! = 24 possibilities. Remembering that the last city is always the starting point. My question in this topic is how to calculate the distance of each possibility (each line of the input file). That is, 24 total distances to be calculated. I need to calculate the shortest distance for each different initial city. For example, for the city (1,10) as a starting point, what is the shortest route? For the city (4,4), what is the shortest route? And so on. Apologies for not being clear.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.