Solved

sorting

Posted on 1997-02-26
2
191 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
Comment Utility
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
Comment Utility
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
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…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.

771 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now