w do I correct this into the correct format: The input begins with the number of locations. Each location is entered x and then y and then name. Output the name of the closest locations.

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question
100%

c++

How do I correct this into the correct format:

The input begins with the number of locations. Each location is entered x and then y and then name. Output the name of the closest locations.

 

Here is my code:

#include <bits/stdc++.h>
#include <cmath>
using namespace std;

//Compute the distance between two points (x1, y1) and (x2 y2)
double getDistance(double x1, double y1, double x2, double y2)
{
  return sqrt ((x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1));
}

struct Location
{
  string name;
  double x;
  double y;
};

int main() 
{
  int y = 0;
  int locationName = 40;
 
  const int NUMBER_OF_POINTS = 5;

  // Each row in points represents a location
  Location location[NUMBER_OF_POINTS];
    
    // make sure to enter the name of the location so like
    // longview 237.89 46.68
    // dallas 373.62 47.0

  cout << "Enter " << NUMBER_OF_POINTS << " points: ";
  for (int i = 0; i < NUMBER_OF_POINTS; i++)
    cin >> location[i].name >> location[i].x >> location[i].y; // allow the user to enter name by adding ">> location[i].name"

  //p1 and p2 are the indices in the locations array
  int p1 = 0, p2 = 1;
  double shortestDistance = getDistance(location[p1].x, location[p1].y, location[p2].x, location [p2].y);//Initialize shortestDistance
  
  
  //Compute distance for every two points
  for (int i = 0; i < NUMBER_OF_POINTS; i++)
  {
    for (int j = 0; j < NUMBER_OF_POINTS; j++)
    { 
      double distance = getDistance(location[i].x, location[i].y, location[j].x, location[j].y); //Find distance
      if (i == j) continue;
     
      if (shortestDistance > distance)
      {
        p1 = i; 
        p2 = j;
        shortestDistance = distance; //Update shortestDistance
      }
      
    }
  }

  //Display result
  cout << "The closest two locations are " << "(" << location[p1].x << ", " << location [p1].y << ") and (" << location[p2].x << "," << location[p2].y << ")" << endl;

  return 0;
}

(2 images for a better view of the format)

```cpp
#include <bits/stdc++.h>
#include <cmath>
using namespace std;

// Compute the distance between two points (x1, y1) and (x2, y2)
double getDistance(double x1, double y1, double x2, double y2) {
    return sqrt((x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1));
}

struct Location {
    string name;
    double x;
    double y;
};

int main() {
    int y = 0;
    int locationName = 40;

    const int NUMBER_OF_POINTS = 5;

    // Each row in points represents a location
    Location location[NUMBER_OF_POINTS];

    // make sure to enter the name of the location so like
    // Longview 237.89 46.68
    // Dallas 373.62 47.0

    cout << "Enter " << NUMBER_OF_POINTS << " points: ";
    for (int i = 0; i < NUMBER_OF_POINTS; i++)
        cin >> location[i].name >> location[i].x >> location[i].y; // allow the user to enter name by adding ">> location[i].name"

    // p1 and p2 are the indices in the locations array
    int p1 = 0, p2 = 1;
    double shortestDistance = getDistance(location[p1].x, location[p1].y, location[p2].x, location[p2].y); // Initialize shortestDistance
```

### Explanation:

This is a C++ program designed to calculate the distance between multiple geographic locations based on their X and Y coordinates.

- **Header Files**: The program includes standard libraries such as `<bits/stdc++.h>` and `<cmath>`, which provide a broad range of functionalities including mathematical operations.

- **Function `getDistance`**: This function calculates the Euclidean distance between two points `(x1, y1)` and `(x2, y2)`. It uses the formula:

  \[
  \text{{distance}} = \sqrt{(x2 - x1)^2 + (y2 - y1)^2}
  \]

- **Struct `Location`**: This structure holds data for a location, which includes the `name`, `x` coordinate, and `y` coordinate.

- **Main
Transcribed Image Text:```cpp #include <bits/stdc++.h> #include <cmath> using namespace std; // Compute the distance between two points (x1, y1) and (x2, y2) double getDistance(double x1, double y1, double x2, double y2) { return sqrt((x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1)); } struct Location { string name; double x; double y; }; int main() { int y = 0; int locationName = 40; const int NUMBER_OF_POINTS = 5; // Each row in points represents a location Location location[NUMBER_OF_POINTS]; // make sure to enter the name of the location so like // Longview 237.89 46.68 // Dallas 373.62 47.0 cout << "Enter " << NUMBER_OF_POINTS << " points: "; for (int i = 0; i < NUMBER_OF_POINTS; i++) cin >> location[i].name >> location[i].x >> location[i].y; // allow the user to enter name by adding ">> location[i].name" // p1 and p2 are the indices in the locations array int p1 = 0, p2 = 1; double shortestDistance = getDistance(location[p1].x, location[p1].y, location[p2].x, location[p2].y); // Initialize shortestDistance ``` ### Explanation: This is a C++ program designed to calculate the distance between multiple geographic locations based on their X and Y coordinates. - **Header Files**: The program includes standard libraries such as `<bits/stdc++.h>` and `<cmath>`, which provide a broad range of functionalities including mathematical operations. - **Function `getDistance`**: This function calculates the Euclidean distance between two points `(x1, y1)` and `(x2, y2)`. It uses the formula: \[ \text{{distance}} = \sqrt{(x2 - x1)^2 + (y2 - y1)^2} \] - **Struct `Location`**: This structure holds data for a location, which includes the `name`, `x` coordinate, and `y` coordinate. - **Main
```cpp
const int NUMBER_OF_POINTS = 5;

// Each row in points represents a location
Location location[NUMBER_OF_POINTS];

// make sure to enter the name of the location so like
// Longview 237.89 46.68
// Dallas 373.62 47.0

cout << "Enter " << NUMBER_OF_POINTS << " points: ";
for (int i = 0; i < NUMBER_OF_POINTS; i++) 
    cin >> location[i].name >> location[i].x >> location[i].y; // allow the user to enter name by adding ">> location[i].name"

// p1 and p2 are the indices in the Locations array
int p1 = 0, p2 = 1;
double shortestDistance = getDistance(location[p1].x, location[p1].y, location[p2].x, location[p2].y); // Initialize shortestDistance

// Compute distance for every two points
for (int i = 0; i < NUMBER_OF_POINTS; i++) 
{
    for (int j = 0; j < NUMBER_OF_POINTS; j++) 
    {
        double distance = getDistance(location[i].x, location[i].y, location[j].x, location[j].y); // Find distance
        if (i == j) continue;
        
        if (shortestDistance > distance)
        {
            p1 = i;
            p2 = j;
            shortestDistance = distance; // Update shortestDistance
        }
    }
}

// Display result
cout << "The closest two locations are " << "(" << location[p1].x << "," << location[p1].y << ") and (" << location[p2].x << "," << location[p2].y << ")" << endl;

return 0;
```

### Code Explanation:

- **Header and Variable Declaration:** 
  - Integer constant `NUMBER_OF_POINTS` is set to 5, determining how many location points will be processed.
  - An array `location[]` is declared to store data for these points.

- **Input Section:**
  - A prompt will ask the user to enter `NUMBER_OF_POINTS` of data points.
  - Each point is expected to have a name, an x-coordinate, and a y-coordinate.

- **Distance Calculation:**
  - Indices `p1` and `p2` are initialized to track the closest pair of points.
  - The
Transcribed Image Text:```cpp const int NUMBER_OF_POINTS = 5; // Each row in points represents a location Location location[NUMBER_OF_POINTS]; // make sure to enter the name of the location so like // Longview 237.89 46.68 // Dallas 373.62 47.0 cout << "Enter " << NUMBER_OF_POINTS << " points: "; for (int i = 0; i < NUMBER_OF_POINTS; i++) cin >> location[i].name >> location[i].x >> location[i].y; // allow the user to enter name by adding ">> location[i].name" // p1 and p2 are the indices in the Locations array int p1 = 0, p2 = 1; double shortestDistance = getDistance(location[p1].x, location[p1].y, location[p2].x, location[p2].y); // Initialize shortestDistance // Compute distance for every two points for (int i = 0; i < NUMBER_OF_POINTS; i++) { for (int j = 0; j < NUMBER_OF_POINTS; j++) { double distance = getDistance(location[i].x, location[i].y, location[j].x, location[j].y); // Find distance if (i == j) continue; if (shortestDistance > distance) { p1 = i; p2 = j; shortestDistance = distance; // Update shortestDistance } } } // Display result cout << "The closest two locations are " << "(" << location[p1].x << "," << location[p1].y << ") and (" << location[p2].x << "," << location[p2].y << ")" << endl; return 0; ``` ### Code Explanation: - **Header and Variable Declaration:** - Integer constant `NUMBER_OF_POINTS` is set to 5, determining how many location points will be processed. - An array `location[]` is declared to store data for these points. - **Input Section:** - A prompt will ask the user to enter `NUMBER_OF_POINTS` of data points. - Each point is expected to have a name, an x-coordinate, and a y-coordinate. - **Distance Calculation:** - Indices `p1` and `p2` are initialized to track the closest pair of points. - The
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 2 steps with 1 images

Blurred answer
Knowledge Booster
Datatypes
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education