Way to sort in C++, how? 4 possiblities

A

Andreas Ott

http://www1.minpic.de/bild_anzeigen.php?id=72043&key=73473177&ende

Hello together,

I have a big problem.
I have 4 cases about the way.
Please see the picture.

Problem:
My code is not working. I don't know why.
Can somebody help me.

C++ Application

I use static. Only with static I can call this function.
std::sort(mylistSortWay.begin(),
mylistSortWay.end(),
CSortWay::SortCase1);
It is good or bad?
Maybe better with a function of the object, or?


Problem
It is also not working. It is not correct.

Pos=1 x= 10.00, y= 10.00
Pos=2 x= 10.00, y= 20.00
Pos=3 x= 10.00, y= 30.00
Pos=4 x= 30.00, y= 10.00
Pos=5 x= 30.00, y= 20.00
Pos=6 x= 10.00, y= 40.00
Pos=7 x= 30.00, y= 30.00
Pos=8 x= 30.00, y= 40.00
Pos=9 x= 50.00, y= 10.00
Pos=10 x= 50.00, y= 20.00
Pos=11 x= 50.00, y= 30.00
Pos=12 x= 50.00, y= 40.00
Pos=13 x= 70.00, y= 10.00
Pos=14 x= 70.00, y= 20.00
Pos=15 x= 70.00, y= 30.00
Pos=16 x= 70.00, y= 40.00
Pos=17 x= 90.00, y= 10.00
Pos=18 x= 90.00, y= 20.00
Pos=19 x= 90.00, y= 30.00
Pos=20 x= 90.00, y= 40.00

I need the algorithm for all 4 cases.
Can you give me that?
Can you say, where can I reread it, if you not want to give me the code?

My code now - Thanks for check!
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~+

// Sort_Wege.cpp :

//

#include "stdafx.h"
#include "Sort_Wege.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif


// Das einzige Anwendungsobjekt

CWinApp theApp;

using namespace std;

#pragma once
#include <vector>
#include <algorithm>

#include <conio.h>
#include <ctype.h>


class CSortWay
{
public:
CSortWay(){}
CSortWay(double x, double y) { X = x; Y = y; }
~CSortWay(){}
typedef std::vector<CSortWay> data;

public:
double X;
double Y;

static double SortCase1(const CSortWay var1,const CSortWay var2)
{
double x1 = var1.X;
double y1 = var1.Y;

double x2 = var2.X;
double y2 = var2.Y;

return( (x1*x1 + y1*y1) < (x2*x2 + y2*y2) );
}

static double SortCase2(const CSortWay var1,const CSortWay var2)
{
// ** TODO
double x1 = var1.X;
double y1 = var1.Y;

double x2 = var2.X;
double y2 = var2.Y;

return( (x1*x1 + y1*y1) < (x2*x2 + y2*y2) );
}


static double SortCase3(const CSortWay var1,const CSortWay var2)
{
// ** TODO
double x1 = var1.X;
double y1 = var1.Y;

double x2 = var2.X;
double y2 = var2.Y;

return( (x1*x1 + y1*y1) < (x2*x2 + y2*y2) );
}

static double SortCase4(const CSortWay var1,const CSortWay var2)
{
// ** TODO
double x1 = var1.X;
double y1 = var1.Y;

double x2 = var2.X;
double y2 = var2.Y;

return( (x1*x1 + y1*y1) < (x2*x2 + y2*y2) );
}
};







int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{
int nRetCode = 0;

// MFC initialisieren und drucken. Bei Fehlschlag Fehlermeldung
aufrufen.
if (!AfxWinInit:):GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0))
{
// TODO: Den Fehlercode an Ihre Anforderungen anpassen.
_tprintf(_T("Schwerwiegender Fehler bei der
MFC-Initialisierung\n"));
nRetCode = 1;
}
else
{
CSortWay::data mylistSortWay;

mylistSortWay.push_back(CSortWay(10,10));
mylistSortWay.push_back(CSortWay(10,20));
mylistSortWay.push_back(CSortWay(10,30));
mylistSortWay.push_back(CSortWay(10,40));

mylistSortWay.push_back(CSortWay(30,10));
mylistSortWay.push_back(CSortWay(30,20));
mylistSortWay.push_back(CSortWay(30,30));
mylistSortWay.push_back(CSortWay(30,40));

mylistSortWay.push_back(CSortWay(50,10));
mylistSortWay.push_back(CSortWay(50,20));
mylistSortWay.push_back(CSortWay(50,30));
mylistSortWay.push_back(CSortWay(50,40));

mylistSortWay.push_back(CSortWay(70,10));
mylistSortWay.push_back(CSortWay(70,20));
mylistSortWay.push_back(CSortWay(70,30));
mylistSortWay.push_back(CSortWay(70,40));

mylistSortWay.push_back(CSortWay(90,10));
mylistSortWay.push_back(CSortWay(90,20));
mylistSortWay.push_back(CSortWay(90,30));
mylistSortWay.push_back(CSortWay(90,40));


// ** Case 1
std::sort(mylistSortWay.begin(),
mylistSortWay.end(),
CSortWay::SortCase1);
int count=0;
for ( CSortWay::data::iterator it2(mylistSortWay.begin());
it2!=mylistSortWay.end();
it2++ )
{
++count;
printf("Pos=%d x= %7.2f, y=%7.2f\r\n",
count,
(*it2).X,
(*it2).Y);
}
// ** Case 2
std::sort(mylistSortWay.begin(),
mylistSortWay.end(),
CSortWay::SortCase2);
count=0;
for ( CSortWay::data::iterator it2(mylistSortWay.begin());
it2!=mylistSortWay.end();
it2++ )
{
++count;
printf("Pos=%d x= %7.2f, y=%7.2f\r\n",
count,
(*it2).X,
(*it2).Y);
}
// ** Case 3
std::sort(mylistSortWay.begin(),
mylistSortWay.end(),
CSortWay::SortCase3);
count=0;
for ( CSortWay::data::iterator it2(mylistSortWay.begin());
it2!=mylistSortWay.end();
it2++ )
{
++count;
printf("Pos=%d x= %7.2f, y=%7.2f\r\n",
count,
(*it2).X,
(*it2).Y);
}
// ** Case 4
std::sort(mylistSortWay.begin(),
mylistSortWay.end(),
CSortWay::SortCase4);
count=0;
for ( CSortWay::data::iterator it2(mylistSortWay.begin());
it2!=mylistSortWay.end();
it2++ )
{
++count;
printf("Pos=%d x= %7.2f, y=%7.2f\r\n",
count,
(*it2).X,
(*it2).Y);
}
getch();
}



return nRetCode;
}
 
A

Alf P. Steinbach

* Andreas Ott:

Why do you include a link to an incomprehensible graphic on a German page?

Most folks here don't read German.

And more importantly, most folks here are /not/ telepathic. You need to /say/
what you want to communicate. Not just think it.

Hello together,

I have a big problem.
I have 4 cases about the way.
Please see the picture.

Problem:
My code is not working. I don't know why.

And we (or at least, I) don't know what you mean by "not working".

You need to /say/ what you want to communicate.

Not just think it.

Can somebody help me.

C++ Application

I use static. Only with static I can call this function.
std::sort(mylistSortWay.begin(),
mylistSortWay.end(),
CSortWay::SortCase1);
It is good or bad?

The naming convention with prefix "C" is bad, it's something Microsoft does.

Maybe better with a function of the object, or?


Problem
It is also not working. It is not correct.

Pos=1 x= 10.00, y= 10.00
Pos=2 x= 10.00, y= 20.00
Pos=3 x= 10.00, y= 30.00
Pos=4 x= 30.00, y= 10.00
Pos=5 x= 30.00, y= 20.00
Pos=6 x= 10.00, y= 40.00
Pos=7 x= 30.00, y= 30.00
Pos=8 x= 30.00, y= 40.00
Pos=9 x= 50.00, y= 10.00
Pos=10 x= 50.00, y= 20.00
Pos=11 x= 50.00, y= 30.00
Pos=12 x= 50.00, y= 40.00
Pos=13 x= 70.00, y= 10.00
Pos=14 x= 70.00, y= 20.00
Pos=15 x= 70.00, y= 30.00
Pos=16 x= 70.00, y= 40.00
Pos=17 x= 90.00, y= 10.00
Pos=18 x= 90.00, y= 20.00
Pos=19 x= 90.00, y= 30.00
Pos=20 x= 90.00, y= 40.00

What is "not correct"?

You need to /say/ what you want to communicate.

Not just think it.

I need the algorithm for all 4 cases.

What are the "4 cases"?

You need to /say/ what you want to communicate.

Not just think it.

Can you give me that?

Nope -- see above.

Can you say, where can I reread it, if you not want to give me the code?

Nope -- see above.

My code now - Thanks for check!
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~+

// Sort_Wege.cpp :

//

#include "stdafx.h"

This is a non-standard header. You don't need it.

#include "Sort_Wege.h"

This is a header that you forgot to present.

As mentioned, few if any in this group are telepathic.

So it's almost impossible to guess the contents of that header.


#ifdef _DEBUG
#define new DEBUG_NEW
#endif

Redefining a keyword yields Undefined Behavior if you're using any standard
library headers.

// Das einzige Anwendungsobjekt

CWinApp theApp;

You don't need this. It's from some library. One would guess MFC.

using namespace std;

#pragma once

You don't need this. It's compiler specific.

#include <vector>
#include <algorithm>

#include <conio.h>

You don't need this, and anyway it's a non-standard header. Instead of using a
'getch' to stop your program, e.g. run your program from a command interpreter.

#include <ctype.h>

You probably don't need this.

class CSortWay

Don't use a 'C' prefix, it's just noise (and adopting that convention makes it
much harder to use a 'C' prefix for something reasonable such as 'const').

'SortWay' is misleading.

An instance of your class stores to numbers X and Y, it might be e.g. a 'Position'.

{
public:
CSortWay(){}

Unless this is very intentional it's bad: it leaves X and Y with indeterminate
values, which, if ever used, yields formally Undefined Behavior. Depending on
the compiler that UB might be a crash.

CSortWay(double x, double y) { X = x; Y = y; }

OK.

You might improve the notation a little by using a memory initializer list:

CSortWay( double x, double y ): X(x), Y(y) {}

And that's less problematic in the general case (for types other than 'double').

~CSortWay(){}

You don't need this.

typedef std::vector<CSortWay> data;

public:
double X;
double Y;

As a general rule, use all uppercase names for macros and macros /only/ (except
for idiomatic usage such as single letter template type parameters).

static double SortCase1(const CSortWay var1,const CSortWay var2)
{
double x1 = var1.X;
double y1 = var1.Y;

double x2 = var2.X;
double y2 = var2.Y;

return( (x1*x1 + y1*y1) < (x2*x2 + y2*y2) );
}

The result type should be 'bool'.

By convention arguments of class type are passed by reference, and local
variables that are not meant to be modified are declared 'const', thus:

static bool SortCase1( CSortWay const& var1, CSortWay const& var2 )
{
double const x1 = var1.X;
double const y1 = var1.Y;
double const x2 = var2.X;
double const y2 = var2.Y;

return (x1*x1 + y1*y1 < x2*x2 + y2*y2);
}


[snip]
int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])

