By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,630 Members | 1,126 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,630 IT Pros & Developers. It's quick & easy.

sort array of struct

P: n/a
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

Oct 9 '07 #1
Share this Question
Share on Google+
5 Replies


P: n/a
On Oct 9, 7:04 pm, tienlx <tien...@gmail.comwrote:
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.

Oct 9 '07 #2

P: n/a
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[i]).x1,(R[i]).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, 9:28 pm, ravinder thakur <ravindertha...@gmail.comwrote:
On Oct 9, 7:04 pm, tienlx <tien...@gmail.comwrote:
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.

Oct 9 '07 #3

P: n/a
"tienlx" <ti*****@gmail.comwrote in message
news:11**********************@g4g2000hsf.googlegro ups.com...
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[i]).x1,(R[i]).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, 9:28 pm, ravinder thakur <ravindertha...@gmail.comwrote:
>On Oct 9, 7:04 pm, tienlx <tien...@gmail.comwrote:
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[i]).x1,(R[i]).x2);
}

delete[] R;

}
Oct 10 '07 #4

P: n/a
On Oct 10, 10:16 am, "Jim Langston" <tazmas...@rocketmail.comwrote:
"tienlx" <tien...@gmail.comwrote in message

news:11**********************@g4g2000hsf.googlegro ups.com...


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[i]).x1,(R[i]).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, 9:28 pm, ravinder thakur <ravindertha...@gmail.comwrote:
On Oct 9, 7:04 pm, tienlx <tien...@gmail.comwrote:
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[i]).x1,(R[i]).x2);
}

delete[] R;

}
I think the problem is that he 'print's after 'delete'.
printf("%d ,%d\n",(R[i]).x1,(R[i]).x2);
why not to use 'cout'?

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

regards,
FM.

Oct 10 '07 #5

P: n/a
"terminator" <fa***********@gmail.comwrote in message
news:11**********************@o3g2000hsb.googlegro ups.com...
On Oct 10, 10:16 am, "Jim Langston" <tazmas...@rocketmail.comwrote:
>"tienlx" <tien...@gmail.comwrote in message

news:11**********************@g4g2000hsf.googlegr oups.com...


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[i]).x1,(R[i]).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, 9:28 pm, ravinder thakur <ravindertha...@gmail.comwrote:
On Oct 9, 7:04 pm, tienlx <tien...@gmail.comwrote:
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[i]).x1,(R[i]).x2);
}

delete[] R;

}

I think the problem is that he 'print's after 'delete'.
printf("%d ,%d\n",(R[i]).x1,(R[i]).x2);
why not to use 'cout'?

#include <iostream>
using namespace std;
...
cout << R[i].x1 << ',' << (R[i].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.
Oct 10 '07 #6

This discussion thread is closed

Replies have been disabled for this discussion.