Selection Sorting Using 2 Arrays - c++

Suppose I want to take an array of pointers that point to x, y, z coordinates such as:
(1, 2, 4)
(2, 3, 8)
(3, 5, 1)
Then I wanted to take each of those values and compare the distance from point (1, 2, 3) and then sort the distance from each point in descending order.
How would I do that? This is the code I have so far. But I dont know how to get the second array to point back to the original values. Any help would be great.
void sortPointsByDistanceFromRefPoint(Points* points, Point* ref_point)
{
double *array;
double x0,y0,z0;
x0 =ref_point->x;
y0 =ref_point->y;
z0 =ref_point->z;
Point** point_array = points-> point_array;
int num_points = points->num_points;
array = new double[num_points];
double distance;
double x,y,z;
for (int i = 0; i< num_points; i++)
{
Point* point = point_array[i];
x = point->x;
y = point ->y;
z = point ->z;
distance = sqrt( pow((x-x0),2) + pow((y- y0),2) + pow((z-z0),2));
array[i] = distance;
}
double tmp;
for( int i = 0; i < num_points; i++)
cout << array[i] << " " << endl;
cout << endl;
cout << endl;
for (int i = 0; i < num_points -1; i++)
for (int j = i+1; j < num_points; j++)
if (array[i] > array[j])
{
tmp = array[i];
array[i] = array[j];
array[j] = tmp;
}
for( int i = 0; i < num_points; i++)
cout << array[i] << " " << endl;
cout << endl;
delete [] array;
}

When you swap the values in array using
tmp = array[i];
array[i] = array[j];
array[j] = tmp;
also swap the values(pointers) in point_array:
Point* tmpPoint = point_array[i];
point_array[i] = point_array[j];
point_array[j] = tmpPoint;

First you need to iterate over both arrays at the same time and calculate their distances. As you calculated their distances you can add them to a list, or another array. But while you are adding the distances to the list, add them in sorted order. You can have a method AddDistance(double distance). The first incoming distance goes into the sorted array as the first element. Then when the next one comes, you check it's value and compare it to all the other elements in the array. Here is pseudo code that can do the job for you:
double sortedDistances[100];
int sortedDistancesSize;
void InsertDistance(double distance)
{
int i=0;
for(i=0; i < sortedDistancesSize, sortedDistances[i] < distance; ++i);
InsertNewDistance(distance, i);
}
Your InsertNewDistance method should insert the given distance at position "i" and shift the contents of the array after position "i" to the right.

Related

Array elements changing values if I comment out cout statement. Is this memory leak related?