This is all Microsoft specific, non-standard.

In standard C++:

int main()

It's also much less to write! :)

{
int nRetCode = 0;

You don't need this.

// MFC initialisieren und drucken. Bei Fehlschlag Fehlermeldung
aufrufen.
if (!AfxWinInit:):GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0))
{
// TODO: Den Fehlercode an Ihre Anforderungen anpassen.
_tprintf(_T("Schwerwiegender Fehler bei der
MFC-Initialisierung\n"));
nRetCode = 1;
}
else
{

You don't need the above.

CSortWay::data mylistSortWay;

mylistSortWay.push_back(CSortWay(10,10));
mylistSortWay.push_back(CSortWay(10,20));
mylistSortWay.push_back(CSortWay(10,30));
mylistSortWay.push_back(CSortWay(10,40));

mylistSortWay.push_back(CSortWay(30,10));
mylistSortWay.push_back(CSortWay(30,20));
mylistSortWay.push_back(CSortWay(30,30));
mylistSortWay.push_back(CSortWay(30,40));

mylistSortWay.push_back(CSortWay(50,10));
mylistSortWay.push_back(CSortWay(50,20));
mylistSortWay.push_back(CSortWay(50,30));
mylistSortWay.push_back(CSortWay(50,40));

mylistSortWay.push_back(CSortWay(70,10));
mylistSortWay.push_back(CSortWay(70,20));
mylistSortWay.push_back(CSortWay(70,30));
mylistSortWay.push_back(CSortWay(70,40));

mylistSortWay.push_back(CSortWay(90,10));
mylistSortWay.push_back(CSortWay(90,20));
mylistSortWay.push_back(CSortWay(90,30));
mylistSortWay.push_back(CSortWay(90,40));
OK.


// ** Case 1
std::sort(mylistSortWay.begin(),
mylistSortWay.end(),
CSortWay::SortCase1);
int count=0;
for ( CSortWay::data::iterator it2(mylistSortWay.begin());
it2!=mylistSortWay.end();
it2++ )
{
++count;
printf("Pos=%d x= %7.2f, y=%7.2f\r\n",
count,
(*it2).X,
(*it2).Y);
}

This seems to be OK.

However, note that by convention

(*p).d

is written as simply

p->d

Both more clear and less to write! :)


[snip]

Cheers & hth.,

- Alf
 
J

Juha Nieminen

Andreas said:
Problem
It is also not working. It is not correct.

Pos=1 x= 10.00, y= 10.00
Pos=2 x= 10.00, y= 20.00
Pos=3 x= 10.00, y= 30.00
Pos=4 x= 30.00, y= 10.00
Pos=5 x= 30.00, y= 20.00
Pos=6 x= 10.00, y= 40.00
Pos=7 x= 30.00, y= 30.00
Pos=8 x= 30.00, y= 40.00
Pos=9 x= 50.00, y= 10.00
Pos=10 x= 50.00, y= 20.00
Pos=11 x= 50.00, y= 30.00
Pos=12 x= 50.00, y= 40.00
Pos=13 x= 70.00, y= 10.00
Pos=14 x= 70.00, y= 20.00
Pos=15 x= 70.00, y= 30.00
Pos=16 x= 70.00, y= 40.00
Pos=17 x= 90.00, y= 10.00
Pos=18 x= 90.00, y= 20.00
Pos=19 x= 90.00, y= 30.00
Pos=20 x= 90.00, y= 40.00
static double SortCase1(const CSortWay var1,const CSortWay var2)
{
double x1 = var1.X;
double y1 = var1.Y;

double x2 = var2.X;
double y2 = var2.Y;

return( (x1*x1 + y1*y1) < (x2*x2 + y2*y2) );
}

You are sorting by the square of the length of the (x,y) vector. As
far as I can see, the result you posted is correct. (For example
70*70+40*40 = 6500 < 90*90+10*10 = 8200.)

So unless you explain how is it that you want to sort, the result is
not incorrect, as far as I can see.
 
A

Andreas Ott

Hello together,

my target without picture.

Maybe is helpful for you.

Regards Andreas
http://www1.minpic.de/bild_anzeigen.php?id=72043&key=73473177&ende

INPUT - not sort, like this
Pos=1 x= 10.00, y= 10.00
Pos=2 x= 10.00, y= 20.00
Pos=3 x= 10.00, y= 30.00
Pos=4 x= 10.00, y= 40.00

Pos=5 x= 30.00, y= 10.00
Pos=6 x= 30.00, y= 20.00
Pos=7 x= 30.00, y= 30.00
Pos=8 x= 30.00, y= 40.00

Pos=9 x= 50.00, y= 10.00
Pos=10 x= 50.00, y= 20.00
Pos=11 x= 50.00, y= 30.00
Pos=12 x= 50.00, y= 40.00

Pos=13 x= 70.00, y= 10.00
Pos=14 x= 70.00, y= 20.00
Pos=15 x= 70.00, y= 30.00
Pos=16 x= 70.00, y= 40.00

Pos=17 x= 90.00, y= 10.00
Pos=18 x= 90.00, y= 20.00
Pos=19 x= 90.00, y= 30.00
Pos=20 x= 90.00, y= 40.00

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
OUTPUT

Case 1
Pos=1 x= 10.00, y= 10.00
Pos=2 x= 10.00, y= 20.00
Pos=3 x= 10.00, y= 30.00
Pos=4 x= 10.00, y= 40.00

Pos=5 x= 30.00, y= 10.00
Pos=6 x= 30.00, y= 20.00
Pos=7 x= 30.00, y= 30.00
Pos=8 x= 30.00, y= 40.00

Pos=9 x= 50.00, y= 10.00
Pos=10 x= 50.00, y= 20.00
Pos=11 x= 50.00, y= 30.00
Pos=12 x= 50.00, y= 40.00

Pos=13 x= 70.00, y= 10.00
Pos=14 x= 70.00, y= 20.00
Pos=15 x= 70.00, y= 30.00
Pos=16 x= 70.00, y= 40.00

Pos=17 x= 90.00, y= 10.00
Pos=18 x= 90.00, y= 20.00
Pos=19 x= 90.00, y= 30.00
Pos=20 x= 90.00, y= 40.00


Case 2
Pos=1 x= 10.00, y= 10.00
Pos=2 x= 10.00, y= 20.00
Pos=3 x= 10.00, y= 30.00
Pos=4 x= 10.00, y= 40.00

Pos=5 x= 30.00, y= 40.00
Pos=6 x= 30.00, y= 30.00
Pos=7 x= 30.00, y= 20.00
Pos=8 x= 30.00, y= 10.00

Pos=9 x= 50.00, y= 10.00
Pos=10 x= 50.00, y= 20.00
Pos=11 x= 50.00, y= 30.00
Pos=12 x= 50.00, y= 40.00

Pos=13 x= 70.00, y= 40.00
Pos=14 x= 70.00, y= 30.00
Pos=15 x= 70.00, y= 20.00
Pos=16 x= 70.00, y= 10.00

Pos=17 x= 90.00, y= 10.00
Pos=18 x= 90.00, y= 20.00
Pos=19 x= 90.00, y= 30.00
Pos=20 x= 90.00, y= 40.00


Case 3
Pos=1 x= 10.00, y= 10.00
Pos=2 x= 30.00, y= 10.00
Pos=3 x= 50.00, y= 10.00
Pos=4 x= 70.00, y= 10.00
Pos=5 x= 90.00, y= 10.00

Pos=6 x= 10.00, y= 20.00
Pos=7 x= 30.00, y= 20.00
Pos=8 x= 50.00, y= 20.00
Pos=9 x= 70.00, y= 20.00
Pos=10 x= 90.00, y= 20.00

Pos=11 x= 10.00, y= 30.00
Pos=12 x= 30.00, y= 30.00
Pos=13 x= 50.00, y= 30.00
Pos=14 x= 70.00, y= 30.00
Pos=15 x= 90.00, y= 30.00

Pos=16 x= 10.00, y= 40.00
Pos=17 x= 30.00, y= 40.00
Pos=18 x= 50.00, y= 40.00
Pos=19 x= 70.00, y= 40.00
Pos=20 x= 90.00, y= 40.00

Case 4
Pos=1 x= 10.00, y= 10.00
Pos=2 x= 30.00, y= 10.00
Pos=3 x= 50.00, y= 10.00
Pos=4 x= 70.00, y= 10.00
Pos=5 x= 90.00, y= 10.00

Pos=6 x= 90.00, y= 20.00
Pos=7 x= 70.00, y= 20.00
Pos=8 x= 50.00, y= 20.00
Pos=9 x= 30.00, y= 20.00
Pos=10 x= 10.00, y= 20.00

Pos=11 x= 10.00, y= 30.00
Pos=12 x= 30.00, y= 30.00
Pos=13 x= 50.00, y= 30.00
Pos=14 x= 70.00, y= 30.00
Pos=15 x= 90.00, y= 30.00

Pos=16 x= 90.00, y= 40.00
Pos=17 x= 70.00, y= 40.00
Pos=18 x= 50.00, y= 40.00
Pos=19 x= 30.00, y= 40.00
Pos=20 x= 10.00, y= 40.00
 
J

James Kanze

* Andreas Ott:

Why do you include a link to an incomprehensible graphic on a
German page?
Most folks here don't read German.

But some do:).
And more importantly, most folks here are /not/ telepathic.
You need to /say/ what you want to communicate. Not just think
it.

I'm not sure how much of the communication problem is purely a
language problem (lack of mastery of English). It looks as if
there is more to it than that, but some of the phrases showed a
clear lack of proficiency in English. My recommendation to him
would be to post in de.comp.lang.iso-c++. There are a fair
number of experts who are proficient in German, and respond
there. In German. (I would give similar advice for a French or
Italian speaker. For others, I'm less sure; I think that there
are a lot of languages where there isn't even a C++ group to ask
in. But then, speakers of such langauges tend to be fluent in
another, more widely spread language. They have to be:).)
And we (or at least, I) don't know what you mean by "not
working".
You need to /say/ what you want to communicate.

