Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Please modify my program so it outputs like this: (I need to create a space after each comma) Example running: ---------------------------------------------------------------------- $ ./a.out points.txt output.txt

Please modify my program so it outputs like this: (I need to create a space after each comma)

Example running: ---------------------------------------------------------------------- $ ./a.out points.txt output.txt

Example input file: (will contain exactly 10 points) 7 19 11 5 15 11 4 10 1 8 10 4 2 5 14 12 10 9 12 4

image text in transcribed

My program:

Usage: gcc -lm

./a.out

CODE:

#include #include #include #define N 10 // Do NOT change this line!

/* Structure definition should go here. */ struct point2d { int x; int y; };

void fill(char str[], struct point2d P[]) { /* This function opens and reads from the input file. You should close the file when you are done with it. Read the points into the array of structures in this function.

Don't forget to check if the file name is valid. */ FILE* fin; fin = fopen(str, "r"); for(int i=0;i { fscanf(fin,"%d",&P[i].x); fscanf(fin,"%d",&P[i].y); }

}

int getdist(struct point2d p,struct point2d q) { /* This function gets the distance between 2 points and returns that value.

Yes, you need to do some math here... */

return sqrt((p.x-q.x)*(p.x-q.x)+(p.y-q.y)*(p.y-q.y));

}

void closest(struct point2d P[], int G[2*N][2*N]) { /* This function finds the 2 points that are the closest.

You should call the getdist() function from inside here. */

// find the 2 points that are closest // p1 and p2 are supposed to be closest points int dmin=getdist(P[0],P[1]); struct point2d p1=P[0]; struct point2d p2=P[1]; int temp; for(int i=0;i { for(int j=i+1;j { temp=getdist(P[i],P[j]); if(temp { dmin=temp; p1=P[i]; p2=P[j]; } } }

// set values in G to unique values // setting -2 for min values in grid G[p1.x][p1.y]=-2; G[p2.x][p2.y]=-2;

}

void grid(struct point2d P[], int G[2*N][2*N]) { /* This function will transfer all the points from your structure into a 2D array used as the grid.

You will call the closest() function from inside here. */

// set G for each of the N points // setting -1 in grid if there is a point for (int i=0;i { G[P[i].x][P[i].y]=-1; }

closest(P, G); }

void printpoints(char str[],struct point2d P[]) { /* This function will print off the list of all the points you have in the following form:

num: ( x, y)

ex. 0: ( 4, 1)

** Note the spacing!

You need to open the output file here and write to it. */ FILE* fout; fout = fopen(str, "a"); for(int i=0;i { fprintf(fout,"%d: ( %d, %d) ",i,P[i].x,P[i].y); }

}

void printgridxy(char str[], int G[2*N][2*N]) {

/* This function will print out your 2D array (the grid)

Use ' ' for spots without a point, use '*' for spots with a point, use 'X' for the 2 points that are closest.

Put 1 space between each element, for example you should print "* " instead of just "*".

You should also have a top and bottom made of 50 hyphens (-)

This should be printed to the same file as the points were. Be careful not to overwrite the file! */ // appending to the end of the file FILE* fout; fout = fopen(str, "a"); for(int i=0;i

for(int i=0;i

for(int i=0;i

}

int main(int argc, char *argv[]) { /* Do not change anything in main! Also do not change the #define at the top of the program.

There is one exception to this, you may change the "type" for the structure you made if you don't use my naming scheme. */ if (argc != 3) { printf("Syntax Error: ./a.out "); exit(1); }

struct point2d P[N]; //

fill(argv[1], P); grid(P, G); printpoints(argv[2], P); printgridxy(argv[2], G);

return 0; }

Example running: $ ./a.out points.txt output.txt Example output file: 0: (7, 19) 2: (15, 11) 3: 4, 10) 5: (10, 4) 6: (2, 5) 7: 14, 12) 8: (10, 9) 9: (12, 4) Example running: $ ./a.out points.txt output.txt Example output file: 0: (7, 19) 2: (15, 11) 3: 4, 10) 5: (10, 4) 6: (2, 5) 7: 14, 12) 8: (10, 9) 9: (12, 4)

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions