Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <math.h>
- //float distance(float coordiateArray[]);
- struct coordinates{
- //The label of the coordinates
- char coordinatePlace[32];
- //The x and y coordinates
- float xCoord;
- float yCoord;
- } coordinate;
- //Function to calculate distance from another point
- float distance(char p1[32], char p2[32], float x1, float x2, float y1, float y2){
- float c;
- c = sqrt( pow((x1-x2),2) + pow((y1-y2),2) );
- printf("distance between %s and %s is: %f", p1, p2, c);
- return 0;
- }
- int main (int argc, char *argv[]) {
- //the first thing the user must ask is how many coordinates will be entered.
- int ENTRIES = atoi(argv[1]);
- //Since multiple entries will be made you must create an array of that "struct" type.
- struct coordinates myCoordinates[ENTRIES];
- //repeat asking for for the label, x coordinate and y coordinate until you have number of coordinate pairs and labels you specified.
- //*** ask for the data in THIS SPECIFIC ORDER to facilitate your and our automated testing explained below.
- for(int i = 0; i<ENTRIES; i++){
- //the second thing requested should be a label of the coordinates
- printf("Enter a Coordinate name: ");
- scanf("%s", &*myCoordinates[i].coordinatePlace);
- //then ask for the x coordinate
- printf("Enter a Coordinate value for x: ");
- scanf("%f", &myCoordinates[i].xCoord);
- //then ask for the y coordinate
- printf("Enter a Coordinate value for y: ");
- scanf("%f", &myCoordinates[i].yCoord);
- }
- printf("\n");
- for (int i = 0; i < ENTRIES; i++) {
- printf("%s %.2f %.2f\n", myCoordinates[i].coordinatePlace,myCoordinates[i].xCoord, myCoordinates[i].yCoord);
- }
- for(int i = 0; i <= ENTRIES-1; i++){
- for (int j = 0; j <= ENTRIES-1; j++) {
- char *p1,*p2;
- float x1,x2,y1,y2;
- p1 = myCoordinates[i].coordinatePlace;
- x1 = myCoordinates[i].xCoord;
- y1 = myCoordinates[i].yCoord;
- p2 = myCoordinates[j].coordinatePlace;
- x2 = myCoordinates[j].xCoord;
- y2 = myCoordinates[j].yCoord;
- if(i==j){
- continue;
- }
- else{
- printf("\n%s %.2f %.2f", p1, x1, y1);
- printf("\n%s %.2f %.2f", p2, x2, y2);
- printf("\n");
- distance(p1, p2, x1, x2, y1, y2);
- }
- }
- }
- }
- /*
- - Once all of the coordinates and labels are entered, calculate the distance between each set of points
- (you DO NOT have to store all of the distances!!!). In this lab you may use "math.h" (it may be useful).
- - Determine which pair of coordinates are closest to each other out of all of the pairs.
- - Determine which pair of coordinates are furthest from each other out of all of the pairs.
- EXTRA non-C-programming requirements:
- 1. There will be a 10 point value for efficiency. Please contemplate the BEST way to do this MOST efficiently.
- Brute-force is fine, but do better somehow!!!
- Explain what you did to make your code run more efficiently...
- think about how you can minimize the number of tests,
- minimize the iterations in a loop or use an intelligent function to make your code run better.
- Tell me how you did it in the comments box in Canvas.
- 2. Entering ALL that data is a LOT of work.
- Create an input file called "input.txt" that will allow you to automatically
- use input redirection "<" to help you enter the data without typing in the data from STDIN (your keyboard).
- This would be done on the BASH command line...not in your C code... include input.txt in your submission.
- 3. Print out ALL of the data points and their distances to the other data points.
- Then print out the closest and furthest at the end of the report.
- Send this all to a file called "output.txt" not STDOUT (your screen).
- This would be done on the BASH command line...not in your C code... include output.txt in your submission.
- 4. In the comments section in Canvas include the command you used to accept data from
- something other than STDIN and how you sent output to something other than STDOUT.
- ***When grading we will use your input file and the command you used
- and see if the results we get match the output in your output file.
- We will then use our own input file to see if the output matches our expected output,
- so we will do 2 tests on your code with 2 different sets of input.
- Data scrubbing
- 1. Coordinates for both x and y should be positive REAL numbers between 1 and 100.
- Check that this is true for each number entered.
- 2. A text label must be entered for each set of coordinates
- and the label CANNOT be used more than once (although a pair of coordinates CAN be used more than once!!!).
- Make sure the same label is NEVER used twice.
- 3. The number of coordinates to be entered MUST be a number greater than 1 (obviously)...
- check to make sure this is true.
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement