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 5 3175
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.
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.
"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;
}
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.
"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. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: cpuracr8 |
last post by:
i've been looking through the topics here and i can't quite find one
that helps me. i'm trying to sort a struct that contains three arrays
using the sort() function. the three arrays are parallel...
|
by: ritchie |
last post by:
Hi all,
I am new to this group and I have question that you may be able to
help me with.
I am trying to learn C but am currently stuck on this.
First of all, I have a function for each sort...
|
by: PCHOME |
last post by:
Hi!
I have questions about qsort( ). Is anyone be willing to help?
I use the following struct:
struct Struct_A{
double value;
...
} *AA, **pAA;
|
by: chellappa |
last post by:
hi
this simple sorting , but it not running...please correect error for
sorting using pointer or linked list sorting , i did value sorting in
linkedlist
please correct error
#include<stdio.h>...
|
by: Gerrit |
last post by:
Hello,
Is it possible to sort an array with a struct in it?
example:
I have a struct:
public struct mp3file
{
public int tracknr;
|
by: Julia |
last post by:
I am trying to sort a linked list using insertion sort. I have seen a
lot of ways to get around this problem but no time-efficient and
space-efficient solution. This is what I have so far:
...
|
by: Santosh Nayak |
last post by:
Hi,
Is it possible to sort the array of struct based on the data members.
e.g.
struct {
int a ;
float b ;
char c ;
} TEMP ;
|
by: Santosh Nayak |
last post by:
Hi,
Is it possible to sort the array of struct based on the data members.
e.g.
struct {
int a ;
float b ;
char c ;
} TEMP ;
|
by: pabl0 |
last post by:
i have an array of strings that need sorting,(not so much an array of strings but a struct that con tains an array of strings) technically the strings are already sorted.
an example is in order.
...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
| |