Alphabetically sorting

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • k1ckthem1dget
    New Member
    • Nov 2006
    • 15

    Alphabetically sorting

    My program displays the unsorted list of names in the array. How do i display these names in alphabetical order also.

    Here is my code.

    #include <iostream>
    using namespace std;

    const int numNames = 10;
    const int nameSize = 17;

    void showArray(char [][nameSize],char);
    void selectionSort(i nt [][nameSize], int);

    int main()
    {

    char names [numNames][nameSize]=
    {"Collins,Bill" ,
    "Smith,Bart ",
    "Allen,Jim" ,
    "Griffin,Ji m",
    "Stamey,Mar ty",
    "Rose,Geri" ,
    "Taylor,Ter ri",
    "Johnson,Ji ll",
    "Allison,Je ff",
    "Looney,Joe "};

    showArray(names ,numNames);

    }
    void showArray(char array [][nameSize], char size)
    {
    for(int counter = 0; counter < size; counter++)
    {
    cout << array[counter] << endl;
    }
    }

    void selectionSort(c har array[][nameSize],int size)
    {
    int counter, minRow;
    char minValue[17];

    for(counter = 0; counter < (size - 1); counter++)
    minRow = counter;
    strcpy(minValue ,array[counter]);

    for(int index = counter+1; index < size; index++)
    { if(strcmp(array[index],minValue)<0)
    strcpy(minValue ,array[index]);
    minRow = index;
    }
    }

    The output is

    Collins,Bill
    Smith,Bart
    Allen,Jim
    Griffin,Jim
    Stamey,Marty
    Rose,Geri
    Taylor,Terri
    Johnson,Jill
    Allison,Jeff
    Looney,Joe


    My question is how do i sort them alphabetically also.
  • Ganon11
    Recognized Expert Specialist
    • Oct 2006
    • 3651

    #2
    Use a sort method - these can be found on the internet pretty easily. It looks like you already have a function prototype called selectionSort - just fill in the function definition, sort the list, and print again.

    Comment

    Working...