Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <math.h>
- void main(void)
- {
- void finddistances(int[][2], int, int[], int[]);
- void findtimes(int[], double[], int[], int, int[], int);
- void findmeetingpoint(int[][2], int[], double[], int[], int, int);
- char shipname[15][18] = { //The names of each ship
- {"Bluenose"},
- {"France"},
- {"Repulse"},
- {"Golden Hind"},
- {"Hyperion"},
- {"Lusitania"},
- {"Hood"},
- {"Edmund Fitzgerald"},
- {"Titanic"},
- {"Andrea Dorea"},
- {"Queen Mary"},
- {"Queen Elizabeth"},
- {"Black Moxy"},
- {"Santa Maria"},
- {"Mary Celeste"}
- };
- int doctor[15] = {1, 1, 0, 1, 0, 1, 0, 1, 0, 1, 1, 1, 0, 1, 1};
- //This array is a flag, specifying if there is a doctor on the ship,
- // corresponding with the elements of shipname.
- // e.g. if element 1 has a 1, then the bluenose has a doctor..
- // (1 = there is a doctor, 2 = there is not)
- int illpassenger[15] = {1, 0, 1, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0};
- //This specifies whether or not there is a sick passenger in the matching
- // location in the array, also corresponding with the shipname array, as above.
- int coord[15][2] = { //The x/y coordinates of each ship
- {125 , 372},
- {800 , 725},
- {500 , 530},
- {120 , 498},
- {600 , 58},
- {650 , 890},
- {400 , 687},
- {280 , 715},
- {510 , 650},
- {89 , 802},
- {784 , 410},
- {900 , 945},
- {176 , 750},
- {200 , 178},
- {200 , 29}
- };
- int *p;
- p = &coord[0][0];
- int speed[15] = {18, 32, 30, 17, 35, 35, 15, 16, 15, 35, 34, 28, 70, 10, 14};
- //This array specifies the speed of each ship, also corresponding with
- // the element in the shipname array, as the ill poassangers and doctors did.
- int distance[15] = {0};
- //The distance from the ship with the ill passenger to the other ships
- int meetpoint[2];
- //The point where the boat with the sick passenger will meet the boat
- //with the doctor
- int inneed, //Index of the ship with an ill pasenger and no doctor.
- i, //Used in loops
- closestship = 0;//This will mark the plce in the array of the closest
- //ship weith a doctor
- double time[15] = {0};
- //The times to get from the ship with the ill passenger to the others;
- //Check each ship to see if there is an ill passenger and no doctor
- for(i = 0 ; i < 15 ; ++i)
- if(doctor[i] == 0 && illpassenger[i] == 1) //if there is no doctor, but an ill passenger
- inneed = i; //Mark the boat in need
- printf("Searching... "); //Just telling the user the program is searching...
- finddistances(coord , inneed , distance , doctor);
- //function to find/store the ditances to all the ships with doctors
- // from the ill passenger
- findtimes(distance , time , speed , inneed , doctor, closestship);
- //function to find/store the time it takes to getthe ill pasenger
- // to ships with doctors
- findmeetingpoint(coord , speed , time , meetpoint, closestship, inneed);
- //Function to find/store the meeting point of the ill passenger
- // and the appropriate doctor
- /* Now that we have all the required information, we must work with it */
- printf("Information retreieved!\n\n");
- printf("Ship in need: %s\n", shipname[inneed]);
- printf("Closest ship with a doctor: %s\n", shipname[closestship]);
- printf("Distance between the ships: %d miles\n", distance[closestship]);
- printf("Ideal meeting point: %d , %d\n", meetpoint[0] , meetpoint[1]);
- printf("Time it will take for the ships to meet: %f\n",time[closestship]);
- }
- void finddistances(int coord[][2] , int inneed , int distance[], int doctor[])
- {
- int i, //for a loop
- a, //Used for math arythmetic with the x coords
- b, //Used for math arythmetic with the y coords
- c; // hypotenuse. (c2 = a2 + b2)
- for(i = 0 ; i < 15 ; ++i) //For each ship
- if(doctor[i] = 1) //If there is actually a doctor...
- {
- //subtract the 2 y values, and the 2 x values, and square them (pythagerous).
- a = coord[inneed][1] - coord[i][1]; //set a to the distance between the x coords
- b = coord[inneed][2] - coord[i][2]; //same as above but with y coord
- a = pow(a , 2); //Set x to x to the power of 2
- b = pow(b , 2); //Set y to y to the power of 2
- c = a + b; //C NEEDS TO BE SQUARE ROOTED (c2 = a2 + b2)
- distance[i] = sqrt( c ); //Record the square root of c2 as the distance..
- //... To that ship.
- }
- }
- void findtimes(int distance[] , double time[] , int speed[] , int inneed , int doctor[], int closestship)
- {
- int i; //used for a loop.
- for(i = 0 ; i < 15 ; ++i)
- {
- if(distance[i] != 0) //Due to the finddistance function, only the ships
- //with doctors will have distances, and the rest will
- //have the value 0, so eliminate these.
- time[i] = distance[i] / (speed[inneed] + speed[i]);
- //take the distance to the current ship, and divide it
- //by the sums of the speeds of both ships
- }
- for(i = 0 ; i < 15 ; ++i) //for each time
- {
- if(time[i] != 0) //Excluding ships without doctors. ships without doctors wil;
- //have a value of 0 in the time array.
- closestship = 999; //set this variable to a rediculously high number, that could not be
- //the shortest time.
- if(closestship > time[i]) //if the closestship variable is greater than the time to get to
- closestship = i; //the ship we are currently looking at, set it to that number.
- }
- }
- void findmeetingpoint(int coord[][2] ,
- int speed[] ,
- double time[] ,
- int meetpoint[],
- int closestship,
- int inneed)
- {
- int i; //used for a loop
- /*
- ** In the following mathematic actions, it wil calculate the best meeting poit for the 2 ships.
- ** It will do this by a modified midpoint formula. (m = x1 * x2 / 2 , m = y1 * y2 /2)....
- ** It will find the x coordinate by taking the x coordinate of the ship in need, multiplying
- ** it by the speed of the other ship, and adding that to the x coordinate of the other ship multiplied
- ** by the speed of the ship in need. The sum of all this will be divided by the 2 combined speeds,
- ** to get the x coordinate. The same arythmetic will be done to find the y coordinate, but the x's
- ** replaced with y's.
- */
- meetpoint[0] = ( (coord[inneed][0] * speed[closestship]) + (coord[closestship][0] * speed[inneed]) )
- / (speed[inneed] + speed[closestship]);
- meetpoint[1] = ( (coord[inneed][1] * speed[closestship]) + (coord[closestship][1] * speed[inneed]) )
- / (speed[inneed] + speed[closestship]);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement