using selection sort with a 2D char array
Hi,
I need to perform a selectionSort on a two dimensional array of characters and for some reason, although the code is compiling, when I start the executable, nothing happens. Could someone help me please?
Code:
#include <iostream>
#include <iomanip>
using namespace std;
char initArray();
void selectionSort(char names[20][17]);
int searchName(char names[20][17]);
int binarySearch(char array[1][17], char names[20][17]);
int main()
{
char initArray();
}
char initArray()
{
const int NUM_NAMES= 20, SIZE = 17;
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"};
return names[20][17];
selectionSort(names);
}
void selectionSort(char names[][17])
{
int number, startScan[81], minIndex[81], elems;
elems = 17;
char minValue[1][17];
for (number = 0; number < (elems - 1); number++)
{
minIndex[81] = number;
strcpy(minValue[17], names[17]);
for (int index = number+1; index < elems; index++)
{
if (strcmp(names[number], minValue[81]) != 0)
{
strcpy(minValue[81], names[number]);
minIndex[81] = index;
}
}
strcpy(names[index], names[number]);
strcpy(names[number], minValue[17]);
}
searchName(names[20][17]);
}
}
int searchName(char names[][17])
{
char name[1][17];
int position;
cout << "Please enter a name putting the last name first.\n";
cin >> name[0][17];
binarySearch(name, names);
cout << name << " was found here: " << position << "\n";
exit(0);
return position;
}
int binarySearch(char name[1][17], char names[][17])
{
int elems = 17,
first = 0,
last = elems - 1,
middle,
position = -1;
bool found = false;
while (!found && first <= last)
{
middle = (first + last)/2;
if(strcmp(names[middle], name[17]) == 0)
{
found = true;
position = middle;
}
else if
(strcmp(names[middle], name[17]) <= 1)
last = middle - 1;
else
first = middle +1;
}
return position;
}