Advertisement
Guest User

Untitled

a guest
Jul 28th, 2017
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.65 KB | None | 0 0
  1.  
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <math.h>
  5.  
  6. void main(void)
  7. {
  8.     void finddistances(int[][2], int, int[], int[]);
  9.     void findtimes(int[], double[], int[], int, int[], int);
  10.     void findmeetingpoint(int[][2], int[], double[], int[], int, int);
  11.  
  12.     char shipname[15][18] = {       //The names of each ship
  13.                 {"Bluenose"},
  14.                 {"France"},
  15.                 {"Repulse"},
  16.                 {"Golden Hind"},
  17.                 {"Hyperion"},
  18.                 {"Lusitania"},
  19.                 {"Hood"},
  20.                 {"Edmund Fitzgerald"},
  21.                 {"Titanic"},
  22.                 {"Andrea Dorea"},
  23.                 {"Queen Mary"},
  24.                 {"Queen Elizabeth"},
  25.                 {"Black Moxy"},
  26.                 {"Santa Maria"},
  27.                 {"Mary Celeste"}
  28.                             };
  29.  
  30.     int doctor[15] = {1, 1, 0, 1, 0, 1, 0, 1, 0, 1, 1, 1, 0, 1, 1};
  31.     //This array is a flag, specifying if there is a doctor on the ship,
  32.     // corresponding with the elements of shipname.
  33.     //  e.g. if element 1 has a 1, then the bluenose has a doctor..
  34.     //   (1 = there is a doctor, 2 = there is not)
  35.  
  36.    
  37.  
  38.     int illpassenger[15] = {1, 0, 1, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0};
  39.    
  40.     //This specifies whether or not there is a sick passenger in the matching
  41.     // location in the array, also corresponding with the shipname array, as above.
  42.    
  43.  
  44.  
  45.    
  46.  
  47.     int coord[15][2] =      {           //The x/y coordinates of each ship
  48.                             {125 , 372},
  49.                             {800 , 725},
  50.                             {500 , 530},
  51.                             {120 , 498},
  52.                             {600 ,  58},
  53.                             {650 , 890},
  54.                             {400 , 687},
  55.                             {280 , 715},
  56.                             {510 , 650},
  57.                             {89  , 802},
  58.                             {784 , 410},
  59.                             {900 , 945},
  60.                             {176 , 750},
  61.                             {200 , 178},
  62.                             {200 ,  29}
  63.                             };
  64.     int *p;
  65.     p = &coord[0][0];
  66.  
  67.     int speed[15] = {18, 32, 30, 17, 35, 35, 15, 16, 15, 35, 34, 28, 70, 10, 14};
  68.     //This array specifies the speed of each ship, also corresponding with
  69.     // the element in the shipname array, as the ill poassangers and doctors did.
  70.  
  71.     int distance[15] = {0};    
  72.     //The distance from the ship with the ill passenger to the other ships
  73.  
  74.     int meetpoint[2];
  75.     //The point where the boat with the sick passenger will meet the boat
  76.         //with the doctor
  77.  
  78.     int inneed,         //Index of the ship with an ill pasenger and no doctor.
  79.         i,              //Used in loops
  80.         closestship = 0;//This will mark the plce in the array of the closest
  81.                                 //ship weith a doctor
  82.  
  83.     double time[15] = {0};
  84.     //The times to get from the ship with the ill passenger to the others;
  85.    
  86.     //Check each ship to see if there is an ill passenger and no doctor
  87.  
  88.     for(i = 0 ; i < 15 ; ++i)
  89.         if(doctor[i] == 0 && illpassenger[i] == 1) //if there is no doctor, but an ill passenger
  90.             inneed = i;                           //Mark the boat in need
  91.  
  92.     printf("Searching...     ");            //Just telling the user the program is searching...
  93.    
  94.     finddistances(coord , inneed , distance , doctor);
  95.         //function to find/store the ditances to all the ships with doctors
  96.         //                                           from the ill passenger
  97.    
  98.     findtimes(distance , time , speed , inneed , doctor, closestship);
  99.         //function to find/store the time it takes to getthe ill pasenger
  100.         //                                          to ships with doctors
  101.  
  102.     findmeetingpoint(coord , speed , time , meetpoint, closestship, inneed);
  103.         //Function to find/store the meeting point of the ill passenger
  104.         //                                   and the appropriate doctor
  105.    
  106.     /* Now that we have all the required information, we must work with it */
  107.    
  108.     printf("Information retreieved!\n\n");
  109.     printf("Ship in need: %s\n", shipname[inneed]);
  110.     printf("Closest ship with a doctor: %s\n", shipname[closestship]);
  111.     printf("Distance between the ships: %d miles\n", distance[closestship]);
  112.     printf("Ideal meeting point: %d , %d\n", meetpoint[0] , meetpoint[1]);
  113.     printf("Time it will take for the ships to meet: %f\n",time[closestship]);
  114.  
  115.  
  116.  
  117. }
  118.  
  119.  
  120. void finddistances(int coord[][2] , int inneed , int distance[], int doctor[])
  121. {
  122.     int i,  //for a loop
  123.         a,  //Used for math arythmetic with the x coords
  124.         b,  //Used for math arythmetic with the y coords
  125.         c;  // hypotenuse. (c2 = a2 + b2)
  126.  
  127.  
  128.     for(i = 0 ; i < 15 ; ++i)   //For each ship
  129.         if(doctor[i] = 1)       //If there is actually a doctor...
  130.         {
  131.             //subtract the 2 y values, and the 2 x values, and square them (pythagerous).
  132.             a = coord[inneed][1] - coord[i][1]; //set a to the distance between the x coords
  133.             b = coord[inneed][2] - coord[i][2]; //same as above but with y coord
  134.             a = pow(a , 2);                     //Set x to x to the power of 2
  135.             b = pow(b , 2);                     //Set y to y to the power of 2
  136.             c = a + b;                          //C NEEDS TO BE SQUARE ROOTED (c2 = a2 + b2)
  137.             distance[i] = sqrt( c );            //Record the square root of c2 as the distance..
  138.                                                     //... To that ship.
  139.         }
  140. }
  141.  
  142. void findtimes(int distance[] , double time[] , int speed[] , int inneed , int doctor[], int closestship)
  143. {
  144.     int i;                      //used for a loop.
  145.  
  146.     for(i = 0 ; i < 15 ; ++i)
  147.     {
  148.         if(distance[i] != 0)    //Due to the finddistance function, only the ships
  149.                                 //with doctors will have distances, and the rest will
  150.                                 //have the value 0, so eliminate these.
  151.  
  152.         time[i] = distance[i] / (speed[inneed] + speed[i]);
  153.                                 //take the distance to the current ship, and divide it
  154.                                     //by the sums of the speeds of both ships
  155.     }
  156.  
  157.     for(i = 0 ; i < 15 ; ++i)   //for each time
  158.     {
  159.         if(time[i] != 0)        //Excluding ships without doctors. ships without doctors wil;
  160.                                         //have a value of 0 in the time array.
  161.             closestship = 999;      //set this variable to a rediculously high number, that could not be
  162.                                     //the shortest time.
  163.             if(closestship > time[i])   //if the closestship variable is greater than the time to get to
  164.                 closestship = i;            //the ship we are currently looking at, set it to that number.
  165.     }      
  166. }
  167.  
  168.  
  169.  
  170. void findmeetingpoint(int coord[][2] ,
  171.                       int speed[] ,
  172.                       double time[] ,
  173.                       int meetpoint[],
  174.                       int closestship,
  175.                       int inneed)
  176. {
  177.     int i;                      //used for a loop
  178.            
  179.     /*
  180.     ** In the following mathematic actions, it wil calculate the best meeting poit for the 2 ships.
  181.     ** It will do this by a modified midpoint formula. (m = x1 * x2 / 2 , m = y1 * y2 /2)....
  182.     ** It will find the x coordinate by taking the x coordinate of the ship in need, multiplying
  183.     ** it by the speed of the other ship, and adding that to the x coordinate of the other ship multiplied
  184.     ** by the speed of the ship in need. The sum of all this will be divided by the 2 combined speeds,
  185.     ** to get the x coordinate. The same arythmetic will be done to find the y coordinate, but the x's
  186.     ** replaced with y's.
  187.     */
  188.  
  189.     meetpoint[0] = ( (coord[inneed][0] * speed[closestship]) + (coord[closestship][0] * speed[inneed]) )
  190.                                         / (speed[inneed] + speed[closestship]);
  191.  
  192.     meetpoint[1] = ( (coord[inneed][1] * speed[closestship]) + (coord[closestship][1] * speed[inneed]) )
  193.                                         / (speed[inneed] + speed[closestship]);
  194. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement