C++ calculate distance between structure of inputed and saved points and the origin, print out list according on distance

The task is to be able to create a structure that can save different points, then calculate the distance between the origin point (0,0) and the input points, after that the user should have the possibility to print a list of the points he input sorted according on the distance between origin and point, the last option should be print the nearest point.

I was able to create the structure function, the distance function and the list, unfortunately I'm not able to sort it and to use the function distance to calculate the actual distances.

Hope someone can help me!


#include<iostream>
#include<cmath>

using namespace std;

double distanceCalculate(double x1, double x2, double y1, double y2)
{
double x = x1-x2;
double y = y1-y2;
double dist;
dist = pow(x, 2) + pow(y , 2);
dist = sqrt(dist);
return dist;
}

struct point
{
char name [1];
double x, y, origin_distance ;
struct point *next;
};

int main(void)
{
int choice;
struct point *head = nullptr, *newElement, *p ;
do
{
cout << "0 end"<< endl;
cout <<"1 input point"<< endl;
cout <<"2 print list of points" << endl;
cin >> choice;
switch(choice)

{
case 1:
newElement = new point;
if (newElement == nullptr)
cerr << "Not enough free memory " << endl;
else
{
cout<< "Please enter point's name: ";
cin>> newElement ->name;
cout << "Please input value x:";
cin >> newElement->x;
cout <<"Please input value y:";
cin >> newElement->y;
newElement ->next = head;
head = newElement;
}
break;
case 2:
cout<< "Print list of points"<< endl;
cout<< "name.\t x.\t y.\t" << endl;
p=head;
while (p!= nullptr)
{
cout<< p->name<<"\t"<< p->x<<"\t" << p->y << "\t" << endl;
p=p->next;
}
break;


}
} while (choice !=0);
return 0;
Why do you use a linked list?

What kind of name is stored in one-character array?

Why does your distance() take four ints and not two points?

Where is your delete?

"Nearest point" as in "nearest to origin"?

Do you need to show the actual distances?
In this task a simplified version of obstacles shall be modeled by points detected by an imaginary sensor system placed in the origin of a two dimensional local cartesian coordinate system.
Arbitrary many of such obstacles /points in front of a vehicle shall be possible to store in a list. This list shall be sorted by the Euclidian distance from the origin (see examples below).
(Hint: write a function to calculate the Euclidian distance d of two obstacles/points P1 and P2 with coordinates (x1,y1) and (x2,y2) by the formula: d=√(x1−x2)2+(y1−y2)2
To simply identify them each obstacle/point shall store a string ("A", "B", ... in example above), the distance and its coordinates (xi,yi).
(Hint: give a type definition for a structure with these four data as components/variables of the structure.)
In a loop in function main arbitrary many of such obstacles/points shall be possible to be inputted and stored in a list. Afterwards the sorted by distance list shall be outputted. Beside the string, distance and coordinates in each output also the string of the nearest obstacle/point to it shall be computed and outputted additionaly (see example below).
(Hint: write a further function with a list of all obstacles/points as first and one obstacle/point as second parameter calculating the neareast other obstacle/point to it and returning it as pointer. Take care that an obstacle/point does not return and output distance 0 to itself, and that at least two obstacles/points need to exist; otherwise a null pointer nullptr shall be returned.)
This is the task, I'm unfortunatelly new to C++ so I don't have so much experience..
I was going to do the delete option too, i just got stuck on the list, i know the structure is wrong, but infortunatelly i don't know how to print out a list already sorted according on the distance origin-> point.
the name sorted in the array should be the name of the point, but i guess that is also wrong..
Your use of a struct is on the right lines but choose string for the name, as keskiverto highlighted. Also, define Euclidean distance as a struct method and the < operator based on Euclidean distance from (0,0) and store the points in a set - that way the points will be automatically sorted based on distance since the default sorting criterion for set is <. Example below (uses range loop, C++11, to print points but you can also use iterators):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include <iostream>
#include <string>
#include<set>
#include<cmath>

using namespace std;

struct Point
{
    string m_name;
    double m_x;
    double m_y;

    Point(string name, double x, double y) : m_name(name), m_x(x), m_y(y) {}

    double distance (const Point& p) const
    {
        return sqrt(pow(m_x - p.m_x,2) + pow(m_y - p.m_y, 2));
    }

    bool operator < (const Point & p) const
    {
        Point origin("origin", 0, 0);
        return this->distance(origin) < p.distance(origin);
    }
};
ostream& operator << (ostream& os, const Point& p)
{
    os<<"The name of the point is: "<<p.m_name<<", with co-ordinates: ("<<p.m_x<<", "<<p.m_y<<") \n";

    return os;
}

int main(){

    set<Point> setPoint;
    Point p1("p1", 2, 3);
    setPoint.insert(p1);
    Point p2 ("p2", 15, 4);
    setPoint.insert(p2);
    Point p3("p3", -7, 5.6);
    setPoint.insert(p3);
    Point p4 ("p4", 13, -63.2);
    setPoint.insert(p4);

    for(auto& itr : setPoint)
    {
        cout<< itr;
    }
}

Output
1
2
3
4
The name of the point is: p1, with co-ordinates: (2, 3)
The name of the point is: p3, with co-ordinates: (-7, 5.6)
The name of the point is: p2, with co-ordinates: (15, 4)
The name of the point is: p4, with co-ordinates: (13, -63.2)
(Hint: write a further function with a list of all obstacles/points as first and one obstacle/point as second parameter calculating the neareast other obstacle/point to it and returning it as pointer. Take care that an obstacle/point does not return and output distance 0 to itself, and that at least two obstacles/points need to exist; otherwise a null pointer nullptr shall be returned.)

Hints are good. Here is a different hint:
1
2
3
4
5
6
7
size_t findNearest( const std::vector<Point> & list, size_t pivot ) {
  if ( ! (1 < pivot.size()) ) return list.size(); // not enough elements
  else if ( ! (pivot < list.size()) ) return list.size(); // invalid pivot
  else {
    // return the index of element in list that is closest to list[pivot], but is not the pivot
  }
}
Topic archived. No new replies allowed.