Link to home
Start Free TrialLog in
Avatar of doraiapp
doraiapp

asked on

sorting

sort(NAME lprod[],int num)
        {
        int i,j;
        for(i=1;i<4;i++)
        {
        for(j=1;j<=4-i;j++)
        {
        if(strcmp(lprod[j-1].Name,lprod[j].Name)>0){
        printf("function call j-1=%s, j=%s\n",lprod[j-1].Name,lprod[j].Name);
        swap(lprod[j],lprod[j-1]);
}
}
        }
        for (i=0;i<=3;i++){
        printf("sorted =%s\n",lprod[i].Name);}
 
        }
 
        swap(NAME a,NAME b)
        {
        NAME c;
        printf("%s\n",a.Name);
        c.No=b.No;strcpy(c.Name,b.Name);c.Rate=b.Rate;c.Hours=b.Hours;
        b.No=a.No;strcpy(b.Name,a.Name);b.Rate=a.Rate;b.Hours=a.Hours;
        a.No=c.No;strcpy(a.Name,c.Name);a.Rate=c.Rate;a.Hours=c.Hours;
        printf("dummy=%s, a=%s,b=%s\n",c.Name,a.Name,b.Name);

What's wrong with my code i 'm not able to sort it in order?
        }
ASKER CERTIFIED SOLUTION
Avatar of emmons
emmons

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of jos010697
jos010697

You're absolutely right of course, by why not simply do this:

void swap(NAME* a, NAME* b) {

NAME c;

c= *a;
*a= *b;
*b= c;

}

instead of copying all struct members individually?

kind regards,

Jos