sort array of struct

T

tienlx

Hi all,

I want to sort an array of struct _line:

....
typedef struct _line
{
int x1;
int x2;
} line;
bool comp(line l1,line l2)
{
if (l1.x1==l2.x1) return (l1.x2 < l2.x2);
return (l1.x1 < l2.x1);
}

line* R;
....
R = new line[k];
....
sort(&R[0],&R[k],comp);
....
delete[] R;

And when i printed out the array R, the result is wrong, but i run
well when i use vector<line>. And i don't know why sometimes the
program crashes when i use delete[] R; .Remove this line, it doesn't.
I use DevC++ 5, Win Vista.


Thanks for help,
tl
 
R

ravinder thakur

Hi all,

I want to sort an array of struct _line:

...
typedef struct _line
{
int x1;
int x2;} line;

bool comp(line l1,line l2)
{
if (l1.x1==l2.x1) return (l1.x2 < l2.x2);
return (l1.x1 < l2.x1);

}

line* R;
...
R = new line[k];
...
sort(&R[0],&R[k],comp);
...
delete[] R;

And when i printed out the array R, the result is wrong, but i run
well when i use vector<line>. And i don't know why sometimes the
program crashes when i use delete[] R; .Remove this line, it doesn't.
I use DevC++ 5, Win Vista.

Thanks for help,
tl

can you please paste what input (a small one) you are giving and what
output you are expecting ? your comparator function might be doing the
tricks.
 
T

tienlx

Thanks for your reply,
I used a loop to print out the array:
for(int i=0;i<k;i++)
{
printf("%d ,%d\n",(R).x1,(R).x2);
}

Here is my output:
3094712 ,3084072
541872467 ,1987208531
544039282 ,1701603654
544039282 ,1701603654
544499311 ,1970497878
809000050 ,1869567068
842231141 ,1547322171
962359909 ,1413766192
977484654 ,1986348124
995913326 ,1348221507
.....

and what i want is
1,1
1,2
1,3
1,4
....
1,10
2,1
2,2
2,3
....
2,10
....
10,10


As you see, my input was in range 1-10,1-10 and i have no idea why
the result genenates random numbers.
Also, why the program crashes when i put this line in the end of
function to release the memory of array R:
delete[] R;
if i remove that line, i doesn't crash.

thanks,
tl

