Solved

sorting

Posted on 1997-02-26
2
196 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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

Suggested Solutions

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
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…
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.
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.

840 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