Problem: Darth Vader wants to check that his TIE fighter pilots are patrolling a
ID: 3569995 • Letter: P
Question
Problem: Darth Vader wants to check that his TIE fighter pilots are patrolling adequately-sized regions of
the galaxy. He has files of data that contain the patrol coordinates for each of his pilots. With the fear
of being force choked, you have agreed to write a program that analyzes the data and determines the
size of the area patrolled by the pilots.
Given a list of checkpoint coordinates logged by the pilot that represent a polygonal shape, the area of
the shape can be calculated with the following formula
Input: All input will come from a file named pilot_routes.txt. The file will contain the pilot
Explanation / Answer
Program code:
// PilotsPatrolling.cpp : Defines the entry point for the console application.
#include "stdafx.h"
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
int main()
{
//create myfile of ifstream
ifstream myfile("pilot_routes.txt");
int n=0;
bool flag=false;
string name, values;
string fx="", fy="";
double x[10];
double y[10];
//if the ifstream is open
if (myfile.is_open())
{
//get the name
myfile>>name;
while ( myfile!=NULL )
{
//get the values
myfile>>values;
fx="", fy="";
flag=false;
//logic to separate the coordinates
for(int k=0;k<values.length();k++)
{
if(values[k]==',')
{
flag=true;
continue;
}
if(!flag)
fx+=values[k];
else
fy+=values[k];
}
//convert the string to double
x[n]=stod(fx);
y[n]=stod(fy);
//increment the counter
n++;
}
}
//close the ifstream file
myfile.close();
float total=0;
//compute the summation value
for(int j=0;j<n-2;j++ )
{
total+=abs((x[j+1]+x[j])*(y[j+1]-y[j]));
}
//compute the final total
total=0.5*total;
//open a ofstream file to write the output
ofstream nfile("pilot_areas.txt");
if (nfile.is_open())
{
nfile<<name<<" "<<total;
}
//close the ofstream file
nfile.close();
system("pause");
return 0;
}
-------------------------------------------------------------------------------------------
Sample input:
pilot_routes.txt
Han 4,0 4,7.5 7,7.5 7,3 9,0 7,0 4,0
Sample output:
pilot_areas.txt
Han 85.5
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.