I have this struct.
struct Transport
{
int id;
float Price;
};
Here I read the data into and array of structs.
void read (struct Transport **Car, int *m)
{
int i;
printf("Insert total number of cars: ");
scanf("%d",m);
*Car=(struct Transport*) malloc ((*m)*3*sizeof(struct Transport));
for(i=1; i<=*m; i++)
{
(*Car)[i].id=i;
printf("Price: ");
scanf("%f",&(*Car)[i].Price);
printf("\n");
}
}
And here is the display function.
void display(struct Transport *Car,int m)
{
int i;
for(i=1; i<=m; i++)
{
printf("Entry #%d: \n",i);
printf("Price: %2.2f\n",(Car+i)->Price);
printf("\n");
}
}
Now here is the problem.I must sort the data by the Price field. So far I've tried this, but it does nothing.
int struct_cmp_by_price(const void *a, const void *b)
{
struct Transport *ia = (struct Transport *)a;
struct Transport *ib = (struct Transport *)b;
return (int)(100.f*ia->Price - 100.f*ib->Price);
}
Here is how the main looks like.
int main()
{
int m;
struct Transport *Car;
read(&Car,&m);
qsort(Car, m, sizeof(struct Transport), struct_cmp_by_price);
display(Car,m);
return 0;
}
Can anyone help me?
qsort(Car,-->qsort(Car+1,orfor(i=1; i<=*m-->for(i=0; i<*mmalloc ((*m)*3*sizeof(struct Transport));