i need to write a C++ program that outputs all distinct points from a given list of 10 points.where each point has x and y values (in the 2D plane)
For example,
Input:
x y
3 5
4 2
2 4
3 5
7 8
7 8
4 2
7 8
3 5
2 4
Output:
Distinct Points
x y
3 5
4 2
2 4
7 8
i need to use structure
like struct point {
int x; int y;
}
Here is a little skeleton to get you started, have a look if you understand the concept.
Using a vector, operator overloading and algorithms would make your life much easier, but I am afraid this is a kind of exercise where your hands got handcuffed behind your back.
#include <iostream>
usingnamespace std;
struct Point
{
int x;
int y;
};
constint NUM_POINTS = 10;
bool EqualPoints(const Point& p1, const Point& p2)
{
//check if the points are equal, return true if they are otherwise false
}
bool PointInArray(Point[] points, const Point& p)
{
// loop through all the points in array points
for (int i = 0; i < NUM_POINTS; i++)
{
if (EqualPoints(points[i], tmp))
{
returntrue;
}
}
returnfalse;
}
int main()
{
Point points[NUM_POINTS];
int index = -1; // stores the index of the last inserted point in the array.
for (int = 0; i < NUM_POINTS; i++)
{
Point tmp;
// read x and y into tmp
// if tmp isn't in points, add it and increment index.
}
return 0;
}
if you sort the list then adjacent items are either duplicates, or not.
you should use a vector, not array, for lots of reasons including built in sort.
When searching the array as in function PointInArray() above, no need to search the entire array. Just the part which is actually used. You could add an extra parameter which would be the current number of items.