I want to sort an array of struct _line:
...
typedef struct _line
{
int x1;
int x2;} line;
bool comp(line l1,line l2)
{
if (l1.x1==l2.x1) return (l1.x2 < l2.x2);
return (l1.x1 < l2.x1);

line* R;
...
R = new line[k];
...
sort(&R[0],&R[k],comp);
...
delete[] R;
And when i printed out the array R, the result is wrong, but i run
well when i use vector<line>. And i don't know why sometimes the
program crashes when i use delete[] R; .Remove this line, it doesn't.
I use DevC++ 5, Win Vista.
Thanks for help,
tl

can you please paste what input (a small one) you are giving and what
output you are expecting ? your comparator function might be doing the
tricks.
 
J

Jim Langston

tienlx said:
Thanks for your reply,
I used a loop to print out the array:
for(int i=0;i<k;i++)
{
printf("%d ,%d\n",(R).x1,(R).x2);
}

Here is my output:
3094712 ,3084072
541872467 ,1987208531
544039282 ,1701603654
544039282 ,1701603654
544499311 ,1970497878
809000050 ,1869567068
842231141 ,1547322171
962359909 ,1413766192
977484654 ,1986348124
995913326 ,1348221507
....

and what i want is
1,1
1,2
1,3
1,4
...
1,10
2,1
2,2
2,3
...
2,10
...
10,10


As you see, my input was in range 1-10,1-10 and i have no idea why
the result genenates random numbers.
Also, why the program crashes when i put this line in the end of
function to release the memory of array R:
delete[] R;
if i remove that line, i doesn't crash.

thanks,
tl

I want to sort an array of struct _line:
...
typedef struct _line
{
int x1;
int x2;} line;
bool comp(line l1,line l2)
{
if (l1.x1==l2.x1) return (l1.x2 < l2.x2);
return (l1.x1 < l2.x1);

line* R;
...
R = new line[k];
...
sort(&R[0],&R[k],comp);
...
delete[] R;
And when i printed out the array R, the result is wrong, but i run
well when i use vector<line>. And i don't know why sometimes the
program crashes when i use delete[] R; .Remove this line, it doesn't.
I use DevC++ 5, Win Vista.
Thanks for help,
tl

can you please paste what input (a small one) you are giving and what
output you are expecting ? your comparator function might be doing the
tricks.


All I can tell you is that the following produces the expected output. I.E.
1,1
1,2
1,3
etc...

Paste your actual code that is producing the errornous output etc...

#include <iostream>
#include <algorithm>

typedef struct _line
{
int x1;
int x2;
} line;

bool comp(line l1,line l2)
{
if (l1.x1==l2.x1) return (l1.x2 < l2.x2);
return (l1.x1 < l2.x1);
}

int main()
{
line* R;

int row = 10;
int col = 10;
int k = row * col;

R = new line[k];
int count = 0;
for ( int i = 10; i > 0; --i )
{
for ( int j = 10; j > 0; --j )
{
R[count].x1 = i;
R[count].x2 = j;
count++;
if ( count > k )
{
std::cout << "Overflowed the buffer, dummy!\n";
return 0;
}

}
}

std::sort(&R[0],&R[k],comp);

for(int i=0;i<k;i++)
{
printf("%d ,%d\n",(R).x1,(R).x2);
}

delete[] R;

}
 
T

terminator

Thanks for your reply,
I used a loop to print out the array:
for(int i=0;i<k;i++)
{
printf("%d ,%d\n",(R).x1,(R).x2);
}

Here is my output:
3094712 ,3084072
541872467 ,1987208531
544039282 ,1701603654
544039282 ,1701603654
544499311 ,1970497878
809000050 ,1869567068
842231141 ,1547322171
962359909 ,1413766192
977484654 ,1986348124
995913326 ,1348221507
....
and what i want is
1,1
1,2
1,3
1,4
...
1,10
2,1
2,2
2,3
...
2,10
...
10,10
As you see, my input was in range 1-10,1-10 and i have no idea why
the result genenates random numbers.
Also, why the program crashes when i put this line in the end of
function to release the memory of array R:
delete[] R;
if i remove that line, i doesn't crash.
thanks,
tl

Hi all,
I want to sort an array of struct _line:
...
typedef struct _line
{
int x1;
int x2;} line;
bool comp(line l1,line l2)
{
if (l1.x1==l2.x1) return (l1.x2 < l2.x2);
return (l1.x1 < l2.x1);
}
line* R;
...
R = new line[k];
...
sort(&R[0],&R[k],comp);
...
delete[] R;
And when i printed out the array R, the result is wrong, but i run
well when i use vector<line>. And i don't know why sometimes the
program crashes when i use delete[] R; .Remove this line, it doesn't.
I use DevC++ 5, Win Vista.
Thanks for help,
tl
can you please paste what input (a small one) you are giving and what
output you are expecting ? your comparator function might be doing the
tricks.

All I can tell you is that the following produces the expected output. I.E.
1,1
1,2
1,3
etc...

Paste your actual code that is producing the errornous output etc...

#include <iostream>
#include <algorithm>

typedef struct _line
{
int x1;
int x2;

} line;

bool comp(line l1,line l2)
{
if (l1.x1==l2.x1) return (l1.x2 < l2.x2);
return (l1.x1 < l2.x1);

}

int main()
{
line* R;

int row = 10;
int col = 10;
int k = row * col;

R = new line[k];
int count = 0;
for ( int i = 10; i > 0; --i )
{
for ( int j = 10; j > 0; --j )
{
R[count].x1 = i;
R[count].x2 = j;
count++;
if ( count > k )
{
std::cout << "Overflowed the buffer, dummy!\n";
return 0;
}

}
}

std::sort(&R[0],&R[k],comp);

for(int i=0;i<k;i++)
{
printf("%d ,%d\n",(R).x1,(R).x2);
}

delete[] R;



}


I think the problem is that he 'print's after 'delete'.
printf("%d ,%d\n",(R).x1,(R).x2);

why not to use 'cout'?

#include <iostream>
using namespace std;
....
cout << R.x1 << ',' << (R.x2 <<'\n';

regards,
FM.
 
J

Jim Langston

terminator said:
Thanks for your reply,
I used a loop to print out the array:
for(int i=0;i<k;i++)
{
printf("%d ,%d\n",(R).x1,(R).x2);
}

Here is my output:
3094712 ,3084072
541872467 ,1987208531
544039282 ,1701603654
544039282 ,1701603654
544499311 ,1970497878
809000050 ,1869567068
842231141 ,1547322171
962359909 ,1413766192
977484654 ,1986348124
995913326 ,1348221507
....
and what i want is
1,1
1,2
1,3
1,4
...
1,10
2,1
2,2
2,3
...
2,10
...
10,10
As you see, my input was in range 1-10,1-10 and i have no idea why
the result genenates random numbers.
Also, why the program crashes when i put this line in the end of
function to release the memory of array R:
delete[] R;
if i remove that line, i doesn't crash.
thanks,
tl

On Oct 9, 7:04 pm, tienlx <[email protected]> wrote:
I want to sort an array of struct _line:
...
typedef struct _line
{
int x1;
int x2;} line;
bool comp(line l1,line l2)
{
if (l1.x1==l2.x1) return (l1.x2 < l2.x2);
return (l1.x1 < l2.x1);

line* R;
...
R = new line[k];
...
sort(&R[0],&R[k],comp);
...
delete[] R;
And when i printed out the array R, the result is wrong, but i run
well when i use vector<line>. And i don't know why sometimes the
program crashes when i use delete[] R; .Remove this line, it
doesn't.
I use DevC++ 5, Win Vista.
Thanks for help,
tl
can you please paste what input (a small one) you are giving and what
output you are expecting ? your comparator function might be doing the
tricks.

All I can tell you is that the following produces the expected output.
I.E.
1,1
1,2
1,3
etc...

Paste your actual code that is producing the errornous output etc...

#include <iostream>
#include <algorithm>

typedef struct _line
{
int x1;
int x2;

} line;

bool comp(line l1,line l2)
{
if (l1.x1==l2.x1) return (l1.x2 < l2.x2);
return (l1.x1 < l2.x1);

}

int main()
{
line* R;

int row = 10;
int col = 10;
int k = row * col;

R = new line[k];
int count = 0;
for ( int i = 10; i > 0; --i )
{
for ( int j = 10; j > 0; --j )
{
R[count].x1 = i;
R[count].x2 = j;
count++;
if ( count > k )
{
std::cout << "Overflowed the buffer, dummy!\n";
return 0;
}

}
}

std::sort(&R[0],&R[k],comp);

for(int i=0;i<k;i++)
{
printf("%d ,%d\n",(R).x1,(R).x2);
}

delete[] R;



}


I think the problem is that he 'print's after 'delete'.
printf("%d ,%d\n",(R).x1,(R).x2);

why not to use 'cout'?

#include <iostream>
using namespace std;
...
cout << R.x1 << ',' << (R.x2 <<'\n';


Yes, when I orignally copied the code and did a quick program, I was
printfing after the delete[] and getting the same results at the OP. So I
moved the delete to the correct position. That may be possible.

And actually there are a few things I would change about this program, get
rid of the typedef (not needed in C++), std::cout instead of printf, etc..
but I left everything as the OP had it to show that the code he had shown
wasn't the issue.
 

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

No members online now.

Forum statistics

Threads
474,099
Messages
2,570,626
Members
47,237
Latest member
David123

Latest Threads

Top