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

Can someone edit my code so that it calcualates the perimeter of polygon? The po

ID: 643850 • Letter: C

Question

Can someone edit my code so that it calcualates the perimeter of polygon? The points of the polygon will be stored in a file and it will be on the command line arguements. The file is a binary file containing integers. The first integer in the file is the number of points contained in the file. The rest of the integers are points with the 1st being the x coordinate and the 2nd being y. It MUST use struct. Heres example

txtfile.txt

1

23 32

12 43


1.   ./perimeter.out test.txt

      The perimeter is 101

#include
#include
#include
#include
#include

int main(int argc, char* argv[])
{
   FILE* txtFile = NULL; // File Pointer

   int fileNum, i, j;
   double mis = 0;

   typedef struct points_struct
   {
   int x;
   int y;
   } points;

   txtFile = fopen(argv[1], "rb");
   fread(&fileNum, sizeof(int), 1, txtFile);

   points p1;
   points p2;
   points temp;  

   printf("%d total", fileNum);

   fread(& p1.x, sizeof(int), 1, txtFile);
   fread(& p1.y, sizeof(int), 1, txtFile);

   p1.x = p2.x;
   p1.y = p2.y;

   for (i = 0; i < fileNum; i++)
   {
      fread(&p2.x, sizeof(int), 1, txtFile);
      fread(&p2.y, sizeof(int), 1, txtFile);
      mis += length(p1, p2);
       }

   // What goes here?
  


   mis+=length(temp, p2);

   fclose(txtFile);

   printf("The perimeter is %.2lf", mis);

   return 0;
}

double length(points p1,points p2)
{
   double mis;
   mis = sqrt(pow((p1.x - p2.x), 2, 0) + pow((p1.y - p2.y), 2, 0));

   return mis;

}

Explanation / Answer

1
23 32
12 43
1.   ./perimeter.out test.txt
      The perimeter is 101
#include
#include
#include
#include
#include
int main(int argc, char* argv[])
{
   FILE* txtFile = NULL; // File Pointer
   int fileNum, i, j;
   double mis = 0;
   typedef struct points_struct
   {
   int x;
   int y;
   } points;
   txtFile = fopen(argv[1], "rb");
   fread(&fileNum, sizeof(int), 1, txtFile);
   points p1;
   points p2;
   points temp;
   printf("%d total", fileNum);
   fread(& p1.x, sizeof(int), 1, txtFile);
   fread(& p1.y, sizeof(int), 1, txtFile);
   p1.x = p2.x;
   p1.y = p2.y;
   for (i = 0; i < fileNum; i++)
   {
      fread(&p2.x, sizeof(int), 1, txtFile);
      fread(&p2.y, sizeof(int), 1, txtFile);
      mis += length(p1, p2);
   p1 = p2; // assign p1 to p2 so that next time p2 will have new point to read.
    }
   fclose(txtFile);
   printf("The perimeter is %.2lf", mis);
   return 0;
}
double length(points p1,points p2)
{
   double mis;
   mis = sqrt(pow((p1.x - p2.x), 2, 0) + pow((p1.y - p2.y), 2, 0));
   return mis;
}