Can't figure out how to swap

This is a discussion on Can't figure out how to swap within the C Programming forums, part of the General Programming Boards category; With this program, I'm trying to open a file that contains a list of people's names, birth dates, grade numbers, ...

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

    Can't figure out how to swap

    With this program, I'm trying to open a file that contains a list of people's names, birth dates, grade numbers, and SSN. I can get it to read the pieces from the file and print them but I'm trying to get it in alphabetical order with a selection sort and it will not swap them. :/. plz help. and btw, theres certain parts in here that won't make sense why i have them in there, but that's because theres much more to the program to be done. thank you for your time

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <stdbool.h>
    #include <string.h>
    
    #define MAX 20
    #define SIZE 5
    
    //date structure
    typedef struct
    { 
       char month[MAX];
       int date;
       int year;
    } DATE;
    
    //student structure
    typedef struct
    {
       char fName[MAX];
       char lName[MAX];
       int gLevel;
       char sNumber[MAX];
       DATE bDate;
    } STUDENT;
    
    //functions
    void fill(STUDENT identity[], FILE* fpIn, FILE* fpOut);
    void printArr(STUDENT identity[]);
    bool openFiles(FILE **pFPIn, FILE **pFPOut);
    void prt(STUDENT identity);
    void sort(STUDENT identity[]);
    void swap(STUDENT *pN1, STUDENT *pN2);
    
    //main function
    int main(int argc, char* argv[])
    {
       STUDENT identity[SIZE];
     
       FILE *fPIn, *fPOut;
       int i;
       if (openFiles(&fPIn, &fPOut))
          {
             fill(identity, fPIn, fPOut);
             printf("\nCreated by _______");
             printf("\n\n%26s %10s %15s %17s", "Name", "Level", 
                    "SSN", "Birthdate");
             printf("\n%26s %10s %15s %17s", "----", "-----", 
                    "---", "---------");
             printArr(identity);    //prints the unsorted array
             sort(identity);        //sorts the array
             printArr(identity);    //prints the sorted array 
    
           }
      
           
       fclose(fPIn);
       fclose(fPOut);
      
       return 0;
    }
    
    //generate function
    void fill(STUDENT identity[], FILE* fPIn, FILE* fPOut)
    {
       int i;
       
       //for loop
       for (i = 0; i < SIZE; i++)
          {
             fscanf(fPIn, "%s%s%d%s%s%d%d", 
                 identity[i].fName, identity[i].lName,
                 &identity[i].gLevel, identity[i].sNumber,
                 &identity[i].bDate.month, &identity[i].bDate.date,
                 &identity[i].bDate.year);
          }
    }
    
    //print function into array of structures
    void printArr(STUDENT identity[])
    {
       int i;
       
       //for loop
       for (i = 0; i < SIZE; i++)
         {
           printf("\n");
           prt(identity[i]);
         
         }
       printf("\n\n");
    }
    
    //opens the files
    bool openFiles(FILE **pFPIn, FILE **pFPOut)
    {
       bool success = false;   
    
       if ((*pFPIn = fopen("t1", "r")) != NULL)
       {
          if ((*pFPOut = fopen("t1.out", "w")) != NULL)
             success = true;
          else
             printf("\nUnable to open \"t1.out\" for writing.\n\n");
       }
       else
          printf("\nUnable to open \"t1.txt\" for reading.\n\n");
       
       return success;
    }
    //prints the function   
    void prt(STUDENT identity)
    {
       
        printf("%17s %8s %10d %15s %9s %2d %d%", identity.fName, 
             identity.lName, 
             identity.gLevel,
             identity.sNumber,
             identity.bDate.month,
             identity.bDate.date,
             identity.bDate.year);
    }
    
    //selection sort function
    void sort(STUDENT identity[])
    {
    
       int pass, min, trav;
       
       for (pass = 0; pass < SIZE - 1; pass++)
        {
          for (min = pass, trav = pass + 1; trav < SIZE; trav++)
             { 
                if (identity[trav].lName < identity[min].lName)
                   {
                      min = trav;
                   }
                trav++;
                       
             } 
          swap(&identity[pass], &identity[min]);
          pass++;
        } 
    }
    
    //swap function
    void swap(STUDENT *pN1, STUDENT *pN2)
    {
       char temp;
       
       temp = pN1;
       pN1 = pN2;
       pN2 = temp;
    }
    The current swap function is the only way that I have done that would compile, it gave me warning messages however.

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Just one post on the subject would be enough, I think.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Dec 2008
    Posts
    16
    this is what i'm getting it to print

    Code:
    $ ./a.out
    
    Created by 
    
                          Name      Level             SSN         Birthdate
                          ----      -----             ---         ---------
                Peter  Griffin          1     111-11-1111       May  9 1965
                 Lois  Griffin          3     234-33-2345     April  2 1966
                Glenn Quagmire          7     555-41-2143   January 10 1965
                  Tom   Tucker          2     999-99-9999      July  4 1970
                 Adam     West         10     444-22-3434  November  4 1960
    
    
                Peter  Griffin          1     111-11-1111       May  9 1965
                 Lois  Griffin          3     234-33-2345     April  2 1966
                Glenn Quagmire          7     555-41-2143   January 10 1965
                  Tom   Tucker          2     999-99-9999      July  4 1970
                 Adam     West         10     444-22-3434  November  4 1960

  4. #4
    Registered User
    Join Date
    Dec 2008
    Posts
    16
    oops, that first one i didnt mean to send. i had clicked on preview and went back to edit i thought. its early. sorry :/

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Well, I can see that the code isn't going to work, I'm just wondering if you have enabled ANY compiler warnings - as your posted code assigns a pointer to a char, then assigns two pointers, and then assigns the original char into another pointer. And most compilers should definitely warn about this code.

    Think about what you are swapping, and what your temporary object is supposed to be. Also consider what the rules are for changing things outside of a function from inside the function.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  6. #6
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,473
    and why to post such a long code to ask a question about function of 3 lines long?
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  7. #7
    Registered User
    Join Date
    Dec 2008
    Posts
    16
    well after a few replies on different threads by many different people. i've noticed that a lot ask for more code posted, so i thought id just cancel out that, but i apologize if it bothers you :/

  8. #8
    Registered User
    Join Date
    Dec 2008
    Posts
    16
    and i've gotten it to give no warnings now, but it is still not swapping. baby steps

    [CODE]
    void swap(STUDENT *pN1, STUDENT *pN2)
    {
    STUDENT *temp[MAX];

    *temp = pN1;
    pN1 = pN2;
    pN2 = *temp;
    }
    /CODE]

  9. #9
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    I doubt that compiles either (at least not without warnings). You may want to enable [and fix] warnings when you compile your code.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  10. #10
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,473
    Quote Originally Posted by deathrattle View Post
    well after a few replies on different threads by many different people. i've noticed that a lot ask for more code posted, so i thought id just cancel out that, but i apologize if it bothers you :/
    yes, we can ask to post more code, when posted code seems to be ok and problem should be in the calling function.

    in your case - the problem is clearly inside your swap function, so posting just:
    Code:
    void swap(STUDENT *pN1, STUDENT *pN2)
    {
       char temp;
       
       temp = pN1;
       pN1 = pN2;
       pN2 = temp;
    }
    would make it easier for anyone ready to help to see th eproblem and thus answers would be delivered faster...

    I haven't even saw your function till I scrolled the code block down

    as was said - your problem is type of the temp var and assigning pointers instead of oblects
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  11. #11
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,473
    Quote Originally Posted by deathrattle View Post
    and i've gotten it to give no warnings now, but it is still not swapping. baby steps

    Code:
    void swap(STUDENT *pN1, STUDENT *pN2)
    {
       STUDENT *temp[MAX];   
             
       *temp = pN1;
       pN1 = pN2;
       pN2 = *temp;
    }
    you need 1 temp variable of type STUDENT instead of array of pointers
    and you should swap objects instead of pointers

    Alternatively - if you really want to swap only pointers

    your temp should be just one pointer - not array
    and your arguments should be passed as pointer to pointer instead of just pointer
    PS. but this is NOT your case... I have checked your sort function to be sure what are your real intentions...
    Last edited by vart; 04-14-2009 at 07:35 AM.
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  12. #12
    Registered User
    Join Date
    Dec 2008
    Posts
    16
    alright, i changed it from an array to just one 1 temp variable. i then changed them from pointers to just objects. i then had to change the & symbols from the calling function. it compiles with no warnings but still no swapping. and i also realized that by last name, the names i had in the file were already in order except for the two griffins. lawlz. so i switched adam wests place to the top to make it simple.

  13. #13
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,473
    Quote Originally Posted by deathrattle View Post
    alright, i changed it from an array to just one 1 temp variable. i then changed them from pointers to just objects. i then had to change the & symbols from the calling function. it compiles with no warnings but still no swapping. and i also realized that by last name, the names i had in the file were already in order except for the two griffins. lawlz. so i switched adam wests place to the top to make it simple.
    no - you still need to pass pointers to objects to swap them - so your function prototype was ok - do not change it, work only on the function body
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  14. #14
    Registered User
    Join Date
    Dec 2008
    Posts
    16
    alrighty, its back to pointers. again it compiles but still no swaps.

    Code:
    void swap(STUDENT *pN1, STUDENT *pN2)
    {
       STUDENT temp;
              
       temp = *pN1;
       *pN1 = *pN2;
       *pN2 = temp;
    }

  15. #15
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by deathrattle View Post
    alrighty, its back to pointers. again it compiles but still no swaps.

    Code:
    void swap(STUDENT *pN1, STUDENT *pN2)
    {
       STUDENT temp;
              
       temp = *pN1;
       *pN1 = *pN2;
       *pN2 = temp;
    }
    That does look fine to me. Are you sure that you are not having some other problem compiling the code, and that is making your OLD code run? [You wouldn't be the first, I've done it more than once - try changing the code in some OBVIOUS way (e.g. add an extra printout somewhere, and confirm that this happens)].

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

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

Similar Threads

  1. linked list swap function
    By Axel in forum C Programming
    Replies: 32
    Last Post: 01-16-2011, 08:40 AM
  2. Atomic compare and swap.
    By Maz in forum C Programming
    Replies: 3
    Last Post: 04-07-2009, 02:47 PM
  3. using swap to make assignment operator exception safe
    By George2 in forum C++ Programming
    Replies: 9
    Last Post: 01-10-2008, 05:32 AM
  4. Double Link List Swap function
    By kennny2004 in forum C++ Programming
    Replies: 1
    Last Post: 04-12-2006, 11:52 AM

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