Problem Scenario

You are in a monster city to capture monsters. It is of most importance for you to be aware of the locations of the nearby monsters. Keeping track of this information is known as "monster tracing." You need to write a small program for monster tracing to show your coding skills!

The monster city can be modeled on the Cartesian plane. You are located at the point (x, y). In addition, you have the Cartesian coordinates of all monsters around you in the city. What you would like to do is write a program that sorts these locations based on their distance from you, followed by handling queries. The queries are of the form of a point you are thinking of visiting. Your program should identify if there is a monster at that location, and if so, what their rank is on the sorted list of the monsters. If no monster is at that location, you should correctly identify this.

Implementation Restrictions

1. You must use a specified combination of Merge Sort and Insertion Sort to sort the point data. Specifically. for each input case, a threshold value, t, will be given. If the subsection of the array to sort has t or fewer values to sort, Insertion Sort should be used. Otherwise, Merge Sort should be used. Further details about the comparison used for the sorting are below.

2. You must store your coordinates in a struct that contains two integer fields. You can add more fields if needed.

3. You must implement a ReadData() function that reads the required data from the in.txt file and return the points to be sorted.

4. You must write a function compareTo which takes in two pointers, ptrPt1 and ptrPt2, to coordinate structs and returns a negative integer if the point pointed to by ptrPtl is closer to you than the point pointed to by ptrPt2, 0 if the two locations pointed to by both are identical locations, and a positive integer if the point pointed to by ptrPt1 is farther from you than the point pointed to by ptrPt2. Exceptions to this will be when the two pointers are pointing to points that are the same distance from you, but are distinct points. In these cases, if ptrPtl's x coordinate is lower than ptrPt2's x coordinate, a negative integer must be returned. Alternatively, if ptrPtl's x coordinate is greater than ptrPt2's x coordinate a positive integer must be returned. Finally, if the x coordinate of both points is the same, if ptrPt1's y coordinate is lower than ptrPt2's y coordinate, a negative integer must be returned. If ptrPtl's y coordinate is greater than ptrPt2's y coordinate, a positive integer must be returned.

5. Since your location must be used for sorting, please make the variable that stores your x and y coordinates global. Your program should have no other global variables.

6. A Binary Search function must be used when answering queries.

7. Your sort function should take in the array to be sorted, the length of the array as well as the threshold value, t, previously mentioned. This function should NOT be recursive. It should be a wrapper function. It means it will call necessary sorting function from here.

8. The recursive sort function (you can call this mergeSort) should take in the array, a starting index into the array, an ending index into the array and the threshold value t. In this function, either recursive calls should be made OR a call to an insertion sort function should be made.

The Problem

Given your location, and the location of each monster, sort the list by distance from you from shortest to longest, breaking ties by x-coordinate (lower comes first), and then breaking those ties by y coordinate (lower comes first).

After sorting, answer several queries about points in the coordinate plane. Specifically, determine if a query point contains a monster or not. If so, determine that monster's ranking on the sorted list in distance from you.

The Input (to be read from in.txt file)

The first line of the input contains 5 integers separated by spaces. The first two of these values are x and y (|x|, |y| <= 10000), representing your location. The third integer is n (2 <= n <= 106), representing the number of monsters. The fourth integer is s (1 <= s <= 2x105), representing the number of points to search for. The last integer, t(1 <= t <= 30), represents the threshold to be used for determining whether you run Merge Sort of Insertion Sort.

The next in lines of the input contain x and y coordinate values, respectively, separated by spaces, representing the locations of monsters. Each of these values will be integers and the points will be distinct (and also different from your location) and the absolute value of x and y for all of these coordinates will not exceed 10,000. Then the next s lines of the file contain x and y coordinate values for searching. Both values on each line will be integers with an absolute value less than or equal to 10,000.

The Output (to be printed to console as well as to out.txt file)

The first n lines of output should contain the coordinates of the monsters, sorted as previously mentioned. These lines should have the x-coordinate, followed by a space, followed by the y-coordinate.

The last s lines of output will contain the answers to each of the s queries in the input. The answer for a single query will be on a line by itself. If the point queried contains a monster, output a line with the following format:

x y found at rank R

where (x, y) is the query point, and R is the one-based rank of that monster in the sorted list. (Thus, R will be 1 more than the array index in which (x, y) is located, after sorting.)

If the point queried does NOT contain a monster, output a line with the following format:

x y not found

Sample Input data in in.txt file (Note: Query points in blue for clarity.)

0 0 14 5 5
3 1
-6 -2
-4 3
4 -4
2 4
-1 3
2 2
0 -5
-4 -2
-6 6
4 4
-2 4
0 5
-4 6
2 -1
3 1
0 -5
0 5
-6 7

Sample Output (out.txt)

2 2
-1 3
3 1
-4 -2
-2 4
2 4
-4 3
0 -5
0 5
4 -4
4 4
-6 -2
-4 6
-6 6
2 -1 not found
3 1 found at rank 3
0 -5 found at rank 8
0 5 found at rank 9
-6 7 not found

Additional Requirement:

You must have to read data from in.txt file and write the result to console and out.txt file. You have to use Merge sort, insertion sort, and binary search for your solution based on the requirement. Without using them, you will get zero. The output must have to match with the sample output format. Do not add additional space, extra characters or words with the output as we will use diff command to check whether your result matches with our result. Next, you must have to write a well structure code.

Academic Honesty!
It is not our intention to break the school's academic policy. Posted solutions are meant to be used as a reference and should not be submitted as is. We are not held liable for any misuse of the solutions. Please see the frequently asked questions page for further questions and inquiries.
Kindly complete the form. Please provide a valid email address and we will get back to you within 24 hours. Payment is through PayPal, Buy me a Coffee or Cryptocurrency. We are a nonprofit organization however we need funds to keep this organization operating and to be able to complete our research and development projects.