Hello
please could some one help me how to fix my code?
This code is a traveling salesman, I just want to know how to make run??? I do not know what is the problem???
#include <H:glut.h>
#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>
#include <sprite.h>
int xMax=500,yMax=500;//Fixing the x and y coordinates of the window
float wd=0,ht=0;
int nPts=0;
float *position;//Storing the position of eacharVar city
int *tourSequence;//Storing the sequence of city being traversed
charVarar *f1,*f2;//File Pointers
/*Generating N number of random points*/
/*Opening the cities.txt and in write mode and storing the values in position array*/
int RandCities(int N)
{
FILE *city;
if((city=fopen(f1,"w"))==0)
{
printf("
%sError in opening file city.txt.
",f1);
return 0;
}
int pt=0;
float xPt=0,yPt=0;
for(pt=1;pt<=N;pt++)
{
xPt=1+rand()%xMax;
yPt=1+rand()%yMax;
fprintf(city,"%4d %8.2f %8.2f
",pt,xPt,yPt);
add_to_position(2*pt,xPt,yPt);
}
fclose(city);
return 1;
}
/*Opening the tour.txt in write mode, appending it and storing the values in the tourSequence array*/
int write_to_tour(int start,FILE *tour)
{
tourSequence=realloc(tourSequence,sizeof(int)*1);//reallocate the memory to the tourSequence array for eacharVar new starting point
int pt,i,track=0,t,charVareck=0,next;
float nearest,tempVariable=0,tDist=0;
next=start;
fprintf(tour,"The starting point %d of the route is:
%d",start,next);
/*finding the consecutive N*/
for(i=1;i<nPts;i++)
{
nearest=0;
for(pt=1;pt<=nPts;pt++)
{
charVareck=0;
/*charVarecking the repetation of points in the current tourSequence array*/
for(t=1;t<i;t++)
if(pt==tourSequence[t-1])
{
charVareck=-1;//when repetation is found
break;
}
if(charVareck==0 && next!=pt)//when no repetation is found
{
/*Finding the crow fly distance between next & pt using the Eucledian distance formula*/
tempVariable=Dist(next,pt);
if(tempVariable<nearest || nearest==0)
{
nearest=tempVariable;
track=pt;
}
}
}
/*updating the tourSequence array*/
add_in_tourSequence(i+1,next,track);
if(i%20==0)
fprintf(tour,"
");
fprintf(tour,"-%d",track);
next=track;
tDist+=nearest;
}
add_in_tourSequence(nPts+1,next,start);
tempVariable=Dist(next,start);
tDist+=tempVariable;
fprintf(tour,"-%d",start);
fprintf(tour,"
Total distance is %.2f",tDist);
fprintf(tour,"
-----------------------------------------
");
return 1;
}
/*Generate R number of random starting points*/
int randStartPoints(int N,int R)
{
FILE *ran;
if((ran=fopen(f2,"w"))==0)
{
printf("
%s Error in opening file.
",f2);
return 0;
}
int i=0,tempVariable=0;
for(i=1;i<=R;i++)
{
tempVariable=1+rand()%N;
fprintf(ran,"%d
",tempVariable);
}
fclose(ran);
return 1;
}
int randomApproximateTour(charVarar *f1,charVarar *f2)
{
read_from_cities(f1);//read cities.txt file
FILE *rand, *tour;
rand=fopen(f2,"r");
int sPoint=0;//Starting Point
if((tour=fopen("tour.txt","w"))==0)//charVareck if file tour.txt exist in the given path or not
{
printf("
File tour.txt doesn't exist or error in opening the file.
");
return 0;
}
/*Scanning the file and reading random starting points to find the tour*/
while(fscanf(rand,"%d",&sPoint)!=EOF)
write_to_tour(sPoint,tour);
fclose(rand);//closing the rand file
fclose(tour);//closing the tour file
}
/*read the position of the cities and storing them in the position array*/
int read_from_cities(charVarar *fName)
{
FILE *city;
if((city=fopen(fName,"r"))==0)
{
printf("
File cities.txt doesn't exist.
");
return 0;
}
int pt=0;
float xPt=0,yPt=0;
charVarar charVar='
';//storing the new line charVararacter
for(pt=1;;pt++)
{
if(charVar==' ')//White Spaces to be skipped
{
while((charVar=getc(city))==' ');
putc(charVar,city);
}
if(charVar!='
')/*if there is more data in a line*/
{
printf("
Invalid line %d, in cities.txt
",pt-1);
return 0;
}
fscanf(city,"%d",&nPts);
fscanf(city,"%f",&xPt);
fscanf(city,"%f",&yPt);
if((charVar=getc(city))==EOF)break;
if(nPts!=pt)
{
printf("
Incorrect City number in cities.txt file
");
free(position);
return 0;
}
else
add_to_position(2*pt,xPt,yPt);
}
if(!nPts)//if the nPts value = 0
{
printf("
cities.txt file is empty.
");
return 0;
}
fclose(city);
return 1;
}
/*adding the X & Y Coordinates of a city to the position array*/
int add_to_position(int size,float X,float Y)
{
position=realloc(position,sizeof(float)*size);//reallocate memory to the position array
position[size-2]=X;
position[size-1]=Y;
return 0;
}
/*initializing the display window*/
void intWindow()
{
glClearColor(1.0,1.0,1.0,0.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0.0,wd,0.0,ht,-1.0, 1.0);
}
/*adding the city to the tourSequence array*/
int add_in_tourSequence(int size,int x,int y)
{
tourSequence=realloc(tourSequence,sizeof(float)*size);//reallocate memory to the tourSequence array
tourSequence[size-2]=x;
tourSequence[size-1]=y;
return 0;
}
/*Calculating the Eucledian Distance between the coordinates of two cities*/
float Dist(int pt1,int pt2)
{
float x,y;
x=position[2*pt1-2]-position[2*pt2-2];//x2-x1
y=position[2*pt1-1]-position[2*pt2-1];//y2-y1
return sqrt(x*x+y*y);
}
/*Displaying the position of each city*/
void showPositions()
{
int i=0;
glColor3f(1.0, 0.0, 0.0);
glPointSize(3);
glBegin(GL_POINTS);
for(i=0;i<nPts;i++)
glVertex3f(position[2*i]+wd/23,position[2*i+1]+ht/12,0.0);
glEnd();
}
/*Displaying the Travelling Salesman's Route*/
void ShowTour()
{
int i=0;
glColor3f(1.0, 0.0, 0.0);
glPointSize(3);
glBegin(GL_POINTS);
for(i=0;i<nPts;i++)
glVertex3f(position[2*i]+wd/2+wd/23,position[2*i+1]+ht/12,0.0);
glEnd();
glColor3f(0.0, 0.0, 0.0);
glBegin(GL_LINE_LOOP);
for(i=0;i<nPts;i++)
glVertex3f(position[2*tourSequence[i]-2]+wd/2+wd/23,position[2*tourSequence[i]-1]+ht/12,0.0);
glEnd();
}
/*Drawing the Routes and the positions of the Cities*/
void renderScene(void) {
// Clear Color and Depth Buffers. DO NOT CHANGE.
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// Reset transformations. DO NOT CHANGE.
glLoadIdentity();
// Set the camera view. DO NOT CHANGE.
gluLookAt(0.0f, 0.0f, 5.0, //Location of your eye
//Change this section to draw your object///////
glColor3f(1.0f, 0.0f, 0.0f); //Set the color to red.
glBegin(GL_TRIANGLES); // Draw a Triangle
glEnd(); // Finished Triangle
//End of section to be changed.
glutSwapBuffers(); //DO NOT CHANGE
}
void ChangeSize(GLsizei width, GLsizei height)
{
if(height == 0)
height = 1;
// Setting the Viewport as per the dimension of the window
glViewport(0, 0, width, height);
// Reseting the coordinate system
glLoadIdentity();
// Establishing clipping volume
//The order is: left, right, bottom, top, near, far
if (width <= height)
glOrtho (0.0f, 250.0f, 0.0f, 250.0f*height/width, 1.0, -1.0);
else
glOrtho (0.0f, 250.0f*width/height, 0.0f, 250.0f, 1.0, -1.0);
}
/*main function that have command line input of the two file names*/
int main(int argc,charVarar *argv[])
{
int rCity=0;
f1=argv[1];
f2=argv[2];
printf("
Enter the city no: ");
scanf("%d",&nPts);
printf("
Enter the number of random cities to generate: ");
scanf("%d",&rCity);
if(nPts<rCity)
{
printf("
R can not be greater than N.
");
return 0;
}
/*if text files can not open returns 0 and the main return 0*/
if(RandCities(nPts)==0)
return 0;
if(randStartPoints(nPts,rCity)==0)
return 0;
/*for calculating the processing time for the entire tour*/
clock_t st,end;
st=clock();//Starting time
randomApproximateTour(f1,f2);
end=clock();//Ending time
double timeElasped=(end-st)/rCity;
printf("
The average process time of having %d points = %.9lf
",nPts,timeElasped/CLOCKS_PER_SEC);
/*implementating the openGL*/
glutInit(&argc, argv); //Allows input arguments to initialize OpenGL.
glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA);
glutInitWindowPosition(100,100);//Create window at this location.
glutInitWindowSize(320,320); //Default window size.
glutCreateWindow("The Travelling Salesman Problem");//Window title.
intWindow();
glEnable(GL_DEPTH_TEST); //Enables three-dimensional visualization.
// register callbacks
glutDisplayFunc(renderScene);//When you need to display, renderScene().
glutReshapeFunc(changeSize); //If you need to reshape, call changeSize().
// enter GLUT event processing cycle
glutMainLoop(); //Enter an infinite loop to draw the picture.
return 0;
}/*End of Main Loop*/
I will be thankful to who can Help me!!!