I'm working on a KNN algorithm with 2D arrays. It worked great when I first had the nodes be simple X and Y coordinates, but when I changed it to 2D arrays and reworked some things, I run into a funny issue.
#include <iostream>
#include <math.h>
using namespace std;
//A node
struct Node
{
int groupValue;
int col = 16; //We only have to update these to reflect the bitmap dimensions
int row = 16;
int bitmap[16][16];
float distance = -1;
//distance is the distance this node is from another
//Overloading this operator to make swapping them with insertion sort easier
Node& operator =(const Node& n)
{
for(int a = 0; a < row; a++)
{
for(int b = 0; b < col; b++)
{
bitmap[a][b] = n.bitmap[a][b];
}
}
distance = n.distance;
groupValue = n.groupValue;
return *this;
}
};
void printNode(Node n)
{
for(int a = 0; a < 16; a++)
{
for(int b = 0; b < 16; b++)
cout << n.bitmap[a][b] << ".";
cout << endl;
}
}
struct Group
{
int frequency = 0;
int id;
};
void insertionSort(Node arr[], int n)
{
int i, key, j;
for (i = 1; i < n; i++)
{
key = arr[i].distance;
j = i - 1;
while (j >= 0 && arr[j].distance > key)
{
Node temp = arr[j+1];
arr[j + 1] = arr[j];
arr[j] = temp;
j = j - 1;
}
arr[j + 1].distance = key;
}
}
//Takes two bitmaps and find the distance between each pixel
//using the formula d=sqrt(sum((pixel_1-pixel_2)^2))
float bitmapDistance(Node n1, Node n2)
{
Node differences; //Node's bitmap data will be the differences
//squared of each pixel from n1 and n2.
float diff;
for(int a = 0; a < n1.row; a++)
{
for(int b = 0; b < n1.col; b++)
{
diff = n1.bitmap[a][b] - n2.bitmap[a][b];
differences.bitmap[a][b] = diff * diff; //Squared difference
}
}
//Now we need to sum up the squared differences that we stored earlier
float sum;
for(int a = 0; a < n1.row; a++)
{
for(int b = 0; b < n1.col; b++)
{
sum += differences.bitmap[a][b];
}
}
//Now just take the square root of the sum and return it
if(sum < 0.01) //To fix some weird rounding issues I added this. 0.01 is arbitrary.
return 0;
return sqrt(sum);
}
/*
*Classify the node using KNN algorithm
*Multiple groups can be used, and we count how many neighbors
*belong to each group.
*n is the total amount of nodes
*k is the number of nearby neighbors
*/
int classify(Node arr[], int n, int k, Node node)
{
//Two switches for sampling, recorded as binary (0-3)
Group g0;
g0.id = 0;
Group g1;
g1.id = 1;
Group g2;
g2.id = 2;
Group g3;
g3.id = 3;
Group groupFrequency[4] = {g0, g1, g2, g3};
//For each neighbor of the given node (node) we use the distance formula
//and set it's distance member variable to this measurement
for(int a = 0; a < n; a++)
{
arr[a].distance = bitmapDistance(node, arr[a]);
//cout << arr[a].distance << ","; //<===============PROBLEM AREA===============
}
cout << endl << "----------" << endl;
insertionSort(arr, n-1);
for(int a = 0; a < n; a++)
cout << arr[a].distance << ",";
cout << endl << "----------" << endl;
//Look at all the nearest neighbors and see which group they belong to
//Increase a counter for each group the neighbor belongs to
for(int i = 0; i < k; i++)
{
if(arr[i].groupValue == 0)
groupFrequency[0].frequency++;
else if(arr[i].groupValue == 1)
groupFrequency[1].frequency++;
else if(arr[i].groupValue == 2)
groupFrequency[2].frequency++;
else if(arr[i].groupValue == 3)
groupFrequency[3].frequency++;
}
cout << "Neighboring group frequencies:" << endl;
cout << "Group Zero: " << groupFrequency[0].frequency << endl;
cout << "Group One: " << groupFrequency[1].frequency << endl;
cout << "Group Two: " << groupFrequency[2].frequency << endl;
cout << "Group Three: " << groupFrequency[3].frequency << endl;
cout << "-------------------------------------------------" << endl;
//Now we just need to look at the most common frequency and classify our node
int highestFrequency = 0;
int groupNumber = -1; //If -1 is returned, something went wrong.
for(int a = 0; a < 4; a++)
{
if(groupFrequency[a].frequency >= highestFrequency)
{
highestFrequency = groupFrequency[a].frequency;
groupNumber = groupFrequency[a].id;
}
}
return groupNumber;
}
//This stuff is just setup for testing purposes. Could this be the issue's origin?
int main()
{
Node nodeArray[200];
for(int c = 0; c < 100; c++)
{
for(int a = 0; a < 16; a++)
{
for(int b = 0; b < 16; b++)
{
nodeArray[c].groupValue = 0;
nodeArray[c].bitmap[a][b] = rand()%10;
}
}
}
for(int c = 100; c < 200; c++)
{
for(int a = 0; a < 16; a++)
{
for(int b = 0; b < 16; b++)
{
nodeArray[c].groupValue = 1;
nodeArray[c].bitmap[a][b] = 50;//rand()%10+50;
}
}
}
Node testNode;
for(int a = 0; a < 16; a++)
{
for(int b = 0; b < 16; b++)
{
testNode.bitmap[a][b] = 50;//rand()%10+50;
}
}
int num = classify(nodeArray, 200, 189, testNode);
cout << "Test node has a group classification of: " << num << endl << endl;
printNode(testNode);
cout << endl << "last node in array:" << endl;
printNode(nodeArray[199]);
cout << endl << bitmapDistance(testNode, nodeArray[199]) << endl << "first node in array:" << endl;
printNode(nodeArray[0]);
cout << bitmapDistance(testNode, nodeArray[0]);
}
The problem specifically is on line 124, in the classify() function in the for loop
for(int a = 0; a < n; a++)
{
arr[a].distance = bitmapDistance(node, arr[a]);
//cout << arr[a].distance << ","; //<====PROBLEM AREA
}
This loop is responsible for calculating the distance each node is from the sample/test node. The "arr" array is then sorted a couple of lines later via insertion sort.
If I have the for loop print out the distance values, everything works perfectly fine. I can see that all the data is properly there.
However, if I comment it out, and only print the sorted data, everything is wrong (the data does not mess up if I comment out the print statements for the sorted data, strangely enough. It's just this one line that breaks everything).
I have attached images depicting the differences between the commented line (wrong) and the uncommented line (correct) running to better show what issues I'm dealing with.
Basically, the data will lose decimal precision, have seemingly random values, and any zero value will be non-existent if I comment out this specific line.
(I know the K value should be low, but I'm using high numbers for testing right now)
Some of the output when I don't comment out the line
Some of the output when I do comment out the line
As you can see, when I comment out the line, all the sorted distance values become garbage.
What can I do to fix this?

Index of the Closest point in an array each containing 3 elements

I am trying to find the index of the closest point in an array(pts) containing 3 elements each.
I have written the following code, but it does not return me the index of the closest points:- Would appreciate if anyone could please let me know where i. am wrong. Thanks!
void point_index(Point pts[], int &size)
}
cout << x << " " << y << endl;
Thank you, everyone, for the help! I have figured out my mistake and corrected the code!
You are not storing the minimum value in min, after compare store the minimum value in min and set distance value outside if to compare properly.
a = get_distance(pts[i],pts[j]);
if (a < min) {
x = i;
y = j;
min = a;
}
Once you find the minimum, you need to assign x,y and min.
Try this code:
double a;
int x = 0, y = 1;
double min = get_distance(pts[x],pts[y]);
for (int i = 0; i < size; i++)
{
for (int j = i+1; j < size; j++)
{
double d = get_distance(pts[i], pts[j]);
if (d < min)
{
x = i;
y = j;
min = d;
}
}
}
cout << x << " " << y << endl;

How to visualize a step-by-step process about Complete-Linkage Clustering in a C++?

How to make or visualize a step-by-step process about Complete-Linkage Clustering in a C++ program? Is there some sort of special library or equation? Course Material
I already know how to input the data in matrix array and find the minimum value.
int rows,column;
int minimum, locx, locy;
cout<<"Input column : ";cin>>column;
cout<<"Input rows : ";cin>>rows;
float matrix[rows][column];
for(int i=0;i<=rows;i++){
for(int j=1;j<=i;j++){
cout<<"Input the data in row "<<i<<" column "<<j<<" :";cin>>matrix[i][j];
}
cout<<endl;
}
cout<<"TABLE :"<<endl<<endl;
for(int k=0;k<=rows;k++){
for(int m=1;m<=k;m++){
cout<<" | "<<matrix[k][m]<<" |";
}
cout<<endl;
}
for(int n=0;n<=rows;n++) {
for(int p=1;p<=n;p++){
if (matrix[n][p] < minimum) {
minimum = matrix[n][p];
locx = p;
locy = n;
}
}
}
cout << "Minimum value is " << minimum << " on row no. " << locy << " and column no. " << locx << endl;
The next process (pictured sample) is hard and confusing.
Anybody know how to identified the rows and columns? And how to update the matrix using the new one, repeat the process, until the last matrix (2x2)?
Course Material
Your choice of datastructure for the array is a bit unfortunate, but you can work in-place by copying rows up and columns to the left.
std::vector<std::string> labels; // Fill this with {"a","b",...}
int n = rows;
while (n > 1) {
int min1, min2;
// TODO: insert loop to get row and column of smallest element
// We assume min1 < min2
// Overwrite row min1 and column min1 with the new distance.
for (int i = 0; i < n; i++) {
int new_value = std::max(matrix[i][min1], matrix[i][min2]);
matrix[i][min1] = matrix[min1][i] = new_value;
}
// Record that we joined min1 and min2
std::ostringstream oss;
oss << "(" << labels[min1] << ", " << labels[min2] << ")";
labels[min1] = oss.str();
labels.erase(labels.begin()+min2);
// We now have D_n+1, with a bogus column at min2.
// We get rid of it by moving all columns > min2 to the left,
// and same for all rows > min2
for (int i = min2; i < n-1; i++) {
for (int j = 0; j < n; j++) {
matrix[i][j] = matrix[i+1][j];
matrix[j][i] = matrix[j][i+1];
}
}
n--;
}

Using C-style Arrays with Eigen for Matrix Inverse

I have about 1000 lines of code that I wrote in C for a linear programming solver (interior point algorithm). I realized that I need to use Eigen to calculate a matrix inverse, so now I am running my code in C++ instead (runs just fine, it seems). Now I have a bunch of arrays declared in C format, for example: A[30][30];
In my program, I do a bunch of matrix calculations and then need to find an inverse of a matrix at some point, let's call it matrix L[30][30]. To use Eigen, I need to have it in a special Eigen matrix format to call the function m.inverse like this:
//cout << "The inverse of L is:\n" << L.inverse() << endl;
My goal is to find a way... ANY way, to get my data from L to a format that Eigen will accept so I can run this thing. I've spent the last 2 hours researching this and have come up with nothing. :-( I'm fairly new to C, so please be as thorough as you can. I want the most simple method possible. I've read about mappings, but I'm not very clear on pointers sadly (which seems to be an integral part). Is there a way to just loop through each row and column and copy them into an Eigen matrix?
While I'm asking, will I need to take the resultant Eigen matrix and turn it back into a C array? How would that process work? Thanks in advance for any help! I've spent about 50-60 hours on this and it's due this week! This is the LAST thing I need to do and I'll be done with my term project. It's a math class, so the programming side of things are a little fuzzy for me but I'm learning a lot.
Possibly relevant information:
-Running on Windows 10 i7 processor Sony VAIO
-Compiling with CodeBlocks in C++, but originally written in C
-This code is all in a while loop that may be iterated through 10 times or so.
-The matrix inverse needs to be calculated for this matrix L each iteration, and the data will be different each time.
Please help! I'm willing to learn, but I need guidance and this class is online so I have virtually none. Thanks so much in advance!
Edit - I saw this and tried to implement it to no avail, but it seems like the solution if I can figure this out:
"Suppose you have an array with double values of size nRows x nCols.
double *X; // non-NULL pointer to some data
You can create an nRows x nCols size double matrix using the Map functionality like this:
MatrixXd eigenX = Map<MatrixXd>( X, nRows, nCols );
The Map operation maps the existing memory region into the Eigen’s data structures. A single line like this allows to avoid to write ugly code of matrix creation, for loop with copying each element in good order etc."
This seems to be a nice solution, but I am clueless on how to do anything with that "double *X" that says to "point to some data". I began looking up pointers and such and it didn't help clarify - I saw all kinds of things about pointing to multi-dimensional arrays that didn't seem to help.
I also don't quite understand the format of the second line. Is every capital X there just going to be the same as the matrix *X in the line before? What would I need to declare/create for that? Or is there an easier way that all of this?
EDIT2: Here is what I have in my program, essentially - this is significantly shrunken down, sorry if it's still too long.
#include <iostream>
#include <Eigen/Dense>
using namespace Eigen;
using namespace std;
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <math.h>
typedef Matrix<double, 30, 30> Matrix30d;
double L[30][30] ={{0}};
double Ax[30][30] = {{0}}; //[A] times [x]
double At[30][30] = {{0}}; //A transpose
double ct[30][30] = {{0}}; //c transpose
double x[30][30] = {{0}}; //primal solution
double w[30][30] = {{0}}; //omega, dual solution
double s[30][30] = {{0}}; //dual slack
double u[30][30] = {{0}}; //[c]t - [A]t x [w] - [s]
double Atxw[30][30] = {{0}}; //A transpose times omega
double t[30][30] = {{0}}; //RHS - [A]x[x]
double v[30][30] = {{0}}; //mu - xij * sij
double p[30][30] = {{0}}; //vij / xij
double D2[30][30] = {{0}}; //diagonal of xij/sij
double AD2[30][30] = {{0}}; //[A] x [D2]
double AD2xAt[30][30] = {{0}}; //[AD2] x [At]
double uminp[30][30] = {{0}}; //[u] - [p]
double AD2xuminp[30][30] = {{0}}; //[AD2] x [uminp]
double z[30][30] = {{0}}; //[AD2] x [uminp] + [t]
double Atxdw[30][30] = {{0}}; //[At] x [dw]
double xt[30][30] = {{0}}; //x transpose
double bt[30][30] = {{0}}; //b transpose
Matrix30d Inv; //C++ style matrix for Eigen, maybe needed?
int main(){
int r1; //rows of A
int c1; //columns of A
int i; //row and column counters
int j;
int k;
double sum = 0;
double size; //size of square matrix being inverted [L]
double *pointer[30][30];
FILE *myLPproblem;
myLPproblem = fopen("LPproblem.txt", "r"); //Opens file and reads in data
float firstLine[4];
int Anz;
for (i = 0; i < 4; i++)
{
fscanf(myLPproblem, "%f", &firstLine[i]);
}
r1 = firstLine[0];
c1 = firstLine[1];
Anz = firstLine[2];
double A[r1][c1];
double b[r1][1];
double c[1][c1];
int Ap[c1+1];
int Ai[Anz];
double Ax2[Anz];
for(i=0; i<r1; i++){
for(j=0; j<c1; j++){
A[i][j]=0;
}
}
for (i = 0; i < (c1 + 1); i++)
{
fscanf(myLPproblem, "%d", &Ap[i]);
}
for (i = 0; i < (Anz); i++)
{
fscanf(myLPproblem, "%d", &Ai[i]);
}
for (i = 0; i < (Anz); i++)
{
fscanf(myLPproblem, "%lf", &Ax2[i]);
}
for (i = 0; i < (r1); i++)
{
fscanf(myLPproblem, "%lf", &b[i][0]);
}
for (i = 0; i < (c1); i++)
{
fscanf(myLPproblem, "%lf", &c[0][i]);
}
fclose(myLPproblem);
int row;
double xval;
int Apj;
int Apj2;
for(j=0; j<c1; j++){
Apj = Ap[j];
Apj2 = Ap[j+1];
for(i=Apj; i<Apj2; i++){
row = Ai[i];
xval = Ax2[i];
A[row][j] = xval;
}
}
size = r1;
for(i=0; i<c1; i++) //Create c transpose
{
ct[i][0] = c[0][i];
}
for(i=0; i<r1; i++) //Create b transpose
{
bt[i][0] = b[0][i];
}
for(i=0; i<c1; i++) //Create A transpose
{
for(j=0; j<r1; j++)
{
At[i][j] = A[j][i];
}
}
while(1){ //Main loop for iterations
for (i = 0; i <= r1; i++) { //Multiply [A] times [x]
for (j = 0; j <= 1; j++) {
sum = 0;
for (k = 0; k <= c1; k++) {
sum = sum + A[i][k] * x[k][j];
}
Ax[i][j] = sum;
}
}
sum = 0; //Multiply [At] times [w]
for (i = 0; i <= c1; i++){
for (j = 0; j <= 1; j++) {
sum = 0;
for (k = 0; k <= r1; k++) {
sum = sum + At[i][k] * w[k][j];
}
Atxw[i][j] = sum;
}
}
for(i=0; i<c1; i++) //Subtraction to create matrix u
{for(j=0; j<1; j++)
{
u[i][j] = (ct[i][j]) - (Atxw[i][j]) - (s[i][j]);
}
}
for(i=0; i<r1; i++) //Subtraction to create matrix t
{for(j=0; j<1; j++)
{
t[i][j] = (b[i][j]) - (Ax[i][j]);
}
}
for(i=0; i<c1; i++) //Subtract and multiply to make matrix v
{for(j=0; j<1; j++)
{
v[i][j] = mu - x[i][j]*s[i][j];
}
}
for(i=0; i<c1; i++) //create matrix p
{for(j=0; j<1; j++)
{
p[i][j] = v[i][j] / x[i][j];
}
}
for(i=0; i<c1; i++) //create matrix D2
{for(j=0; j<c1; j++)
{
if(i == j){
D2[i][j] = x[i][0] / s[i][0];
}else{
D2[i][j] = 0;
}
}
}
sum = 0;
for (i = 0; i <= r1; i++) { //Multiply [A] times [D2]
for (j = 0; j <= c1; j++) {
sum = 0;
for (k = 0; k <= c1; k++) {
sum = sum + A[i][k] * D2[k][j];
}
AD2[i][j] = sum;
}
}
sum = 0;
for (i = 0; i <= r1; i++) { //Multiply [AD2] times [At], to be inverted!
for (j = 0; j <= r1; j++) {
sum = 0;
for (k = 0; k <= c1; k++) {
sum = sum + AD2[i][k] * At[k][j];
}
AD2xAt[i][j] = sum;
}
}
//Here is where I need to calculate the inverse (and determinant probably) of matrix AD2xAt. I'd like to inverse to then be stored as [L].
//cout << "The determinant of AD2xAt is " << AD2xAt.determinant() << endl;
//cout << "The inverse of AD2xAt is:\n" << AD2xAt.inverse() << endl;
printf("\n\nThe inverse of AD2xAt, L, is : \n\n"); //print matrix L
for (i=0; i<size; i++)
{
for (j=0; j<size; j++)
{
printf("%.3f\t",AD2xAt[i][j]);
}
printf("\n");
}
}
return 0;
}
In a nutshell, it reads matrices from a file, calculates a bunch of matrices, then needs to invert AD2xAt and store it as L. The critical part is at the end, where I need to take the inverse (scroll to the bottom - I have it commented).
Have you tried
Map<MatrixXd>(A[0],30,30).inverse() ??
– ggael
What you're proposing seems like it would be doing both at once or
something?
Right, the Map<MatrixXd>() returns the Eigen's MatrixXd, on which the method inverse() is called.
May I ask what the [0] is after A?
[0] is the array subscript operator [] designating the 0-th element; A[0] is the initial row of the matrix A[30][30] and is converted to the pointer to A[0][0] corresponding to the X you saw.

Bucket Sort with a custom data structure

My program is tasked with sorting points on an x-y plane, given by the user, according to their distance from the origin using bucket sort. In the instance of having two points with the same distance, the point with the smallest x-coordinate would be selected as the first point. If both the distance and the x-coordinate are the same, the element with the smallest y-coordinate will come first. The output is the points themselves, not their distances. The most logical way I've found to do it so far is to create a custom data structure that houses both the x coordinate, y-coordinate, and its distance in one element. The problem I have at the moment is my current algorithm for standard vectors of doubles, and I have no idea how to convert the sort to fit my needs. Any ideas or suggestions would be helpful.
Here is the layout of the structure:
struct point {
double xc;
double yc;
double dist; };
The current bucket sort, which works fine with vectors of doubles.
void bucketSort(vector<double> &arr) {
int n = B.size();
vector<point> b[n];
for (int i=0; i<n; i++)
{
int bi = n*arr[i];
b[bi].push_back(arr[i]);
}
for (int i=0; i<n; i++)
sort(b[i].begin(), b[i].end());
int index = 0;
for (int i = 0; i < n; i++){
for (int j = 0; j < b[i].size(); j++){
arr[index++] = b[i][j]; }
}
}
The entirety of the code, as of now.
using namespace std;
struct point {
double xc;
double yc;
double dist;
};
vector<double> A;
vector<double> B;
double findDistance(double x = 0, double y = 0) {
double x2 = pow(x, 2);
double y2 = pow(y, 2);
double z = x2 + y2;
double final = sqrt(z);
return final;
}
void bucketSort(vector<double> &arr)
{
int n = B.size();
vector<point> b[n];
for (int i=0; i<n; i++)
{
int bi = n*arr[i];
b[bi].push_back(arr[i]);
}
for (int i=0; i<n; i++)
sort(b[i].begin(), b[i].end());
int index = 0;
for (int i = 0; i < n; i++){
for (int j = 0; j < b[i].size(); j++){
arr[index++] = b[i][j]; }
}
}
int main(){
double number; int t = 0;
while (cin >> number){
A.push_back(number); }
struct point C[A.size()];
while (t < A.size()){
C[t / 2].xc = A[t]; C[t / 2].yc = A[t + 1];
C[t / 2].dist = (findDistance(A[t], A[t + 1])); t += 2; }
cout << setprecision(6); cout << fixed; ;
bucketSort(C);
cout << showpos; cout << fixed;
int x = 0;
while (x < (A.size() / 2)){
cout << C[x].xc << " " << C[x].yc << endl;
x++;
}
}
A vector of doubles B is here because initially, I was trying to get it done with multiple vectors of doubles.
Here is a sample of the input:
0.2 0.38
0.6516 -0.1
-0.3 0.41
-0.38 0.2
Sample output:
-0.380000 +0.200000
+0.200000 +0.380000
-0.300000 +0.410000
+0.651600 -0.100000
I realize that point could have a lot more functions added to it to make it more usable in general, but I'm aiming for just enough to get the current job. Any suggestions or help would be greatly appreciated. Please and thank you.
I would suggest one of there 2 options -
make point a class, not an struct, and overload the < operator, thus making the sort work well.
2.use the sort by function instead of the normal sort:
Firstly, add a compare function:
bool comparePoint(point* a, point* b) {
return true if a < b;
}
the function above would compare the 2 points, according to any rules you like, depends on your code.
and instead of the sort use:
std::sort(b[i].begin(), b[i].end(),comparePoint);
that should work for you.