He's obviously not said enough (independantly of the language).
But it might just be because he doesn't know how to say it in
English (or doesn't feel at ease trying to say it). Something
like "I have 4 cases about the way" is not something a native
English speaker would say (and I can't figure out what is meant
by it, even trying to translate it literally back into German).
 
A

Andreas Ott

Hello together,
>but some do:).
it is only a picture, not more, for better understanding.
>I'm not sure how much of the communication problem is purely a
>language problem (lack of mastery of English). It looks as if
>there is more to it than that, but some of the phrases showed a
>clear lack of proficiency in English. My recommendation to him
>would be to post in de.comp.lang.iso-c++. There are a fair
>number of experts who are proficient in German, and respond
>there. In German. (I would give similar advice for a French or
>Italian speaker. For others, I'm less sure; I think that there
>are a lot of languages where there isn't even a C++ group to ask
>in. But then, speakers of such langauges tend to be fluent in
>another, more widely spread language. They have to be:).)
>like "I have 4 cases about the way" is not something a native
What is here wrong? Yes it is native and enough.
I have an unordered list.
This list, I would like to sort 4 types.
I need the algorithm and the C + + code.
In a German group, I get little support.
It would be very nice, if you would help me.
many thanks and a nice weekend
Andreas
 
T

Thomas J. Gritzan

