IkeKap

Untitled

Oct 12th, 2019
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.57 KB | None | 0 0
  1. /* Mission 3 Part 1
  2.  * I.Kaper
  3.  * U2 Reconnaissance
  4.  * Use a pseudorandom number generator to authenticate user
  5.  * Store 2d coordinates of target positions
  6.  * Shuffle travel order randomly
  7.  * Evaluate if outputted flight path is viable under given constraints
  8.  * Display results to user
  9.  *
  10.  */
  11.  
  12. #include <iostream>
  13. #include <cmath>
  14. #include <cstdlib>
  15. #include <ctime>
  16. #include <algorithm>
  17. #include <random>
  18. #include <chrono>
  19.  
  20. using namespace std;
  21.  
  22. double distance (double, double, double, double);
  23. double distance_total(double[],double[],int[]);
  24.  
  25. int main() {
  26.     srand(4); // Set seed value to 4 (the requested seed value)
  27.     int key = rand() % 41; // Generates a random integer between 0 and 40
  28.     // cout << key << endl;
  29.     // The above line was used for testing to determine what pseudorandom number will always be generated
  30.     cout << "Please enter passcode integer to gain access to program" << endl;
  31.     int passcode;
  32.     cin >> passcode;
  33.     string missionName = "Ladybug";
  34.     cout << "Please enter your codename to gain access to program" << endl;
  35.     string codeName;
  36.     cin >> codeName;
  37.     if (passcode != key || missionName != codeName)
  38.     {
  39.         cout << "INCORRECT Credentials!! Program has been terminated" << endl;
  40.         return 0;
  41.     }
  42.     else
  43.     {
  44.         double x_coords[15] = {58,1.25,25,96,22,51,12.8,91,4,63,65.5,12,42,72.7,45};
  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
  46.         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
  47.                                                              // the hypothetical flight plan
  48.         unsigned seed=chrono::system_clock::now().time_since_epoch().count(); // This creates a unique seed based on system time
  49.         shuffle(&order[0],&order[15],default_random_engine(seed)); // This uses the default random engine and the seed in the previous line
  50.                                                                    // to randomly shuffle the elements in the array order to create
  51.                                                                    // a random flight path
  52.  
  53.         double totalDistance = distance_total (x_coords,y_coords,order);
  54.         bool enoughGas = (totalDistance < 525);
  55.         cout << "The length of the randomly selected route is " << (int) totalDistance << " miles." << endl; // cast to int for conciseness
  56.         if (enoughGas){
  57.             cout << "The route is short enough to fly without refueling." << endl;
  58.         }
  59.         else{
  60.             cout << "The route is too long to fly without refueling." << endl;
  61.         }
  62.  
  63.     }
  64.    
  65.    
  66.     return 0;
  67. }
  68.  
  69. double distance(double x1, double y1, double x2, double y2) { // This function uses the Pythagorean theorem to calculate 2d distance
  70.  
  71.     double ans, x_dist, y_dist;
  72.     x_dist = x2 - x1;
  73.     y_dist = y2 - y1;
  74.     ans = sqrt(pow(x_dist,2)+pow(y_dist,2));
  75.     return ans;
  76.  
  77. }
  78.  
  79. double distance_total(double a[], double b[], int c[]) { // This function calculates total flight distance
  80.                                                          // I split it into a subfunction to keep code cleaner
  81.     int foo = 15; // Length of the given array
  82.     double sum;
  83.     for (int i = 0; i < foo-1; i++)
  84.     {   int Index1 = c[i];
  85.         int Index2 = c[i+1];
  86.         double x1 = a[Index1]; double x2 = a[Index2];
  87.         double y1 = b[Index1]; double y2 = b[Index2];
  88.         double z = distance(x1,y1,x2,y2);
  89.         sum += z;
  90.     }
  91.     return sum;
  92.    
  93. }
Advertisement
Add Comment
Please, Sign In to add comment