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;
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 = 20;
int number, minIndex, elems;
elems = 20;
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();
system ("cls");
for (number = 0; number < (elems - 1); number++)
{
minIndex = number;
strcpy(minValue[17], names[number]);
for (int index = number + 1; index < elems; index++)
{
if (strcmp(names[index], minValue[17]) < 0)
{
strcpy(minValue[17], names[index]);
minIndex = index;
}
}
strcpy(names[minIndex], 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();
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.getline(name[17], 17);
binarySearch(name, names);
if (position < 0)
{
cout << "Sorry, name was not found.\n";
cout << "Press [Enter] to continue.\n";
cin.get();
system("cls");
}
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;
do
{
cout << "Would you like to repeat the operation?\n";
cout << "[Y]es or [N]o\n";
cin >> choice;
system("cls");
switch (choice)
{
case 'y':
case 'Y': {
cin.ignore();
system("cls");
initArray();
}
break;
case 'n':
case 'N': exit(0);
break;
default:{
cout << "Please enter a valid value!\n";
safe = 1;
DoOperationAgain();
}
}
}
while (safe != 0);
}
Code tags added. -Narue
When it sorts, it only does about 1/2 of them and then leaves the rest (it also cuts up some of the names :mad: ). Also we can't get the search function to work!
Could y'all please help us?! :cry: :cry: