Question
C language Use the code below. For this program you will be printing of a grid of 10 points (x, y). You will be reading
C language
Use the code below.
For this program you will be printing of a grid of 10 points (x, y).
You will be reading the points from a file, and printing the points
and grid to a file.
Both files will be specified in the command-line arguments.
** Your output in the file must match mine EXACTLY. **
The grid will be 20x20, which means all points will fall within
the range 0 <= x <= 19.
You will also be tasked to find the 2 points that are closest to
each other. These 2 points will be marked by a different character
in your grid.
You should use the following functions:
fill()
getdist()
closest()
grid()
printpoints()
printgridxy()
You will read the points from the file into an array of structures.
One structure should contain an x-value and a y-value.
Follow all instructions in the program.
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
#include#define N 10 // Do NOT change this line! /* Structure definition should go here. */ void fill(char str[], 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. */ } int getdist(point2d p, point2d q) { /* This function gets the distance between 2 points and returns that value. Yes, you need to do some math here... */ } void closest(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 // set values in G to unique values } void grid(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 closest(P, G); } void printpoints(char str[], 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. */ } 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! */ } 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); } point2d P[N]; // <-- This is the only line you are allowed to change. int G[2*N][2*N] = {0}; fill(argv[1], P); grid(P, G); printpoints(argv[2], P); printgridxy(argv[2], G); return 0; }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started