Thread: help needed for sorting nested loops...segmentation error

  1. #1
    Registered User
    Join Date
    Oct 2008
    Posts
    46

    help needed for sorting nested loops...segmentation error

    ok i have a project due wedsnday i am trying to sort a list of structures...when i run the program i get a segmentation fault..please help
    Code:
    void exchangeSmallest (STUDENT student[], int i, int last)
    {
    /* Local Definitions */   
       int walker;
       int smallest;
       STUDENT tempData = {"", "", 2, "123-45-6789", {0,0,0}};
    
    /* Statements */
       smallest = i;
       for (walker = i + 1; walker <= last; walker++)
         if (strcmp(student[walker].lName, student[smallest].lName) == 0)
          {
           if (strcmp(student[walker].fName, student[smallest].fName) == 0)
            {
             if (strcmp(student[walker].ssn, student[smallest].ssn) < 0)
              {
               smallest = walker;
              }
            } 
            else
             {
               if (strcmp(student[walker].fName, 
                   student[smallest].fName) < 0)
                {
                  smallest = walker;
                } 
             }
          }
         else
          { 
            if (strcmp(student[walker].fName,
                    student[smallest].fName) < 0)
             { 
                 smallest = walker;
             } 
          }  
      
          strcpy(tempData.fName, student[i].fName);
          strcpy(tempData.lName, student[i].lName);
          tempData.grade = student[i].grade;
          strcpy(tempData.ssn, student[i].ssn);
          tempData.bDate.month = student[i].bDate.month;
          tempData.bDate.date = student[i].bDate.date;
          tempData.bDate.year = student[i].bDate.year;
          strcpy(student[i].fName, student[smallest].fName);
          strcpy(student[i].lName, student[smallest].lName);
          student[i].grade = student[smallest].grade;
          strcpy(student[i].ssn, student[smallest].ssn);    
          student[i].bDate.month = student[smallest].bDate.month;
          student[i].bDate.date = student[smallest].bDate.date;
          student[i].bDate.year = student[smallest].bDate.year; 
          strcpy(student[smallest].fName, tempData.fName);
          strcpy(student[smallest].lName, tempData.lName);
          student[smallest].grade = tempData.grade;
          strcpy(student[smallest].ssn, tempData.ssn);
          student[smallest].bDate.month = tempData.bDate.month;
          student[smallest].bDate.date = tempData.bDate.date;
          student[smallest].bDate.year = tempData.bDate.year; 
    printf("%s", student[smallest].fName);
       return;
    } 
    
    /*===================================prtArr=================================*/
    void prtArr(STUDENT* student, int numRecords)
    {
       int i;
       char* Name;
    
       printf("\n%26sName     Level            SSN      Birthdate"," ");
       printf("\n%26s----     -----            ---      ---------"," ");
    
    
       for (i = 0; i < numRecords; i++)
         {
           strcat(strcat(strcpy(Name, student[i].fName), " "), student[i].lName);
           printf("\n%30s %9d  %13s     %02d/%02d/%4d",
                 Name, student[i].grade, student[i].ssn,
                 student[i].bDate.month, student[i].bDate.date,
                 student[i].bDate.year);
         }
       printf("\n\n");
    
    }

  2. #2
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    Well, I am really a bad "spotter" (generally), but could I suggest using a debugger? If you don't want/have just put printf("OK\n") at some points to see where you get the segfault or printf("%d\n") (always the \n) inside loops to see at what iteration there is the error

  3. #3
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    A hunch would be this line:
    Code:
    for (walker = i + 1; walker <= last; walker++)
    particularly the <= ... I suspect that should be just <.

  4. #4
    Registered User
    Join Date
    Oct 2008
    Posts
    46
    ya i tried... to put printf("&#37;s", student[smallest].fName); at the end of exchange smallest and it prints out like 2 first names and a last name after that that are all jumbled together...my infile is a list of names, grades, social, grades, and b-dates.

  5. #5
    Registered User
    Join Date
    Oct 2008
    Posts
    46
    ok i tried the above comment to put just < and it says john,marsupial,justin,marsupial,marsupial but its not sorted

  6. #6
    Registered User
    Join Date
    Oct 2008
    Posts
    46
    here is the module to sort right above this module
    Code:
    /*===================================sortArr=================================*/
    
    void sortArr(STUDENT student[], int last)
    {
       int i;
       
       for (i = 0; i < last; i++)
         exchangeSmallest (student, i, last);
    
       return;
    }

  7. #7
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Code:
           if (strcmp(student[walker].fName, student[smallest].fName) == 0)
            {
             if (strcmp(student[walker].ssn, student[smallest].ssn) < 0)
              {
               smallest = walker;
              }
            } 
            else
             {
               if (strcmp(student[walker].fName, 
                   student[smallest].fName) < 0)
                {
                  smallest = walker;
                } 
             }
          }
         else
          { 
            if (strcmp(student[walker].fName,
                    student[smallest].fName) < 0)
             { 
                 smallest = walker;
             } 
          }
    This would be pretty inefficient, as you are comparing the exact same thing every time. How about:
    Code:
       int fcmp;
    ...
           fcmp = strcmp(student[walker].fName, student[smallest].fName);
           if (fcmp == 0)
            {
             if (strcmp(student[walker].ssn, student[smallest].ssn) < 0)
              {
               smallest = walker;
              }
            } 
            else
             {
               if (fcmp < 0)
                {
                  smallest = walker;
                } 
             }
          }
         else
          { 
            // Is this RIGHT?
            if (fcmp < 0)
             { 
                 smallest = walker;
             } 
          }
    I think the last if-statement should be different, comparing the last name, rahter than first name.

    --
    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.

  8. #8
    Registered User
    Join Date
    Oct 2008
    Posts
    46
    ok i tried this and it gave me a bunch of error messages

  9. #9
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    How about we all have a party at your house, and when we come over, you can show us the error messages?
    Mainframe assembler programmer by trade. C coder when I can.

  10. #10
    Registered User
    Join Date
    Oct 2008
    Posts
    46
    haha sounds good...lol ok sytax error before the second else

  11. #11
    Registered User
    Join Date
    Oct 2008
    Posts
    46
    nevermind it was a forgotten first bracket...i get the first names from the structure but they arent sorted

  12. #12
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    matsp probably typed it in off the top of his head and didn't do any syntax checking. Syntax checking, as might be typical for code snippets like this, is left for the reader.
    Mainframe assembler programmer by trade. C coder when I can.

  13. #13
    Registered User
    Join Date
    Oct 2008
    Posts
    46
    ok ya the prgram runs but when i echo out the first names they arent sorted...it just gives them as they appear in the file that is opened

  14. #14
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    Aye mateee - post yer code. Arg...
    Mainframe assembler programmer by trade. C coder when I can.

  15. #15
    Registered User
    Join Date
    Oct 2008
    Posts
    46
    Code:
    void sortArr(STUDENT student[], int last)
    {
       int i;
       
       for (i = 0; i < last; i++)
         exchangeSmallest (student, i, last);
    
       return;
    }
    
    /*===============================exchangeSmallest=============================*/
    
    void exchangeSmallest (STUDENT student[], int i, int last)
    {
    /* Local Definitions */   
       int walker;
       int smallest;
       int fcmp;
       STUDENT tempData = {"", "", 2, "123-45-6789", {0,0,0}};
    
    /* Statements */
       smallest = i;
       for (walker = i + 1; walker <= last; walker++)
            
    
           fcmp = strcmp(student[walker].fName, student[smallest].fName);
           if (fcmp == 0)
            {
             if (strcmp(student[walker].ssn, student[smallest].ssn) < 0)
              {
               smallest = walker;
              }
            } 
            else
             {
               if (fcmp < 0)
                {
                  smallest = walker;
                } 
             }
          }
         else
          { 
            // Is this RIGHT?
            if (fcmp < 0)
             { 
                 smallest = walker;
             } 
          }  
      
          strcpy(tempData.fName, student[i].fName);
          strcpy(tempData.lName, student[i].lName);
          tempData.grade = student[i].grade;
          strcpy(tempData.ssn, student[i].ssn);
          tempData.bDate.month = student[i].bDate.month;
          tempData.bDate.date = student[i].bDate.date;
          tempData.bDate.year = student[i].bDate.year;
          strcpy(student[i].fName, student[smallest].fName);
          strcpy(student[i].lName, student[smallest].lName);
          student[i].grade = student[smallest].grade;
          strcpy(student[i].ssn, student[smallest].ssn);    
          student[i].bDate.month = student[smallest].bDate.month;
          student[i].bDate.date = student[smallest].bDate.date;
          student[i].bDate.year = student[smallest].bDate.year; 
          strcpy(student[smallest].fName, tempData.fName);
          strcpy(student[smallest].lName, tempData.lName);
          student[smallest].grade = tempData.grade;
          strcpy(student[smallest].ssn, tempData.ssn);
          student[smallest].bDate.month = tempData.bDate.month;
          student[smallest].bDate.date = tempData.bDate.date;
          student[smallest].bDate.year = tempData.bDate.year; 
    printf("&#37;s", student[smallest].fName);
       return;
    } 
    
    /*===================================prtArr=================================*/
    void prtArr(STUDENT* student, int numRecords)
    {
       int i;
       char* Name;
    
       printf("\n%26sName     Level            SSN      Birthdate"," ");
       printf("\n%26s----     -----            ---      ---------"," ");
    
    
       for (i = 0; i < numRecords; i++)
         {
           strcat(strcat(strcpy(Name, student[i].fName), " "), student[i].lName);
           printf("\n%30s %9d  %13s     %02d/%02d/%4d",
                 Name, student[i].grade, student[i].ssn,
                 student[i].bDate.month, student[i].bDate.date,
                 student[i].bDate.year);
         }
       printf("\n\n");

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. An error is driving me nuts!
    By ulillillia in forum C Programming
    Replies: 5
    Last Post: 04-04-2009, 09:15 PM
  2. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 03:07 AM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  4. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-29-2005, 11:21 PM
  5. Stupid compiler errors
    By ChrisEacrett in forum C++ Programming
    Replies: 9
    Last Post: 11-30-2003, 05:44 PM