Andreas said:
Hello together,
it is only a picture, not more, for better understanding.
What is here wrong? Yes it is native and enough.

I would say "I have to sort [a list] in 4 different ways".
I have an unordered list.
This list, I would like to sort 4 types.

"...in 4 different ways"
I need the algorithm and the C + + code.

Do you need 4 different sorting algorithms, or the comparision functions
that a ready made sorting algorithm would use to sort the list?
In a German group, I get little support.

Because you provided the same little information in that group.
It would be very nice, if you would help me.

Ok. From what you said, I guess you need 4 comparision functions, that
compare two pairs of numbers, or two 2-dimensional vectors.

But you didn't say how this vectors should be ordered. There's no
natural ordering for points in a 2d space, you need to define one, or
four, in your case.

Also, this group is about the C++ language and not about algorithms or
even linear algebra. So I suggest asking in sci.math or
de.sci.mathematik for the algorithms instead (but without all this C++
code).
 
S

SG

Andreas said:
What is here wrong? Yes it is native and enough.

You didn't express your problem well in English. I see that you have
asked the question in de.sci.informatik.misc in German as well.
Unfortunately, the German version isn't any better at explaining the
problem.
I have an unordered list.
This list, I would like to sort 4 types.
I need the algorithm and the C + + code.
In a German group, I get little support.
It would be very nice, if you would help me.
many thanks and a nice weekend
Andreas

Your "I need the algorithm" is indicating that you're off-topic in
this group.

From what I understand you have a sequence of points (2D) you whish to
sort in four different ways. This should be solvable via std::sort and
appropriate comparators. What those comparators should compute is up
to you and the desired orders. You need to figure this out for
yourself, first. Then, when you have trouble implementing your
algorithm/comparator, you can ask here for help.

Cheers!
SG
 
J

James Kanze

[...]
What is here wrong? Yes it is native and enough.

It's certainly nothing a native speaker would say. I don't
understand it, and I was born and grew up in the United States.
I have an unordered list.
This list, I would like to sort 4 types.

What does "to sort 4 types" mean? The list contains four
different types (polymorphic?)? Are you asking about how to
establish an ordering criterion for polymorphic types?
I need the algorithm and the C ++ code.
In a German group, I get little support.

I also participate in de.comp.lang.c++-iso. The support there
is excellent, better than here, really. But as I said, "It
looks like if there is more to it than that [just a language
problem]". If you don't explain your problem clearly, you'll
not get much support anywhere---I can't answer a question I
don't understand. (And don't forget, "Besides a mathematical
inclination, an exceptionally good mastery of one's native
tongue is the most vital asset of a competent programmer."
(Dijkstra).
It would be very nice, if you would help me.

I would if I could, but first, I've got to understand where you
need help.
 
A

Andreas Ott

Hello,
It's certainly nothing a native speaker would say. I don't
understand it, and I was born and grew up in the United States.
I have an unordered list.
This list, I would like to sort 4 types.

What does "to sort 4 types" mean? The list contains four
different types (polymorphic?)? Are you asking about how to
establish an ordering criterion for polymorphic types?
I need the algorithm and the C ++ code.
In a German group, I get little support.

I also participate in de.comp.lang.c++-iso. The support there
is excellent, better than here, really. But as I said, "It
looks like if there is more to it than that [just a language
problem]". If you don't explain your problem clearly, you'll
not get much support anywhere---I can't answer a question I
don't understand. (And don't forget, "Besides a mathematical
inclination, an exceptionally good mastery of one's native
tongue is the most vital asset of a competent programmer."
(Dijkstra).
now completely simply explains, without code.

http://www1.minpic.de/bild_anzeigen.php?id=72374&key=12060165&ende

It is clear now?

Thank you very much.

Have a nice Sunday.

Regards Andreas
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Example
INPUT - not sort, like this 20 or more positions
Pos=1 x= 10.00, y= 10.00
Pos=2 x= 10.00, y= 20.00
Pos=3 x= 10.00, y= 30.00
Pos=4 x= 10.00, y= 40.00

Pos=5 x= 30.00, y= 10.00
Pos=6 x= 30.00, y= 20.00
Pos=7 x= 30.00, y= 30.00
Pos=8 x= 30.00, y= 40.00

Pos=9 x= 50.00, y= 10.00
Pos=10 x= 50.00, y= 20.00
Pos=11 x= 50.00, y= 30.00
Pos=12 x= 50.00, y= 40.00

Pos=13 x= 70.00, y= 10.00
Pos=14 x= 70.00, y= 20.00
Pos=15 x= 70.00, y= 30.00
Pos=16 x= 70.00, y= 40.00

Pos=17 x= 90.00, y= 10.00
Pos=18 x= 90.00, y= 20.00
Pos=19 x= 90.00, y= 30.00
Pos=20 x= 90.00, y= 40.00

My target now, the OUTPUT
Possibility 1
Pos=1 x= 10.00, y= 10.00
Pos=2 x= 10.00, y= 20.00
Pos=3 x= 10.00, y= 30.00
Pos=4 x= 10.00, y= 40.00

Pos=5 x= 30.00, y= 10.00
Pos=6 x= 30.00, y= 20.00
Pos=7 x= 30.00, y= 30.00
Pos=8 x= 30.00, y= 40.00

Pos=9 x= 50.00, y= 10.00
Pos=10 x= 50.00, y= 20.00
Pos=11 x= 50.00, y= 30.00
Pos=12 x= 50.00, y= 40.00

Pos=13 x= 70.00, y= 10.00
Pos=14 x= 70.00, y= 20.00
Pos=15 x= 70.00, y= 30.00
Pos=16 x= 70.00, y= 40.00

