Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

For this program you will be printing of a grid of 10 points (x, y). You will be

ID: 3592206 • Letter: F

Question

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 <stdio.h>

#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 <infile> <outfile> ");
       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;
}

Explanation / Answer


#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define N 10 // Do NOT change this line!
/*
   Structure definition should go here.
*/
typedef struct point2d
{
    int x;
    int y;
}point2d;
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.
*/
   FILE* fp = fopen(str, "r");
   if(fp == NULL)
       printf("File cannot be opened. ");
   else
   {
       int i = 0;
       while(!feof(fp))
       {
          int x, y;
          fscanf(fp, "%d%d", &x, &y);
          P[i].x = x;
          P[i].y = y;
          i++;
       }
   }
   fclose(fp);
}

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...
*/
   return sqrt((p.x - q.x) * (p.x - q.x) + (p.y - q.y) * (p.y - q.y));

}

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
int minDist = getdist(P[0], P[1]);
point2d point1 = P[0], point2 = P[1];
for(int i = 0; i < N; i++)
{
    for(int j = i+1; j < N; j++)
    {
       int dist = getdist(P[i], P[j]);
       if(dist < minDist)
       {
          minDist = dist;
          point1 = P[i];
          point2 = P[j];
       }
    }
}

// set values in G to unique values
G[point1.x][point1.y] = 1;
G[point2.x][point2.y] = 1;


}

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
for(int i = 0; i < 2*N; i++)
   for(int j = 0; j < 2*N; j++)
       G[i][j] = 0;
for(int i = 0; i < N; i++)
{
    G[P[i].x][P[i].y] = 2;
}
   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.
*/
   FILE *fp = fopen(str, "w");
   for(int i = 0; i < N; i++)
       fprintf(fp, "%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!
*/
   FILE *fp = fopen(str, "a");
   fprintf(fp, " ");
   for(int i = 0; i < 50; i++)
       fprintf(fp, "-");
   fprintf(fp, " ");
   for(int i = 0; i < 2*N; i++)
   {
      for(int j = 0; j < 2*N; j++)
          if(G[i][j] == 0)
              fprintf(fp, " ");
          else if(G[i][j] == 1)
              fprintf(fp, "X ");
          else
              fprintf(fp, "* ");
      fprintf(fp, " ");            
   }
    
   for(int i = 0; i < 50; i++)
       fprintf(fp, "-");
   fprintf(fp, " ");
}


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 <infile> <outfile> ");
       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;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote