Solved

sorting

Posted on 1997-02-26
2
194 Views
Last Modified: 2011-04-14
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?
        }
0
Comment
Question by:doraiapp
2 Comments
 
LVL 4

Accepted Solution

by:
emmons earned 50 total points
ID: 1249391
You are creating new versions of the structures on the stack.
Change the "swap" routine to

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);
}

And the call would be
swap(&(lprod[j]),&(lprod[j-1]));

This seems to work fine for me
0
 
LVL 4

Expert Comment

by:jos010697
ID: 1249392
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
0

Featured Post

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.

803 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question