Pos=17 x= 90.00, y= 10.00
Pos=18 x= 90.00, y= 20.00
Pos=19 x= 90.00, y= 30.00
Pos=20 x= 90.00, y= 40.00


Possibility 2
Pos=1 x= 10.00, y= 10.00
Pos=2 x= 10.00, y= 20.00
Pos=3 x= 10.00, y= 30.00
Pos=4 x= 10.00, y= 40.00

Pos=5 x= 30.00, y= 40.00
Pos=6 x= 30.00, y= 30.00
Pos=7 x= 30.00, y= 20.00
Pos=8 x= 30.00, y= 10.00

Pos=9 x= 50.00, y= 10.00
Pos=10 x= 50.00, y= 20.00
Pos=11 x= 50.00, y= 30.00
Pos=12 x= 50.00, y= 40.00

Pos=13 x= 70.00, y= 40.00
Pos=14 x= 70.00, y= 30.00
Pos=15 x= 70.00, y= 20.00
Pos=16 x= 70.00, y= 10.00

Pos=17 x= 90.00, y= 10.00
Pos=18 x= 90.00, y= 20.00
Pos=19 x= 90.00, y= 30.00
Pos=20 x= 90.00, y= 40.00


Possibility 3
Pos=1 x= 10.00, y= 10.00
Pos=2 x= 30.00, y= 10.00
Pos=3 x= 50.00, y= 10.00
Pos=4 x= 70.00, y= 10.00
Pos=5 x= 90.00, y= 10.00

Pos=6 x= 10.00, y= 20.00
Pos=7 x= 30.00, y= 20.00
Pos=8 x= 50.00, y= 20.00
Pos=9 x= 70.00, y= 20.00
Pos=10 x= 90.00, y= 20.00

Pos=11 x= 10.00, y= 30.00
Pos=12 x= 30.00, y= 30.00
Pos=13 x= 50.00, y= 30.00
Pos=14 x= 70.00, y= 30.00
Pos=15 x= 90.00, y= 30.00

Pos=16 x= 10.00, y= 40.00
Pos=17 x= 30.00, y= 40.00
Pos=18 x= 50.00, y= 40.00
Pos=19 x= 70.00, y= 40.00
Pos=20 x= 90.00, y= 40.00

Possibility 4
Pos=1 x= 10.00, y= 10.00
Pos=2 x= 30.00, y= 10.00
Pos=3 x= 50.00, y= 10.00
Pos=4 x= 70.00, y= 10.00
Pos=5 x= 90.00, y= 10.00

Pos=6 x= 90.00, y= 20.00
Pos=7 x= 70.00, y= 20.00
Pos=8 x= 50.00, y= 20.00
Pos=9 x= 30.00, y= 20.00
Pos=10 x= 10.00, y= 20.00

Pos=11 x= 10.00, y= 30.00
Pos=12 x= 30.00, y= 30.00
Pos=13 x= 50.00, y= 30.00
Pos=14 x= 70.00, y= 30.00
Pos=15 x= 90.00, y= 30.00

Pos=16 x= 90.00, y= 40.00
Pos=17 x= 70.00, y= 40.00
Pos=18 x= 50.00, y= 40.00
Pos=19 x= 30.00, y= 40.00
Pos=20 x= 10.00, y= 40.00
 
S

SG

now completely simply explains, without code.
http://www1.minpic.de/bild_anzeigen.php?id=72374&key=12060165&ende

It is clear now?

No, this picture doesn't explain anything.
Example
INPUT - not sort, like this  20 or more positions
Pos=1 x=   10.00, y=  10.00
Pos=2 x=   10.00, y=  20.00
Pos=3 x=   10.00, y=  30.00
Pos=4 x=   10.00, y=  40.00

Pos=5 x=   30.00, y=  10.00
Pos=6 x=   30.00, y=  20.00
Pos=7 x=   30.00, y=  30.00
Pos=8 x=   30.00, y=  40.00

Pos=9 x=    50.00, y=  10.00
Pos=10 x=   50.00, y=  20.00
Pos=11 x=   50.00, y=  30.00
Pos=12 x=   50.00, y=  40.00

Pos=13 x=   70.00, y=  10.00
Pos=14 x=   70.00, y=  20.00
Pos=15 x=   70.00, y=  30.00
Pos=16 x=   70.00, y=  40.00

Pos=17 x=   90.00, y=  10.00
Pos=18 x=   90.00, y=  20.00
Pos=19 x=   90.00, y=  30.00
Pos=20 x=   90.00, y=  40.00

This is a sequence of pairs of doubles.
My target now, the OUTPUT
Possibility 1
Pos=1 x=   10.00, y=  10.00
Pos=2 x=   10.00, y=  20.00
Pos=3 x=   10.00, y=  30.00
Pos=4 x=   10.00, y=  40.00

