Write a main program to do the following 1) Open a file called route.txt and con
ID: 3631879 • 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
ifstream fin(“route.txt”); // Don’t forget the stream header files!
The first line in the file contains the number of planets. Subsequent lines contain the id x y z coordinates of the planets.
2) Define an array called the_planets of 10 planet pointers, i.e. use the line
planet* the_planets[10]; // Are you sure why the syntax is like this?
We will assume for the present that there are never more than 10 deliveries per route.
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) Finally, evaluate the total route length. (We will visit the the planets in the order in which they appear in the file).
Our Base – the_planets[0] – the_planets[1] --- the_planets[n-1] – our base
The our base is coordinate (0,0,0), so the calculation should be start from our base point (0,0,0) then planet 1, planet 2...at last, come back to base point (0,0,0)
and print it on the screen. Be careful to deal with the start and end locations
Explanation / Answer
#include <iostream>
#include <fstream>
using namespace std;
//planet structure
typedef struct planet
{
int id;
int x,y,z;
};
//function prototypes
planet* generate_planet(ifstream &fin);
void report_planet(planet *the_planet);
//main function
int main()
{
ifstream fin; //ifstream to read file data
fin.open("route.txt");
//array of 10 planet pointers
planet *the_planet[10];
//number of deliveries read from first line of the file
int deliveries;
fin>>deliveries;
//reads data from the file
//and stores them in array of planet pointers
for(int i=0;i<deliveries;i++)
the_planet[i] = generate_planet(fin);
//displays the array values to the screen
cout<<" Planet ID X y Z"<<endl;
cout<<" ---------------------------------- ";
for(int i=0;i<deliveries;i++)
report_planet(the_planet[i]);
//finding routes
int **route;
route = new int*[deliveries];
for(int i=0;i<deliveries;i++)
{
route[i] = new int [deliveries];
int j=i;
for(int k=0;k<deliveries;k++)
{
route[i][k] = j;
j++;
j = j% deliveries;
}
}
cout<<endl;
//displaying possible routes to the screen
for(int i=0;i<deliveries;i++)
{
cout<<"route"<<i+1<<":"<<endl<<"source -> ";
for(int j=0;j<deliveries;j++)
cout<<the_planet[route[i][j]]->id<< " -> ";
cout<<"end"<<endl;
}
return 0;
}
//reads one planet from the file
//and store it in a planet object
//then returns a pointer to the planet object
planet* generate_planet(ifstream &fin)
{
planet *Planet = new planet();
fin>>Planet->id>>Planet->x>>Planet->y>>Planet->z;
return Planet;
}
//prints the planet data to the screen
void report_planet(planet *the_planet)
{
cout<<" "<<the_planet->id<<" "<<the_planet->x<<" "<<the_planet->y<<" "<<the_planet->z<<endl;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.