Program doesn't swap

This is a discussion on Program doesn't swap within the C Programming forums, part of the General Programming Boards category; I am having difficulty with a code in which I have to use selection sorting to sort a list of ...

  1. #1
    Registered User
    Join Date
    Dec 2008
    Posts
    5

    Program doesn't swap

    I am having difficulty with a code in which I have to use selection sorting to sort a list of indexes. The program compiles, but the function I am using to perform the swap doesn't seem to be doing anything. The program is supposed to look at the alphabetical order from the Last Name = lName, to make the list descending order. Any help would be appreciated.


    Code:
    void sort(PERSON people[], int num)
    {
       int pass, trav, min;
    
       for (pass = 0; pass < num; pass++);
       {
          for (min = pass, trav = pass +1; trav < num; trav++)
          {
             if (strcmp(people[trav].fName, people[min].fName) == 0)
                {
                   min = trav;
                } 
          }
             swap(people[trav], people[min]); 
    
       }
    }
    
    void swap (PERSON people1, PERSON people2)
    {
       PERSON temp = {"", "", 0, "", {0,0,0}};
    printf("bebebe");
       strcpy(temp.fName, people1.fName);
       strcpy(temp.lName, people1.lName);
       temp.Glevel = people1.Glevel;
       strcpy(temp.ssn, people1.ssn);
       temp.bDate.month = people1.bDate.month;
       temp.bDate.day = people1.bDate.day;
       temp.bDate.year = people1.bDate.year;
    
       strcpy(people1.fName, people2.fName);
       strcpy(people1.lName, people2.lName);
       people1.Glevel = people2.Glevel;
       strcpy(people1.ssn, people2.ssn);
       people1.bDate.month = people2.bDate.month;
       people1.bDate.day = people2.bDate.day;
       people1.bDate.year = people2.bDate.year;
    
       strcpy(people2.fName, temp.fName);
       strcpy(people2.lName, temp.lName);
       people2.Glevel = temp.Glevel;
       strcpy(people2.ssn, temp.ssn);
       people2.bDate.month = temp.bDate.month;
       people2.bDate.day = temp.bDate.day;
       people2.bDate.year = temp.bDate.year;
    
    }

  2. #2
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    What does the structure look like?

  3. #3
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    It seems you are passing copies of PERSON on the stack, where you should be passing pointers to PERSON, and then you can use indirection to swap the values.
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

  4. #4
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Ah yes. Nicely done ol' eagle eye.

    Code:
    void sort(PERSON people[], int num)
    {
       int pass, trav, min;
    
       for (pass = 0; pass < num; pass++);
       {
          for (min = pass, trav = pass +1; trav < num; trav++)
          {
             if (strcmp(people[trav].fName, people[min].fName) == 0)
                {
                   min = trav;
                } 
          }
             swap(people + trav, people + min); 
    
       }
    }
    
    
    void swap (PERSON *people1, PERSON *people2)
    {
       PERSON temp = {"", "", 0, "", {0,0,0}};
    printf("bebebe");
       strcpy(temp.fName, people1->fName);
       strcpy(temp.lName, people1->lName);
       temp.Glevel = people1->Glevel;
       strcpy(temp.ssn, people1->ssn);
       temp.bDate->month = people1->bDate->month;
       temp.bDate->day = people1->bDate->day;
       temp.bDate->year = people1->bDate->year;
    
       strcpy(people1->fName, people2->fName);
       strcpy(people1->lName, people2->lName);
       people1->Glevel = people2->Glevel;
       strcpy(people1->ssn, people2->ssn);
       people1->bDate->month = people2->bDate->month;
       people1->bDate->day = people2->bDate->day;
       people1->bDate->year = people2->bDate->year;
    
       strcpy(people2->fName, temp.fName);
       strcpy(people2->lName, temp.lName);
       people2->Glevel = temp.Glevel;
       strcpy(people2->ssn, temp.ssn);
       people2->bDate->month = temp.bDate->month;
       people2->bDate->day = temp.bDate->day;
       people2->bDate->year = temp.bDate->year;
    
    }
    Last edited by master5001; 12-02-2008 at 05:45 PM.

  5. #5
    Registered User
    Join Date
    Dec 2008
    Posts
    5
    I just tired with your suggestion, it came out
    error: invalid type argument of `->'
    this as errors with all of those you replace

  6. #6
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Ooops! Nope. I stupidly deleted one of the parameter types. I bolded my error.

  7. #7
    Registered User
    Join Date
    Dec 2008
    Posts
    5
    Unfortunately, it is still coming up the same errors.

  8. #8
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    Err, maybe this:
    Code:
    void swap (PERSON* people1, PERSON* people2)
    {
       PERSON temp = {"", "", 0, "", {0,0,0}};
        printf("bebebe");
       strcpy(temp.fName, people1->fName);
       strcpy(temp.lName, people1->lName);
       temp.Glevel = people1->Glevel;
       strcpy(temp.ssn, people1->.ssn);
       temp.bDate.month = people1->bDate.month;
       temp.bDate.day = people1->bDate.day;
       temp.bDate.year = people1->bDate.year;
    
       strcpy(people1->fName, people2->fName);
       strcpy(people1->lName, people2->lName);
       people1->Glevel = people2->Glevel;
       strcpy(people1->ssn, people2->ssn);
       people1->bDate.month = people2->bDate.month;
       people1->bDate.day = people2->bDate.day;
       people1->bDate.year = people2->bDate.year;
    
       strcpy(people2->fName, temp.fName);
       strcpy(people2->lName, temp.lName);
       people2->Glevel = temp.Glevel;
       strcpy(people2->ssn, temp.ssn);
       people2->bDate.month = temp.bDate.month;
       people2->bDate.day = temp.bDate.day;
       people2->bDate.year = temp.bDate.year;
    
    }
    Just changing people1 and people2 to pointers and . to -> respectively
    edit: missed some dots
    Last edited by C_ntua; 12-02-2008 at 06:02 PM.

  9. #9
    Registered User
    Join Date
    Dec 2008
    Posts
    5
    Good new is it complies, but the result is the same, didn't swap.

  10. #10
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Yucky, there's no need to change the values... just move pointers about and leave the internal values of the structures alone!

    That's a serious waste of resources copying values around the place.

    BTW, is it class time again? Lots of people have been posting stuff about "STUDENTS" and "PEOPLE".

  11. #11
    Registered User
    Join Date
    Dec 2008
    Posts
    5
    Quote Originally Posted by zacs7 View Post
    Yucky, there's no need to change the values... just move pointers about and leave the internal values of the structures alone!
    What do you mean by that?

  12. #12
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Have an array of PEOPLE pointers that you move around, ie move the pointers and leave the content alone other than reading the name for sorting that is...

  13. #13
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Ah I see... Well it isn't my part that find/replace isn't 100&#37; idiot proof. Especially when I first did find/replace "." with "[color=red]->[/color]" then realized, oops and did a find replace on "temp[color=red]->[/color]" with "temp."

    Oh well. I should have done my find/replace a little more carefully -_- In either case, the original code stored data in an array.

    I think this would be apt (but I am only guessing since the OP still hasn't posted the structure as I had asked before):

    Example:
    Code:
    /* at the top put this */
    #include <string.h>
    
    void swap (PERSON* people1, PERSON* people2)
    {
      PERSON temp;
    
      memcpy(&temp, people1, sizeof temp);
      memcpy(people1, people2, sizeof temp);
      memcpy(people2, &temp);
    }
    But I am lazy and crazy like that.

  14. #14
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    zac7's approach would be something like

    Example:
    Code:
    PERSON **sort(PERSON people[], int num)
    {
       PERSON **r = malloc(sizeof(*r) * num), *tmp;
       int pass, trav, min;
    
       if(!r)
          return 0;
    
       for(pass = 0; pass < num; ++pass)
          r[pass] = people + pass;
    
       for (pass = 0; pass < num; pass++);
       {
          for (min = pass, trav = pass +1; trav < num; trav++)
          {
             if (strcmp(r[trav]->fName, r[min]->fName) == 0)
                {
                   min = trav;
                } 
          }
          
          tmp = r[trav];
          r[trav] = r[min];
          r[min] = tmp;
       }
    
       return r;
    }

  15. #15
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Sigh, lets all ignore the proper way to do it...

    Code:
    #include <stdio.h>
    
    typedef struct people_s
    {
       char name[32];
    } people_t;
    
    void swap(people_t ** a, people_t ** b);
    
    int main(void)
    {
       people_t persons[2] = {{"Adam"}, {"Zac"}};
       people_t * sorted[2];
    
       
       /* test */
       sorted[0] = &persons[0];
       sorted[1] = &persons[1];
    
       printf("Before: [&#37;s, %s]\n", sorted[0]->name, sorted[1]->name);
    
       /* sort */
       swap(&sorted[0], &sorted[1]);
    
       printf("After: [%s, %s]\n", sorted[0]->name, sorted[1]->name);
       return 0;
    }
    
    void swap(people_t ** a, people_t ** b)
    {
       people_t * tmp = NULL;
    
       tmp = *a;
       *a = *b;
       *b = tmp;
    }

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Issue with program that's calling a function and has a loop
    By tigerfansince84 in forum C++ Programming
    Replies: 9
    Last Post: 11-12-2008, 01:38 PM
  2. Need help with a program, theres something in it for you
    By engstudent363 in forum C Programming
    Replies: 1
    Last Post: 02-29-2008, 01:41 PM
  3. Replies: 4
    Last Post: 02-21-2008, 10:39 AM
  4. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  5. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 11:04 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21