Pos=5 x=   30.00, y=  10.00
Pos=6 x=   30.00, y=  20.00
Pos=7 x=   30.00, y=  30.00
Pos=8 x=   30.00, y=  40.00

Pos=9 x=    50.00, y=  10.00
Pos=10 x=   50.00, y=  20.00
Pos=11 x=   50.00, y=  30.00
Pos=12 x=   50.00, y=  40.00

Pos=13 x=   70.00, y=  10.00
Pos=14 x=   70.00, y=  20.00
Pos=15 x=   70.00, y=  30.00
Pos=16 x=   70.00, y=  40.00

Pos=17 x=   90.00, y=  10.00
Pos=18 x=   90.00, y=  20.00
Pos=19 x=   90.00, y=  30.00
Pos=20 x=   90.00, y=  40.00

Ok, then. Use std::sort with an appropriate comparator. The comparator
takes two elements of your sequence and returns a boolean value. It
should return true if and only if the first parameter is "less than"
the second parameter with respect to your desired ordering. The
comparator doesn't have to be a static member function of your
element's class. In fact, it shouldn't. It should be a free function
or a "functor class". It could be as simple as:

bool order1(const point2d & a, const point2d & b)
{
if (a.x != b.x) return a.x < b.x;
return a.y < b.y;
}

...
std::sort(vect.begin(), vect.end(), way1);
...

The thing is that you want us to extrapolate some order and write C++
code for you based on pictures and examples that are hardly self-
explanatory.

If you want to learn the basics of programming and how to apply those
in C++ you should probably ask for book recommendations in a German
group or checkout C++/programming-related lists of frequently asked
questions for book recommendations.

Cheers!
SG
 
A

Andreas Ott

Hello!
No, this picture doesn't explain anything.
That is only the overview.
One input ----- Four possiblitis for sort.

Input - A sequence of pairs of doubles.
Output - A sequence of pairs of doubles in correct order
Ok, then. Use std::sort with an appropriate comparator. The comparator
takes two elements of your sequence and returns a boolean value. It
should return true if and only if the first parameter is "less than"
the second parameter with respect to your desired ordering. The
comparator doesn't have to be a static member function of your
element's class. In fact, it shouldn't. It should be a free function
or a "functor class". It could be as simple as:

bool order1(const point2d & a, const point2d & b)
{
if (a.x != b.x) return a.x < b.x;
return a.y < b.y;
}

...
std::sort(vect.begin(), vect.end(), way1);
...

The thing is that you want us to extrapolate some order and write C++
code for you based on pictures and examples that are hardly self-
explanatory.

If you want to learn the basics of programming and how to apply those
in C++ you should probably ask for book recommendations in a German
group or checkout C++/programming-related lists of frequently asked
questions for book recommendations.
yes of course, but if you have a task, the best is you solve it.
In this case I/you learn at most.

I look for for all four possibilities the algorithm.
Do you have it or can I in the Internet it reread.
Do you have a good link for mathematical algorithms?

Cheers!
Andreas
 
A

Andreas Ott

Hello,

the actual state.
http://www1.minpic.de/bild_anzeigen.php?id=72043&key=73473177&ende

My project.
http://www.fileuploadx.de/361318

Why I need the 4 ways?
Reason:
http://www1.minpic.de/bild_anzeigen.php?id=72991&key=4250916&ende
The marking unit is fix, no moving.
It is a machine to mark something in correct direction.
Please see the picture.

My problem:
I need the algorithm for case 2 and 4
Maybe somebody can help me.

Greet

- Andreas -


static bool SortCase1(const CSortWay var1,const CSortWay var2)
{
double x1 = var1.X;
double y1 = var1.Y;

double x2 = var2.X;
double y2 = var2.Y;

return( (x1<x2) || (x1==x2 && y1<y2) );

}

static bool SortCase2(const CSortWay var1,const CSortWay var2)
{
// ** TODO
double x1 = var1.X;
double y1 = var1.Y;

double x2 = var2.X;
double y2 = var2.Y;

return( (x1*x1 + y1*y1) < (x2*x2 + y2*y2) );
}


static bool SortCase3(const CSortWay var1,const CSortWay var2)
{
double x1 = var1.X;
double y1 = var1.Y;

double x2 = var2.X;
double y2 = var2.Y;

return( (y1<y2) || (y1==y2 && x1<x2) );

}

static bool SortCase4(const CSortWay var1,const CSortWay var2)
{
// ** TODO
double x1 = var1.X;
double y1 = var1.Y;

double x2 = var2.X;
double y2 = var2.Y;

return( (x1*x1 + y1*y1) < (x2*x2 + y2*y2) );
}
};
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top