C language Use the code below. For this program you will be printing of a grid o
ID: 3591397 • Letter: C
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 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
Explanation / Answer
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#define N 10
struct val1
{
int x,y;
};
struct val1 typedef val;
void fill(char *str, val* p)
{
FILE * fp; char ch;
fp=fopen(str,"rb");
if(fp == NULL)
{
printf("file do not exist or file name is invalid!! ");
exit(0);
}
int i=0;int tempx;
for(i=0;i<N;i++)
{
fread(&p[i].x,2,1,fp);
printf("x=%d",p[i].x);
fread(&p[i].y,2,1,fp);
printf("y=%d",p[i].y);
}
fclose(fp);
}
int getdistance(val p,val q)
{
int dist=0;
dist=(sqrt(pow(q.x-p.x,2)+pow(q.y-p.y,2)));
return dist;
}
void closest(val *p,int g[2*N][2*N])
{
int min=-1;int xp,yp;int i,j;
for(i=0;i<N;i++)
{
for(j=0;j<N;j++)
{
int dist1=getdistance(p[i],p[j]);
if(dist1<min)
{
min=dist1;
xp=i;
yp=j;
}
}
}
//g[p[xp].x][p[xp].y]=2;
//g[p[yp].x][p[yp].y]=2;
}
void grid(val* 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.
*/
int i;
for(i=0;i<N;i++)
{
//g[p[i].x][p[i].y]=1;
}
// set G for each of the N points
closest(p,g);
}
void printpoints(char *str, val *p)
{
FILE *fp;int i;
fp=fopen(str,"wb");
for( i=0;i<N;i++)
{
printf("%d: (%d , %d) ",i,p[i].x,p[i].y);
fwrite(&p[i].x,2,1,fp);
fwrite(&p[i].y,2,1,fp);
}
fclose(fp);
}
void printgridxy(char *str, int g[2*N][2*N])
{
/*
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 i,j;
for(i=0;i<N;i++)
{
for(j=0;j<N;j++)
{
if(g[i][j]==1)
printf("* ");
else if(g[i][j]==2)
printf("X ");
else
printf(" ");
}
}
}
int main(int argc, char *argv[])
{
if (argc != 3)
{
printf("Syntax Error: ./a.out <infile> <outfile> ");
exit(1);
}
val p[N];
int g[2*N][2*N] = {0};
fill(argv[1], p);
grid(p, g);
printpoints(argv[2], p);
printgridxy(argv[2], g);
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.