Solved

sorting

Posted on 1997-02-26
2
197 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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…
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…
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.

749 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