Solved

sorting

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

Control application downtime with dependency maps

Visualize the interdependencies between application components better with Applications Manager's automated application discovery and dependency mapping feature. Resolve performance issues faster by quickly isolating problematic components.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How to configure Digital Ouput in Renesas RH850/FIL Micrcontroller ? 5 185
Test against App 49 133
Read file descriptor 3 in C 14 104
Raspberry Pi 3 to send text message 9 71
Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
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 and use structures in the C programming language.
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.

911 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

23 Experts available now in Live!

Get 1:1 Help Now