Ok so i understand conceptually what I have to do. I am having problems with the syntax however. I am dealing with 8 total cities the person has to travel to, including the starting and ending city that are the same. I have decided to go with a brute force method, i.e., solving the total distance for all permutations, and then selecting the shortest one. Here is my code so far:
Code:
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
typedef struct {
double x;
double y;
}city_location;
void permutations( *city)
void distance( *city)
main() {
city_location *city;
//latitude and longitude converted to xy coordinates with Tucson as origin
//units of meters
city1.x = 0;
city1.y = 0;
city2.x = 2212660;
city2.y = 1142144;
city3.x = 2212660;
city3.y = 1262261;
city4.x = 3480853;
city4.y = 1007221;
city5.x = 3406993;
city5.y = 916664;
city6.x = 1973793;
city6.y = 781752;
city7.x = 575166;
city7.y = 892638;
city8.x = 1313767;
city8.y = 112735;
total_dist = 0;
void distance(city_location *city){
double dist;
int I;
for(i=0; i<6; i++){
dist = sqrt(((x[i+1]*x[i+2])^2)+((y[i+1]*y[i+2])^2));
//store a value for each leg or the trip
double partial_dist = //sum of these values;
double start_dist = sqrt(((x[1])^2)+((y[1])^2));
double end_dist = sqrt(((x[7])^2)+((y[7])^2));
double total_dist = partial_dist + start_dist + end_dist;
if(new_dist < shortest_dist){
//store value and continue loop
}
}
so the things i don't know how to do are:
- how to create all the possible permutations
- how to read all those permutations into my distance function
- how to store various values within a loop and then sum them after loop is finished
Any help at all would be appreciated greatly.