[solved]-Working Assignment Following Code Code Issue Issue Stated Bottom Post Assignment Read Text Q39003272
I am working on this assignment for the following, Ihave the code, but the code has an issue. Issue is stated at thebottom of the post
In this assignment, you have to read a text file (in.txt) thatcontains a set of point coordinates (x, y). The first line of thefile contains the number of points (N) and then each line of thefile contains x and y values that are separated by space. The valueof x and y are integer. They can be both negative and positivenumbers. You have to sort those points in x-axis major order (itmeans it will be sorted based on x first, if multiple point hassame x, then it will sort based on y) and then write the outputinto another file (out.txt) in the same format, i.e., each linecontains one coordinate point x and y and they are space delimited.After writing the sorted points in the file, your program shoulddisplay the message “sorted and output written”. After that yourprogram should continuously prompt the user for an input coordinatepoint x and y values and your program should perform binary searchto determine whether the point exists in the given list of points.If it is found, it should show “Found” and record number. Theprogram will stop prompting for x and y, if the search input x=-999and y=-999

the code I am using is the following:
#include <stdio.h>
#include <stdlib.h>
struct Point{
int x,y;
};
void merge(struct Point *points, int l, int m, int r) {
int i, j, k;
int n1 = m – l + 1;
int n2 = r – m;
struct Point lpoints[n1], rpoints[n2];
/* Copy data to temp arrays L[] and R[] */
for (i = 0; i < n1; i++)
lpoints[i] = points[l + i];
for (j = 0; j < n2; j++)
rpoints[j] = points[m + 1+ j];
/* Merge the temp arrays back into arr[l..r]*/
i = 0; // Initial index of first subarray
j = 0; // Initial index of second subarray
k = l; // Initial index of merged subarray
while (i < n1 && j < n2)
{
// x point is less than
if(lpoints[i].x < rpoints[j].x){
points[k] = lpoints[i];
i++;
}
// x point is greater than
else if(lpoints[i].x > rpoints[j].x){
points[k] = rpoints[j];
j++;
}
else {// x points are equal then compare y point.
if(lpoints[i].y <= rpoints[j].y){
points[k] = lpoints[i];
i++;
}else if(lpoints[i].y > rpoints[j].y){
points[k] = rpoints[j];
j++;
}
}
k++;
}
// copy the rest of elements.
while (i < n1)
{
points[k] = lpoints[i];
i++;
k++;
}
while (j < n2)
{
points[k] = rpoints[j];
j++;
k++;
}
}
void mergeSort(struct Point *points, int l, int r) {
if (l<r) {
// get midpoint
int m = (l+r)/2;
//Sort two arrays
mergeSort(points, l,m);
mergeSort(points, m+1,r);
merge(points, l,m,r);
}
}
int binarySearch(struct Point points[], int l, int r, int x, inty)
{
while (l <= r) {
int m = l + (r – l) / 2;
// check if co-ordinates exist.
if (points[m].x == x && points[m].y == y)
return m;
if (points[m].x < x)
l = m + 1;
else
r = m – 1;
}
return -1;
}
int main() {
int n,i;
FILE *fp, *fout;
// open file for reading.
fp = fopen(“in.txt”,”r”);
fscanf(fp,”%d”,&n);
struct Point *points = (struct Point*) malloc(n*sizeof(structPoint));
for( i = 0; i<n; i++) {
fscanf(fp, “%d%d”,&points[i].x, &points[i].y);
}
fclose(fp);
// call merge sort.
mergeSort(points, 0,n-1);
fout = fopen(“output.txt”,”r”);
for( i = 0; i<n; i++) {
fprintf(fout,”%d %dn”,points[i].x, points[i].y);
// printed for debugging purpose. comment if you want.
printf(“%d %dn”,points[i].x, points[i].y);
}
printf(“sorted and output writtenn”);
fclose(fout);
while(1) {
printf(“Enter the co-ordinates to search: “);
int x1, y1;
scanf(“%d%d”, &x1, &y1);
if (x1 == -999 && y1 == -999) {
break;
}
// perform binary search.
int result = binarySearch(points, 0, n-1, x1,y1);
if (result != -1){
printf(“Foundn”);
}
else
printf(“Not Foundn”);
}
}
When I enter “2 7” it is shown as not found, when itshould be found please help!
Sample Input file in.txt: 100 2 99 -22 Uvw -52 Sample output file out.txt: -5 2 25 27 4 6 63 99 -22 100 2 “Sorted and output written” Search input (x y): 2 3 Output: Not found Search input (x y): 4.6 Output: Found at record 4 Search input (x y): 27 Output: Found at record 3 Search input (x y): 25 Output: Found at record 2 Search input (x y): -999 -999 Output: exit Show transcribed image text Sample Input file in.txt: 100 2 99 -22 Uvw -52 Sample output file out.txt: -5 2 25 27 4 6 63 99 -22 100 2 “Sorted and output written” Search input (x y): 2 3 Output: Not found Search input (x y): 4.6 Output: Found at record 4 Search input (x y): 27 Output: Found at record 3 Search input (x y): 25 Output: Found at record 2 Search input (x y): -999 -999 Output: exit
Expert Answer
Answer to I am working on this assignment for the following, I have the code, but the code has an issue. Issue is stated at the bo… . . .
OR

