Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* Mission 3 Part 1
- * I.Kaper
- * U2 Reconnaissance
- * Use a pseudorandom number generator to authenticate user
- * Store 2d coordinates of target positions
- * Shuffle travel order randomly
- * Evaluate if outputted flight path is viable under given constraints
- * Display results to user
- *
- */
- #include <iostream>
- #include <cmath>
- #include <cstdlib>
- #include <ctime>
- #include <algorithm>
- #include <random>
- #include <chrono>
- using namespace std;
- double distance (double, double, double, double);
- double distance_total(double[],double[],int[]);
- int main() {
- srand(4); // Set seed value to 4 (the requested seed value)
- int key = rand() % 41; // Generates a random integer between 0 and 40
- // cout << key << endl;
- // The above line was used for testing to determine what pseudorandom number will always be generated
- cout << "Please enter passcode integer to gain access to program" << endl;
- int passcode;
- cin >> passcode;
- string missionName = "Ladybug";
- cout << "Please enter your codename to gain access to program" << endl;
- string codeName;
- cin >> codeName;
- if (passcode != key || missionName != codeName)
- {
- cout << "INCORRECT Credentials!! Program has been terminated" << endl;
- return 0;
- }
- else
- {
- double x_coords[15] = {58,1.25,25,96,22,51,12.8,91,4,63,65.5,12,42,72.7,45};
- double y_coords[15] = {17.19,20,32,48.3,88,26,67,85,64,77,35,80,6.6,99,10.6}; // The above are the given coordinates
- int order[15]={0,1,2,3,4,5,6,7,8,9,10,11,12,13,14}; // This array holds the order in which you will go to each site
- // the hypothetical flight plan
- unsigned seed=chrono::system_clock::now().time_since_epoch().count(); // This creates a unique seed based on system time
- shuffle(&order[0],&order[15],default_random_engine(seed)); // This uses the default random engine and the seed in the previous line
- // to randomly shuffle the elements in the array order to create
- // a random flight path
- double totalDistance = distance_total (x_coords,y_coords,order);
- bool enoughGas = (totalDistance < 525);
- cout << "The length of the randomly selected route is " << (int) totalDistance << " miles." << endl; // cast to int for conciseness
- if (enoughGas){
- cout << "The route is short enough to fly without refueling." << endl;
- }
- else{
- cout << "The route is too long to fly without refueling." << endl;
- }
- }
- return 0;
- }
- double distance(double x1, double y1, double x2, double y2) { // This function uses the Pythagorean theorem to calculate 2d distance
- double ans, x_dist, y_dist;
- x_dist = x2 - x1;
- y_dist = y2 - y1;
- ans = sqrt(pow(x_dist,2)+pow(y_dist,2));
- return ans;
- }
- double distance_total(double a[], double b[], int c[]) { // This function calculates total flight distance
- // I split it into a subfunction to keep code cleaner
- int foo = 15; // Length of the given array
- double sum;
- for (int i = 0; i < foo-1; i++)
- { int Index1 = c[i];
- int Index2 = c[i+1];
- double x1 = a[Index1]; double x2 = a[Index2];
- double y1 = b[Index1]; double y2 = b[Index2];
- double z = distance(x1,y1,x2,y2);
- sum += z;
- }
- return sum;
- }
Advertisement
Add Comment
Please, Sign In to add comment