Ok, here is my updated code:
Code:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>
struct City
{
float name;
float x;
float y;
};
void randPerm(int N, int *perm[])
{
srand(time(NULL));
int i, n1, n2, tmp;
for(i=0;i<N;i++)//initalize numbers
(*perm)[i] = i;
for(i=0;i<N;i++)
{
n1 = rand()%N;
n2 = rand()%N;
tmp = (*perm)[n2];
(*perm)[n2] = (*perm)[n1];
(*perm)[n1] = tmp;
printf("%d\n", (*perm)[n1]);
}
}
void printInGivenOrder(struct City cityArray[], int *perm[])
{
int i,num;
printf("\nOrder of Cities:\n");
for(i=0;i<10;i++)
{
num=(*perm[i]);
printf("%d\n", cityArray[num].name);
}
}
float getTotalDistGivenPerm(int N, struct City cityArray[], int perm[])
{
int i;
int index1,index2;
float distance,totaldist=0, a1,a2,b1,b2,x,y;
for(i=1;i<N;i++)
{
index1 = perm[i];
index2 = perm[i-1];
a1=cityArray[index1].x;
a2=cityArray[index1].y;
b1=cityArray[index2].x;
b2=cityArray[index2].y;
x=69.1*(a1-b1);
y=53.0*(a2-b2);
distance=sqrt(x*x + y*y);
totaldist=totaldist+distance;
}
return totaldist;
}
int main()
{
struct City cityArray[10];
cityArray[0].name=1; //"Ann Arbor";
cityArray[0].x=42.28;
cityArray[0].y=-83.75;
cityArray[1].name=2;// "Austin";
cityArray[1].x=30.25;
cityArray[1].y=-97.75;
cityArray[2].name=3;// "Boston";
cityArray[2].x=42.36;
cityArray[2].y=-71.06;
cityArray[3].name=4;// "Chicago";
cityArray[3].x=41.88;
cityArray[3].y=-87.63;
cityArray[4].name=5;// "Detroit";
cityArray[4].x=42.33;
cityArray[4].y=-83.05;
cityArray[5].name=6;// "Lansing";
cityArray[5].x=42.73;
cityArray[5].y=-84.55;
cityArray[6].name=7;// "Las Vegas";
cityArray[6].x=36.18;
cityArray[6].y=-115.14;
cityArray[7].name=8;// "Los Angeles";
cityArray[7].x=34.05;
cityArray[7].y=-118.25;
cityArray[8].name=9;// "Miami";
cityArray[8].x=25.79;
cityArray[8].y=-80.22;
cityArray[9].name=10;// "Seattle";
cityArray[9].x=47.61;
cityArray[9].y=-122.33;
int number=10;
int array[10];
randPerm(number, &array);
printInGivenOrder(cityArray, &array);
float finalDistancefromPerm= getTotalDistGivenPerm(number, cityArray, array);
printf("\n %f", finalDistancefromPerm);
return 0;
}
I guess my questions still are:
1. Most important question: How do I enter the names of the cities into the structure? If I change the "float name" to "char name[50]" and "cityArray[0].name"= "Ann Arbor" instead of 1 it says "incompatible types in assignment"
2. Program still crashes when I hit the randPerm function, and I tried the debugger but couldn't figure out what's wrong