Sorting a 2d array of strings in C++??
Hi y'all!
I found this forum through google and I was hoping that I could get some help!!
I've been assigneda rather odd assignment by my C++ teacher. I have a 2d array of names (names[17][20]) which has to be sorted alphabetically via selection sort, we can not use pointers. We then have to do a Binary Search and find a name input by the user, if the name is found we have to display it and its position in the array, if the name is not found we have to return the name and the fact that it's not found.
My 3 friends and I have spent all weekend on this assignment, and searching for possible help, but we have only been able to come up with following (I'm sorry for the way this is formatted I don't know how to do the cool formatting I've seen on here) :
#include <iostream>
#include <cstring>
using namespace std;
int initArray();
int selectionSort(char names[20][17]);
int searchName(char names[20][17]);
int binarySearch(char array[], char names[20][17]);
int main()
{
int initArray();
}
int initArray()
{
char names[20][17]= {"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"};
selectionSort(names[20][17]);
}
int 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 in [Last, First] format.\n";
cin >> name[0][17];
binarySearch(name[1][17], names[20][17]);
cout << name << " was found in position: " << position << "!\n";
exit(0);
}
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;
}
I realize there are ALOT of errors in this program but we would be appreciative on any help at all that you could offer us!
I found this forum through google and I was hoping that I could get some help!!
I've been assigneda rather odd assignment by my C++ teacher. I have a 2d array of names (names[17][20]) which has to be sorted alphabetically via selection sort, we can not use pointers. We then have to do a Binary Search and find a name input by the user, if the name is found we have to display it and its position in the array, if the name is not found we have to return the name and the fact that it's not found.
My 3 friends and I have spent all weekend on this assignment, and searching for possible help, but we have only been able to come up with following (I'm sorry for the way this is formatted I don't know how to do the cool formatting I've seen on here) :
#include <iostream>
#include <cstring>
using namespace std;
int initArray();
int selectionSort(char names[20][17]);
int searchName(char names[20][17]);
int binarySearch(char array[], char names[20][17]);
int main()
{
int initArray();
}
int initArray()
{
char names[20][17]= {"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"};
selectionSort(names[20][17]);
}
int 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 in [Last, First] format.\n";
cin >> name[0][17];
binarySearch(name[1][17], names[20][17]);
cout << name << " was found in position: " << position << "!\n";
exit(0);
}
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;
}
I realize there are ALOT of errors in this program but we would be appreciative on any help at all that you could offer us!
0
Comments
What you actually have is an array of names. You have 20 char arrays of length 17.
Are you looking for help on a certain part or what?
Actually we have fixed a few problems and are running into some more!
#include <iostream>
#include <cstring>
using namespace std;
void initArray();
void selectionSort(char names[20][17]);
void searchName(char names[20][17]);
int binarySearch(char name[][17], char names[20][17]);
void DoOperationAgain();
int main()
{
initArray();
}
void initArray()
{
char names[20][17]= {"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"};
selectionSort(names);
}
void selectionSort(char names[][17])
{ int NumEles = 17;
int number, minIndex, elems;
elems = 17;
char minValue[1][17];
cout << "The unsorted names are \n";
for (int count = 0; count < NumEles; count++)
cout << names[count] << endl;
cout << "Press [Enter] to continue!";
cin.get();
cin.ignore();
system ("cls");
for (number = 0; number < (elems -1); number++)
{
minIndex = number;
strcpy(minValue[17], names[17]);
for (int index = number + 1; index < elems; index++)
{
if (strcmp(names[number], minValue[17]) !=0)
{
strcpy(minValue[17], names[number]);
minIndex = index;
}
}
strcpy(names[index], names[number]);
strcpy(names[number], minValue[17]);
}
cout << "The sorted names are \n";
for (int count = 0; count < NumEles; count++)
cout << names[count] << endl;
cout << "Press [Enter] to continue!";
cin.get();
cin.ignore();
searchName(names);
}
void searchName(char names[][17])
{char name[1][17];
int position = -1;
cout << "Please enter a name in [Last, First] format.\n";
cin >> name[0][17];
binarySearch(name, names);
if (position < 0)
{
cout << "Sorry, name was not found.\n";
cin.get();
cin.ignore();
}
else
{
cout << name << " was found in position: " << position << "!\n";
}
DoOperationAgain();
}
int binarySearch (char name [1][17], char names [][17])
{
int numElems = 17;
int first = 0, last = numElems - 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;
}
void DoOperationAgain()
{
char choice;
int safe;
safe = 0;
cout << "Would you like to repeat the operation?\n";
cout << "[Y]es or [N]o\n";
cin >> choice;
do
{
switch (choice)
{
case 'y':
case 'Y': {
system("cls");
initArray();
}
break;
case 'n':
case 'N': exit(0);
break;
default:{
cout << "Please enter a valid value!\n";
safe = 1;
}
}
}
while (safe != 0);
}
The problem is putting data into the string. We've tried one and 2 letters for the search and they cause no errors, but if you enter three or more you get a never ending loop! Also the sort does not work at all!
:bawling: :bawling: :bawling:
I have an idea of how to but not sure