Thread: Question sorting character arrays: C programming

  1. #1
    Registered User
    Join Date
    May 2003
    Posts
    2

    Unhappy Question sorting character arrays: C programming

    I was just wondering why my code isn't working as it should, I will sort well for the FIRST letter, but will not keep checking to sort names in TRUE ALPHABETICAL order. Here is my function that I made to do this sorting:

    for(pass = 0; pass < CLASS-1; pass++)
    {
    for(i = 0; i < CLASS-1; i++)
    {
    letterOne = students[i].lastName[0];
    letterTwo = students[i+1].lastName[0];

    if(letterOne > letterTwo)
    {
    for(k = 0; k < LAST_NAME; k++)
    {
    lastNameTempSpace[k] = students[i].lastName[k];
    firstNameTempSpace[k] = students[i].firstName[k];
    }

    for(k = 0; k < LAST_NAME; k++)
    {
    students[i].lastName[k] = students[i+1].lastName[k];
    students[i].firstName[k] = students[i+1].firstName[k];
    }

    for(k = 0; k < LAST_NAME; k++)
    {
    students[i+1].lastName[k] = lastNameTempSpace[k];
    students[i+1].firstName[k] = firstNameTempSpace[k];
    }
    }

    while(letterOne == letterTwo)
    {
    while(letterOne != ' ' && letterTwo != ' ')
    {
    l++;

    letterOne = students[i].lastName[l+1];
    letterTwo = students[i+1].lastName[l+1];

    if(letterOne > letterTwo)
    {
    for(k = 0; k < LAST_NAME; k++)
    {
    lastNameTempSpace[k] = students[i].lastName[k];
    firstNameTempSpace[k] = students[i].firstName[k];
    }

    for(k = 0; k < LAST_NAME; k++)
    {
    students[i].lastName[k] = students[i+1].lastName[k];
    students[i].firstName[k] = students[i+1].firstName[k];
    }

    for(k = 0; k < LAST_NAME; k++)
    {
    students[i+1].lastName[k] = lastNameTempSpace[k];
    students[i+1].firstName[k] = firstNameTempSpace[k];
    }
    if(letterOne == letterTwo)
    {
    letterOne = students[i].lastName[l];
    letterTwo = students[i+1].lastName[l];
    }
    }
    }
    }
    }
    }

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    It doesn't work because that's a horrible way to do it. Let me introduce you to your new friend!

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    May 2003
    Posts
    2
    pointers? I'm not there yet, Im just trying to really get this done using arrays and structs without any pointers...or any other function i read: strcmp...what does that do? compares both strings, but then what? where do i go from there?

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Originally posted by NeoSigma
    pointers? I'm not there yet, Im just trying to really get this done using arrays and structs without any pointers...or any other function i read: strcmp...what does that do? compares both strings, but then what? where do i go from there?
    It compares two strings and tells you which one comes first. Then you'll know if you need to swap their position or not. Assuming you are actually trying to sort them...

    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sorting number
    By Leslie in forum C Programming
    Replies: 8
    Last Post: 05-20-2009, 04:23 AM
  2. Comparing Arrays question
    By taugust7 in forum C++ Programming
    Replies: 36
    Last Post: 04-14-2009, 12:29 PM
  3. Question about char arrays
    By PJYelton in forum C++ Programming
    Replies: 5
    Last Post: 10-21-2003, 12:44 AM
  4. Quick question on sorting a singly linked list
    By Ham in forum C++ Programming
    Replies: 1
    Last Post: 10-08-2001, 11:26 PM
  5. an actual question (sorting arrays)
    By master5001 in forum C Programming
    Replies: 4
    Last Post: 08-13-2001, 10:21 PM