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

C Program Data Structures Include the following struct definition at the top of

ID: 3849132 • Letter: C

Question

C Program Data Structures

Include the following struct definition at the top of your program:

struct data {

            int x;

            int y;

};

File contains:

4 1

1 2

6 8

0 1

9 7

struct data* read_data(char *file, int size);

Parameter: size – file and number of integer couples to be read

Return: A pointer to an array where the structure data is stored

This function will dynamically create an array to hold the x and y data and read the the file to load it.

double distance(point *first, point *second);

Parameter: first- a point

Parameter: second-a point

return:the distance between the two points

formula: sqrt((x1-x2)^2 +(y1-y2)^2))

struct data* sort(struct data* array, int size);

Parameter: array – An array of data

Parameter: size – The size of the array

Return: A pointer to the sorted array

The points should be sorted based on their distance from (0,0) on a graph. Greatest to least distance

void print_data(struct data* array, int size);

Parameter: array – An array of points

Prints out all of the points in the array, example output shown below

int main(int argc, char *argv[]);

Main should take in the number of data sets (x and y) to be read. It should then call sort the array and print it out.

No dot operator/no aray notation ONLY POINTERS

Example output

$ ./a.out 5 input2.txt

Data sorted:

0 1

1 2

4 3

6 8

9 7

Explanation / Answer

C Program:

#include<stdio.h>
#include<stdlib.h>
#include<math.h>

//Struct definition
struct data {
        int x;
   int y;
};

//Reading data from file
struct data* read_data(char *file, int size)
{
   FILE *fp;
   int i;  

   struct data *points;
   struct data temp;

   //Allocating memory to structure array
   points = (struct data*)malloc(sizeof(struct data) * size);
  
   //Opening file in read mode
   fp = fopen(file, "r");

   //Reading size number of elements
   for(i=0; i<size; i++)
   {
       //Reading data into variables
       fscanf(fp, "%d %d", &temp.x, &temp.y);
       //Storing in array
       points[i] = temp;
   }  
  
   //Closing file
   fclose(fp);

   return points;
}

//Sorting array
struct data* sort(struct data* array, int size)
{
   struct data temp;
   int i, j;

   float distance1, distance2;

   //Iterating over outer array
   for(i=0; i<size; i++)
   {
       //Iterating over remaining array
       for(j=0; j<i; j++)
       {
           //Calculating distance
           distance1 = sqrt( (array[i].x * array[i].x) + (array[i].y * array[i].y) );
           distance2 = sqrt( (array[j].x * array[j].x) + (array[j].y * array[j].y) );
          
           //comparing distances
           if(distance1 > distance2)
           {
               //Swapping elements
               temp = array[i];
               array[i] = array[j];
               array[j] = temp;
           }
       }
   }

   //Return array
   return array;  
}

//Printing struct array
void print_data(struct data* array, int size)
{
   int i;
  
   printf(" Data Sorted: ");

   //Iterating over data points
   for(i=0; i<size; i++)
   {
       //Printing each point
       printf(" %d %d ", array[i].x, array[i].y);  
   }  

   printf(" ");
}

//Main function
int main(int argc, char *argv[])
{
   int size;
   char *file;

   struct data *points;

   //Reading size
   size = atoi(argv[1]);

   //Reading file name
   file = argv[2];  
  
   //Reading points
   points = read_data(file, size);

   //Sorting data
   points = sort(points, size);

   //Printing data
   print_data(points, size);      
}