Advertisement
jTruBela

6

Mar 28th, 2022 (edited)
906
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.10 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <math.h>
  4.  
  5. //float distance(float coordiateArray[]);
  6.  
  7. struct coordinates{
  8.     //The label of the coordinates
  9.     char coordinatePlace[32];
  10.     //The x and y coordinates
  11.     float xCoord;
  12.     float yCoord;
  13.  
  14. } coordinate;
  15.  
  16. //Function to calculate distance from another point
  17. float distance(char p1[32], char p2[32], float x1, float x2, float y1, float y2){
  18.     float c;
  19.  
  20.     c = sqrt( pow((x1-x2),2) + pow((y1-y2),2) );
  21.  
  22.     printf("distance between %s and %s is: %f", p1, p2, c);
  23.     return 0;
  24. }
  25.  
  26. int main (int argc, char *argv[]) {
  27.     //the first thing the user must ask is how many coordinates  will be entered.
  28.     int ENTRIES = atoi(argv[1]);
  29.     //Since multiple entries will be made you must create an array of that "struct" type.
  30.     struct coordinates myCoordinates[ENTRIES];
  31.  
  32.     //repeat asking for for the label, x coordinate and y coordinate until you have number of coordinate pairs and labels you specified.
  33.     //*** ask for the data in THIS SPECIFIC ORDER to facilitate your and our automated testing explained below.
  34.     for(int i = 0; i<ENTRIES; i++){
  35.         //the second thing requested should be a label of the coordinates
  36.         printf("Enter a Coordinate name: ");
  37.         scanf("%s", &*myCoordinates[i].coordinatePlace);
  38.         //then ask for the x coordinate
  39.         printf("Enter a Coordinate value for x: ");
  40.         scanf("%f", &myCoordinates[i].xCoord);
  41.         //then ask for the y coordinate
  42.         printf("Enter a Coordinate value for y: ");
  43.         scanf("%f", &myCoordinates[i].yCoord);
  44.     }
  45.     printf("\n");
  46.     for (int i = 0; i < ENTRIES; i++) {
  47.         printf("%s %.2f %.2f\n", myCoordinates[i].coordinatePlace,myCoordinates[i].xCoord, myCoordinates[i].yCoord);
  48.     }
  49.  
  50.  
  51.     for(int i = 0; i <= ENTRIES-1; i++){
  52.         for (int j = 0; j <= ENTRIES-1; j++) {
  53.  
  54.             char *p1,*p2;
  55.             float x1,x2,y1,y2;
  56.  
  57.             p1 = myCoordinates[i].coordinatePlace;
  58.             x1 = myCoordinates[i].xCoord;
  59.             y1 = myCoordinates[i].yCoord;
  60.             p2 = myCoordinates[j].coordinatePlace;
  61.             x2 = myCoordinates[j].xCoord;
  62.             y2 = myCoordinates[j].yCoord;
  63.  
  64.             if(i==j){
  65.                 continue;
  66.             }
  67.             else{
  68.                 printf("\n%s %.2f %.2f", p1, x1, y1);
  69.                 printf("\n%s %.2f %.2f", p2, x2, y2);
  70.                 printf("\n");
  71.                 distance(p1, p2, x1, x2, y1, y2);
  72.             }
  73.         }
  74.     }
  75.  
  76. }
  77.  
  78. /*
  79.  
  80.  
  81.  
  82.  
  83. - Once all of the coordinates and labels are entered, calculate the distance between each set of points
  84.     (you DO NOT have to store all of the distances!!!).  In this lab you may use "math.h" (it may be useful).
  85.  
  86. - Determine which pair of coordinates are closest to each other out of all of the pairs.
  87.  
  88. - Determine which pair of coordinates are furthest from each other out of all of the pairs.
  89.  
  90.  
  91. EXTRA non-C-programming requirements:
  92.  
  93. 1. There will be a 10 point value for efficiency. Please contemplate the BEST way to do this MOST efficiently.
  94.     Brute-force is fine, but do better somehow!!!
  95.         Explain what you did to make your code run more efficiently...
  96.         think about how you can minimize the number of tests,
  97.         minimize the iterations in a loop or use an intelligent function to make your code run better.
  98.         Tell me how you did it in the comments box in Canvas.
  99.  
  100. 2. Entering ALL that data is a LOT of work.
  101.     Create an input file called "input.txt" that will allow you to automatically
  102.         use input redirection "<" to help you enter the data without typing in the data from STDIN (your keyboard).
  103.         This would be done on the BASH command line...not in your C code... include input.txt in your submission.
  104.  
  105. 3. Print out ALL of the data points and their distances to the other data points.
  106.     Then print out the closest and furthest at the end of the report.
  107.     Send this all to a file called "output.txt" not STDOUT (your screen).
  108.     This would be done on the BASH command line...not in your C code... include output.txt in your submission.
  109.  
  110. 4. In the comments section in Canvas include the command you used to accept data from
  111.     something other than STDIN and how you sent output to something other than STDOUT.
  112.  
  113. ***When grading we will use your input file and the command you used
  114.     and see if the results we get match the output in your output file.
  115.         We will then use our own input file to see if the output matches our expected output,
  116.         so we will do 2 tests on your code with 2 different sets of input.
  117.  
  118.  
  119.  
  120. Data scrubbing
  121.  
  122. 1. Coordinates for both x and y should be positive REAL numbers between 1 and 100.
  123.     Check that this is true for each number entered.
  124.  
  125. 2. A text label must be entered for each set of coordinates
  126.     and the label CANNOT be used more than once (although a pair of coordinates CAN be used more than once!!!).
  127.     Make sure the same label is NEVER used twice.
  128.  
  129. 3. The number of coordinates to be entered MUST be a number greater than 1 (obviously)...
  130.     check to make sure this is true.
  131. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement