Write a main program to do the following: 1) Open a file called route.txt and co
ID: 3633824 • Letter: W
Question
Write a main program to do the following:
1) Open a file called route.txt and connect it to the stream fin using
2) Define an array called the_planets of 10 planet pointers
3) Each element of the array is to point to a particular planet, the details of which come from the file. Therefore use your generate_planet function within a loop to achieve this, maybe using the line.
the_planets [i]=generate_planet(fin);
4) Report the details of all the planets to the screen by using your report_planet function within a loop. Be careful that the_planets[i] is a pointer to a planet.
5) I want to scan through all possible routes and find the shortest one please
Some comments:
To find the shortest route we will just do an exhaustive search – just considering all possible routes in turn. So with 3 planets we could do
Our Base – the_planets[0] – the_planets[1] – the_planets[2] – our base
or
rOur Base – the_planets[0] – the_planets[2] – the_planets[1] – our base
or
Our Base – the_planets[2] – the_planets[0] – the_planets[1] – our base
etc
We’ll limit ourselves to small numbers of locations to keep the calculation practical (use the first 6 locations in route.txt) but please don’t assume a fixed number.
We need to generate all possible routes. If we have n delivery locations then we will specify a route by an integer array defined asint *route=new int[n];Each possible route will characterised as followsroute[0] – first delivery locationroute[1] – second delivery location etc
Therefore the planets would be visited in the orderthe_planets [route[0]] - the_planets [route[1]] - the_planets [route[2]] - etc
*********************************************************************************************************
From Example 5, there is a example program
My Program:
#include<iostream>
#include<fstream>
#include<cmath>
using namespace std;
class planet
{
public:
int id;
float x;
float y;
float z;
};
planet *generate_planet(ifstream &fin)
{
planet *p = new planet; fin >>p->id >>p->x >>p->y >>p->z; return p;
}
void report_planet(planet *p)
{
cout<<p->id<<" ";
cout<<p->x<<" ";
cout<<p->y<<" ";
cout<<p->z<<endl;
}double pdistance(planet &p1,planet &p2)
{
float x=p1.x-p2.x;
float y=p1.y-p2.y;
float z=p1.z-p2.z;
double distance=sqrt(pow(x,2)+pow(y,2)+pow(z,2));
return distance;
}
unsigned long int factorial(int n)
{
if( n == 1 || n == 0)
return 1;
else
return n * factorial(n-1);
}
int main(){ ifstream fin("route.txt");
planet* the_planets[6];
int Number_0f_Planets;
fin >> Number_0f_Planets;
for(int i = 0; i < Number_0f_Planets; i++)
the_planets[i]=generate_planet(fin);
cout<<"Planet Id ";cout<<"x-position ";cout<<"y-position ";cout<<"z-position"<<endl;
for(int i = 0; i < Number_0f_Planets; i++)
report_planet(the_planets[i]);
double total=0;
for(int i = 0; i < Number_0f_Planets; i++)
total = total + pdistance(*the_planets[i], *the_planets[i+1]);
total += pdistance(*the_planets[0], *the_planets[Number_0f_Planets-1]);
cout<<"The total distance is:"<<total<<endl;
int *route = new int(Number_0f_Planets);
unsigned long int k, fi = factorial(Number_0f_Planets);
for( k = 0; k < fi ; k++)
{
for(int j = 0; j < Number_0f_Planets; j++)
route[j] = j;
unsigned long int ft = 1;
for(int j = 2; j <= Number_0f_Planets; j++)
{
ft = ft * (j-1);
int s = j-(( k / ft) % j);
int tmp = route[s-1];
route[s-1] = route[j-1];
route[j-1] = tmp;
}
cout<<" The Possible Route: ";
for(int i=0;i<Number_0f_Planets;i++)
cout<<the_planets[route[i]]->id<< " -> ";
}
return 0;}
Thank you very much !