I'm being given the assignment of using the selection sort method to alphabetize a 2D array of names. Specifically I am supposed to modify a selection sort algorithm for integers and convert it for my purposes. I took the skeleton code and tried to convert it. The program compiles and executes but does not run, i.e. it doesn't output anything, just a blinking cursor in the box. Obviously I've made a logical error but I cannot find it. Any help?

#include <iostream>

using namespace std;

const int NUM_NAMES = 20, SIZE = 17;

void CopyName(char [][SIZE], char [], int);
void CopyName2(char [], char [][SIZE], int);
void Rearrange(char [][SIZE], int, int);
int CompareString(char [][SIZE], char [], int);
void output(char[][SIZE]);

int main()
{
        char names[NUM_NAMES][SIZE] = {"Collins, Bill", "Smith, Bart", "Allen, Jim",
                                       "Griffin, Jim", "Stamey, Marty", "Rose, Geri",
                                       "Taylor, Terri", "Johnson, Jill",
                                       "Allison, Jeff", "Looney, Joe", "Wolfe, Bill",
                                       "James, Jean", "Weaver, Jim", "Pore, Bob",
                                       "Rutherford, Greg", "Javens, Renee",
                                       "Harrison, Rose", "Setzer, Cathy",
                                       "Pike, Gordon", "Holland, Beth" };

        int startScan, minIndex;
        char minValue[SIZE];

        for (startScan = 0; startScan < (NUM_NAMES - 1); startScan++)
        {
                minIndex = startScan;
                CopyName(names, minValue, startScan);
                for (int index = (startScan + 1); index < NUM_NAMES; index++)
                {
                        if (CompareString(names, minValue, index) == 1)
                        {
                                CopyName(names, minValue, index);
                                minIndex = index;
                        }
                }
                Rearrange(names, minIndex, startScan);
                CopyName2(minValue, names, startScan);

        }

        output(names);
        system("PAUSE");
        return 0;
}

void output(char names[][SIZE])
{
        for (int i=0; i < NUM_NAMES; i++)
        {
                for (int j=0; j < SIZE; j++)
                        cout << names[i][j];
                cout << endl;
        }
}

void Rearrange(char name[][SIZE], int row1, int row2)
{
        for(int i=0; i < SIZE; i++)
        {
                name[row1][i] = name[row2][i];
        }
}

void CopyName(char name[][SIZE], char copy[], int row)
{
        for(int i=0; i < SIZE; i++)
        {
                copy[i] = name[row][i];
        }
}

void CopyName2(char copy[], char name[][SIZE], int row)
{
        for(int i=0; i < SIZE; i++)
        {
                name[row][i] = copy[i];
        }
}

int CompareString(char names[][SIZE], char other[], int row)
{
        int result;
        int index = 0;
        bool compare = 0;
        while(compare == 0)
        {
                if (names[row][index] < other[index])
                {
                        result = 1;
                        compare = 1;
                }
                if (names[row][index] > other[index])
                        result = 0;
                if (names[row][index] == other[index])
                        index++;
        }

        return result;
}

Recommended Answers

All 3 Replies

Your compare function has at least 2 problems
1. It doesn't always increment index, so it can get stuck in an infinite loop.
2. result isn't always assigned, so it can return junk.

Why are you reinventing wheels? There are string functions for doing the comparison, for copying one string to another.

If the assignment requires you to create your own string functions, you still don't need the two copy functions - one will do. Note that you can send a single row of the names array and it will be treated as a 1D array, such as strcpy( names[0], names[1] ); Also, when writing your own string functions, generally you don't loop till SIZE elements are handled, you go till the NULL terminator is encountered.

Thanks for pointing those errors out. I fixed it now.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.